OpenCV将CV_8U转换为CV_64F

Ang*_*los 3 c++ opencv

我正在尝试将灰度图像转换为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);

}
Run Code Online (Sandbox Code Playgroud)

Mik*_*iki 5

当你这样做:

input_image *= (double) 1 / 255;   // (1)
input_image.convertTo(I, CV_64F);  // (2)
Run Code Online (Sandbox Code Playgroud)

您将CV_8UC1矩阵中的每个值除以(1)中的255,因此每个像素将为:

new_value = static_cast<uchar>(old_value / 255)
Run Code Online (Sandbox Code Playgroud)

所以new_value只能有0for 0 <= old_value < 2551for的值old_value = 255.然后在(2)中对截断值应用转换.

所以,你需要先转换为CV_64FC1然后除以:

input_image.convertTo(I, CV_64F);
I *= (double)1 / 255;
Run Code Online (Sandbox Code Playgroud)

或在转换过程中直接应用缩放:

input_image.convertTo(I, CV_64F, 1.0 / 255.0);
Run Code Online (Sandbox Code Playgroud)