numpyopencvimage-processingpython-imaging-library

OpenCV - convert uint8 image to float32 normalized image


I'm trying to convert parts of a Keras DarkNet code to try to make the code run faster. Here is the code I'm trying to optimize:

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

This is my attempt to achieve the same output without using the intermediate PIL coversion to reduce time:

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

However, upon running the code, it will return:

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''

It seems like I need to change the uint8 type to float32 before normalizing but I'm not sure how to achieve it with OpenCV.


Solution

  • You can use resized_image.astype(np.float32) to convert resized_image data from unit8 to float32 and then proceed with normalizing and other stuffs:

    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.