Search code examples
pythonc++numpyopencv

convert image from CV_64F to CV_8U


I want to convert an image of type CV_64FC1 to CV_8UC1 in Python using OpenCV.

In C++, using convertTo function, we can easily convert image type using following code snippet:

image.convertTo(image, CV_8UC1);

I have searched on Internet but unable to find any solution without errors. Any function in Python OpenCV to convert this?


Solution

  • You can convert it to a Numpy array.

    import numpy as np
    
    # Convert source image to unsigned 8 bit integer Numpy array
    arr = np.uint8(image)
    
    # Width and height
    h, w = arr.shape
    

    It seems OpenCV Python APIs accept Numpy arrays as well. I've not tested it though. Please test it and let me know the result.