繁体   English   中英

OpenCV将CV_8U转换为CV_64F

[英]OpenCV convert CV_8U to CV_64F

我正在尝试将灰度图像转换为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只能有值00 <= old_value < 255 ,和1old_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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM