26
votes

Is there any function in the OpenCV python wrapper that does the same thing as Mat's convertTo method in OpenCV 2?

I basically want to call this function in python

out.convertTo( out, CV_32F, 1.0/255, 0 );

where out is a grayscale image.

I have already made use of cv.ConvertScale by keeping my dst argument as type CV_32FC1, but I am trying to keep my python code as cv2 conformant as possible. Any clues?

3

3 Answers

18
votes

You can simply use Numpy functions for this.

eg :

res = np.float32(out)

scaling, you will have to do separately:

res = res*scaling_factor
15
votes

If you are not trying to convert the data type, use this:

cv2.convertScaleAbs(image, result, alpha, beta)

where alpha is you scale factor and beta is a shift value. More details in the OpenCV docs.

2
votes

In the OP,

0 < multiplier < 1,

so you don't have to worry about underflow or overflow. Solutions from Adid Rahman K and knipknap will work just fine. And they should be plenty fast.

If, for any reason, you need a multiplier > 1, then you can have problems with overflow. That is, the value is not big enough to fit in the chosen data type. Most OpenCV functions will handle overflow by truncating to the max value of the data type. NumPy, though, will just "roll over" the value (e.g. for an 8 bit data type -- max value 255 -- OpenCV will force 260 to become 255, but NumPy will force 260 to 4!).

So to process an 8 bit grayscale image and handle under/over flows do this:

img2 = np.int16(img1)     # convert to signed 16 bit integer to allow overflow
img2 = scale_factor*img2  # apply scale factor
img2 = clip(img2, 0, 255) # force all values to be between 0 and 255

# after clip img2 is effectively unsigned 8 bit, but make it explicit:
img2 = np.uint8(img2)