How to convert image from CV_8UC1 to CV_32FC1 in opencv?

I read an image which type is CV_8UC1 and I want to convert it to CV_32FC1.but when I use the convertTO () function my image becomes completely white and I don't know why!

Mat Image(512,512,CV_32FC1);     
Image = imread("C:\\MGC.jpg",CV_LOAD_IMAGE_GRAYSCALE);   

/// show image
namedWindow("pic");
int x = 0; int y = 0;
imshow("pic", Image);

cout<<Image.type()<<endl;
Image.convertTo(Image,CV_32FC1);
cout<<Image.type()<<endl;

////show converted image 
namedWindow("pic1");
imshow("pic1",Image );

      

+4


source to share


3 answers


Since the displayed range for images of elements of type 32FC is [0: 1] (for imshow). Try it.



Image.convertTo(Image,CV_32FC1, 1.0/255.0);

      

+5


source


The methods used by Andrew doesn't seem to exist in the current (v3.4) opencv for python. Here is an alternative solution that uses scikit image

http://scikit-image.org/docs/dev/user_guide/data_types.html



 import cv2
 from skimage import img_as_float

 cap = cv2.VideoCapture(0)

 ret, frame = cap.read()

 image1  = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)

 image2 = img_as_float(image1)

 cv2.imshow('IMAGE1',image1)
 cv2.imshow('IMAGE2', image2)

 while(1):
     k = cv2.waitKey(100) & 0xff
     if k == 27:
        break

 cap.release()
 cv2.destroyAllWindows()

      

0


source


This is a scaling problem. cvtColor

tricky in data type ranges. Read it here .

If you switch from CV_8U

to CV32F

and the ranges do not scale on their own, you must specify a scaling measure.

0


source







All Articles