如何在 opencv 中将图像从 CV_8UC1 类型转换为 CV_32FC1 类型?

How to convert an image from CV_8UC1 to CV_32FC1 type in opencv?

我读了一张图像,它的类型是 CV_8UC1,我想将它转换为 CV_32FC1.but,当我使用 convert TO() 函数时,我的图像变成全白,但我没有知道为什么!

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 );

因为32FC类型元素图像的可显示范围是[0:1](对于imshow)。 试试这个。

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

Andrey 使用的方法似乎不存在于 python 的当前 (v3.4) opencv 中。 这是使用 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()

这是一个缩放问题。 cvtColor 在数据类型的范围内很棘手。阅读此 here

如果您从 CV_8U 更改为 CV32F 并且范围不会自行缩放,您必须指明缩放度量。