rgb图像的标准化

时间:2015-05-22 13:45:11

标签: image matlab image-processing

我试图去除两个图像中的照明变化。我的一个方法是:

    1) There are two images im1 and im2. Extract the R G B content of im1.
    2)Calculate the normalized value for each content
    3)Reform a color image.
    4)Repeat the process for im2
    5)Compare each pixel and replace the content of im2 with im1.

Image_rgb = imread('aswathy_33_crop.jpg');
Image_rgb = double(Image_rgb);
figure;imshow(uint8(Image_rgb));

Image_red = Image_rgb(:,:,1);
Image_green = Image_rgb(:,:,2);
Image_blue = Image_rgb(:,:,3);


[row,col] = size(Image_rgb(:,:,1));

for y = 1:row 
for x = 1:col 
   Red = Image_red(y,x);
   Green = Image_green(y,x);
   Blue = Image_blue(y,x);


    if(Red == 0 && Green==0 && Blue==0)
        Red = 1;
        Green = 1;
        Blue = 1;
    end

      NormalizedRed = Red/(Red + Green + Blue);
      NormalizedGreen = Green/(Red + Green + Blue);
      NormalizedBlue = Blue/(Red + Green + Blue);

 Image_red(y,x) = NormalizedRed;
 Image_green(y,x) = NormalizedGreen;
 Image_blue(y,x) = NormalizedBlue;
   end
end
Image_rgb(:,:,1) = Image_red;
Image_rgb(:,:,2) = Image_green;
Image_rgb(:,:,3) = Image_blue;

new_image1 = cat(3, Image_rgb(:,:,1) ,Image_rgb(:,:,2), Image_rgb(:,:,3));

figure; imshow(uint8(255*new_image1));

这只是单个图像的标准化,最终会因完全扭曲的图像而崩溃。任何人都可以建议我出错的地方以及我对此问题的处理方法是否正确?

enter image description here

INPUT1

enter image description here

输入2

1 个答案:

答案 0 :(得分:0)

您的代码看起来不错。但有两件事。最后的错误是拼写unit8也应该是uint8,因为图像是规范化的,值应该在0和1之间,所以你应该将它们乘以255

figure; imshow(uint8(255*new_image1));

我也不确定你从哪里得到归一化方程式。像素值应为

  rgb_sum = Red + Green + Blue;
  NormalizedRed = Red/rgb_sum;
  NormalizedGreen = Green/rgb_sum;
  NormalizedBlue = Blue/rgb_sum;

因为在每个像素的标准化空间R + B + G = 1中。运行这个简单的代码,我们可以看到使用sqrt(平方和)不遵守规范化

%sums the normalizes R,G,B values for all pixels
flat = sum(new_image1,3);
min(flat(:))
max(flat(:))

对于真正的标准化RGB图像,min和max都应为1(+ - 舍入误差),这意味着所有颜色分量对所有元素加起来为1。对于每个像素,使用带有sqrt的代码并不总和为

修改

现在我更好地理解了你的问题,你试图比较两个图像(红色图像和灰色图像)并使它们尽可能相似。我不认为在RGB色彩空间中有一种简单的方法可以做到这一点。一种替代方案是YCbCr色彩空间。像RGB一样,它有3个通道,它们是 Luminance (灰度图像)红色差异蓝色差异你会看到亮度通道几乎相同两个图像。真正的区别在于颜色通道。下面是一些示例代码,向您展示YCbCrSpace的外观

sidebyside = [red_im,grey_im]; 

%converts to new colorspace
YUVsidebyside = rgb2ycbcr(sidebyside);

%display results
figure();
subplot(2,2,1);imshow(sidebyside);title('original images side by side');
subplot(2,2,2);imshow(YUVsidebyside(:,:,1));title('Luminance channel');
subplot(2,2,3);imshow(YUVsidebyside(:,:,2));title('Blue Difference channel');
subplot(2,2,4);imshow(YUVsidebyside(:,:,3));title('Red Differrence channel');

enter image description here