【问题标题】:OpenCV convert CV_8U to CV_64FOpenCV 将 CV_8U 转换为 CV_64F
【发布时间】:2023-03-05 22:27:02
【问题描述】:

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

}

【问题讨论】:

    标签: c++ opencv


    【解决方案1】:

    当你这样做时:

    input_image *= (double) 1 / 255;   // (1)
    input_image.convertTo(I, CV_64F);  // (2)
    

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

    new_value = static_cast<uchar>(old_value / 255)
    

    这样new_value 只能有值0 对应0 &lt;= old_value &lt; 2551 对应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);
    

    【讨论】:

      猜你喜欢
      • 2018-02-25
      • 2018-09-04
      • 2017-07-16
      • 2015-12-13
      • 2021-06-29
      • 2014-05-19
      • 2017-02-19
      • 2018-11-28
      相关资源
      最近更新 更多