我正在尝试将灰度图像转换为CV64F类型。从OpenCv文档中我了解到灰度图像的类型为CV_8U。我还发现imshow以不同的方式绘制不同的类型,因此我需要在转换之前除以255。但转换图像后,我仍然会得到很多饱和像素。
我正在使用此图片,保存为jpg: http://www.ele.uri.edu/~hansenj/projects/ele585/lab2/cameraman.gif
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <cstring>
#include <cmath>
int main()
{
Mat I, input_image;
string path = "C:/<your_path>/camera_man.jpg";
input_image = imread(path.c_str(), 0); // Read the file as grayscale
imshow("Original", input_image);
// Convert image to CV_64F
input_image *= (double) 1 / 255;
input_image.convertTo(I, CV_64F);
imshow("Converted", I);
}
答案 0 :(得分:5)
执行此操作时:
input_image *= (double) 1 / 255; // (1)
input_image.convertTo(I, CV_64F); // (2)
您将CV_8UC1
矩阵中的每个值除以(1)中的255,因此每个像素将为:
new_value = static_cast<uchar>(old_value / 255)
因此new_value
0
只能包含0 <= old_value < 255
,1
只能包含old_value = 255
。然后在(2)中对截断值应用转换。
因此,您需要先转换为CV_64FC1
,然后再划分:
input_image.convertTo(I, CV_64F);
I *= (double)1 / 255;
或在转换过程中直接应用缩放:
input_image.convertTo(I, CV_64F, 1.0 / 255.0);