opencv和matlab之间的像素值不同

时间:2015-06-25 08:33:18

标签: matlab opencv image-processing pixel

当我使用matlab中的“显示”功能显示图像的像素值(转换为灰度的RGB图像)时,我发现像素值小于1(所有值都在0和1之间)。而当我在opencv中做同样的事情时,我的价值也越来越高。为什么价值观会发生变化? 打开CV代码和matlab代码如下:

for (int i = 0; i < img1.rows; i++)
{
    for (int j = 0; j < img1.cols; j++)
    {
        cout << (unsigned int)img1.at<uchar>(i, j) << endl;
    }
}

Matlab code:

gI=rgb2gray(I);
imshow(gI);

2 个答案:

答案 0 :(得分:1)

Sorry to disappoint you. No one guarantees that the conversion of RGB to gray-scale will yield the same result. There are 2 reasons

  1. The conversion is not a mathematical correct formula but rather subjective issue. Over the course of the last 50 years a few different standards (mainly for color television supporting greyscale signals) were written of how to convert RGB to gray level. Different standards lead to almost identical visible results, though the actual values of the pixels differ a bit (~roughly 0.2%). You can read more about the standards in Wikipedia. For example: One standard defines the conversion of RGB to gray while another defines conversion of RGB to YUV and taking Y channel as the gray scale. There are few other standards as well (preserving luminosity, intensity, contrast, etc). Since the preference of the standards change over time, it is hard to know how exactly the RGB to gray is implemented. Here is an example of 3 different methods. Open CV uses completely different method (0.299f*RED + 0.587f*GREEN + 0.114f*BLUE Here) while Matlab uses another method (0.2989*RED + 0.587*GREEN + 0.1140*BLUE. Here). Note the difference between openCV 0.299 and Matlabs 0.2989. More information can be found in this answer (here)
  2. Second reason - different floating point representation across hardware and software. Floating point computation (regardless whether it is double or float) is not precise and depends on the specific hardware you use, type of compiler with witch you build a software and specific compiler directives. Same mathematical calculation can yield slightly different results on different computers or even on the same computer in 2 different programs. So unfortunately you will not get exactly the same results for RGB to gray conversion but if you round the values to range of [0..255] you will typically get a discrepancy of no more than 2 levels. I would say that in 99.9% pixels you will get the same value and in the others difference of 1. 2 is very rare and I have never seen a difference of 3, unless you do few sequential conversions like RGB->XYZ->HSV->YUV

答案 1 :(得分:1)

  

我的垫子是8UC3型。如何转换归一化的double值   MATLAB到8UC3?仅乘以255就足够了吗?


8UC3字节的情况下,然后足以乘以255.它们被设计为以这种方式工作。 8UC 3 中的 3 表示三个频道红色绿色蓝色 < / p>

无论如何,在0 to 255范围值和0.0 to 1.0范围值之间进行转换

//Psuedo-code formula:

col_val = 210; //range 0-255
normalised = col_val / 255; //always divide by 255 for a normalised to "1.0" version

normal_reverse_to255 = normalised * 255; //make it back into "0 to 255" range

预期结果:
col_val = 210 //original value before normalised
标准化 = 0.8235 //...294117647058 etc etc //after normalised to 0.0 to 1.0 range
normal_reverse_to255 = 210; //multiply result is same as original value

希望这对你有所帮助。

编辑:
我刚刚意识到你正在使用gI=rgb2gray(I);来获取灰度图像。

尝试类似的事情:
cvtColor(src,dst,CV_GRAY2RGB);this answer

劫持latitude_new = atan(sin(latitude)/fabs(cos(latitude)))
相关问题