我正在尝试将灰度图像转换为 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);
}
最佳答案
当你这样做时:
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);
关于c++ - OpenCV 将 CV_8U 转换为 CV_64F,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33299374/