簡體   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