Skip to content
Advertisement

implement mat2gray in Opencv with Python

I have the same problem with him: Scaling a matrix in OpenCV

I got the same problem with him, I have a colored picture, I used matlab to read the picture: Input = imread('input1.jpg');, and the format of the picture is 612x612x3 uint8, I print the 5x5x1 pixel in the picture as below:Input(1:5,1:5,1)

JavaScript

by using the mat2gray function: rgb_out = mat2gray(Input);, these pixels can be transformed to this, they all in the range between 0 and 1: rgb_out(1:5,1:5,1)

JavaScript

so the question is how can I implement this in Opencv with Python, I tried the code as below:

JavaScript

but the first print is :

JavaScript

and the elements in the rgb_out is no more than 1 or 0. please help, thanks.

Advertisement

Answer

Your input matrix is an integer datatype and your output matrix is defined to be np.uint8 (an integer type). By default, cv2.normalize is going to return a result that is the same datatype as the inputs. You’ll want to use floating point datatypes if you want output values between 0.0 and 1.0.

One option would be to convert your input and outputs to np.double prior to calling cv2.normalize

JavaScript

Alternately, you can specify a floating point datatype to cv2.normalize via the dtype kwarg to force a specific output datatype.

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement