gpt4 book ai didi

numpy - OpenCV - 将 uint8 图像转换为 float32 标准化图像

转载 作者:行者123 更新时间:2023-12-02 16:20:55 24 4
gpt4 key购买 nike

我正在尝试转换部分 Keras DarkNet 代码,以尝试使代码运行得更快。
这是我正在尝试优化的代码:

model_image_size = (416, 416)

import cv2
from PIL import Image

frame = cv2.imread("test.png", cv2.IMREAD_COLOR)

im = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
im = Image.fromarray(im).crop((1625, 785, 1920, 1080)) # crop ROI

resized_image = im.resize(tuple(reversed(model_image_size)), Image.BICUBIC)
image_data = np.array(resized_image, dtype='float32')

image_data /= 255.
image_data = np.expand_dims(image_data, 0) # Add batch dimension.

return image_data

这是我在不使用中间 PIL 覆盖来减少时间的情况下实现相同输出的尝试:
model_image_size = (416, 416)

import cv2

frame = cv2.imread("test.png", cv2.IMREAD_COLOR)

frame = frame[785:1080,1625:1920] # crop ROI
im = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

resized_image = cv2.resize(im, model_image_size, interpolation = cv2.INTER_CUBIC)

resized_image /= 255.
image_data = np.expand_dims(resized_image, 0) # Add batch dimension.

return image_data

但是,在运行代码时,它将返回:
resized_image /= 255.
TypeError: ufunc 'true_divide' output (typecode 'd') could not be coerced to provided output parameter (typecode 'B') according to the casting rule ''same_kind''

看来我需要更改 uint8输入到 float32在规范化之前,但我不确定如何使用 OpenCV 实现它。

最佳答案

您可以使用 resized_image.astype(np.float32)转换 resized_image数据来自 unit8float32然后继续规范化和其他东西:

frame = cv2.imread("yourfile.png")

frame = frame[200:500,400:1000] # crop ROI
im = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

model_image_size = (416, 416)
resized_image = cv2.resize(im, model_image_size, interpolation = cv2.INTER_CUBIC)
resized_image = resized_image.astype(np.float32)
resized_image /= 255.

image_data = np.expand_dims(resized_image, 0) # Add batch dimension.

关于numpy - OpenCV - 将 uint8 图像转换为 float32 标准化图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57325720/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com