'Normalization of rgb images

I am trying to remove the variation of illumination in two images. One of my approach is:

    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));

This is just normalization of single image, which at the end crashes with a completely distorted image.Can anyone suggest where I am going wrong and whether my approach to this issue is right or not?

enter image description here

input1

enter image description here

input2



Solution 1:[1]

Your code looks good. but two a few things. The error at the end is a typo unit8 should be uint8 also, because the image is normalized, the values should be between 0 and 1, so you should probably multiply them by 255

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

I'm also not sure where you got your normalizing equations from. The pixel values should be

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

because in the normalized space R + B + G = 1 for every pixel. Running this simple code we can see that using the sqrt(sum of squares) doesn't obey the normalization

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

For a true normalized RGB image, both min and max should be 1 (+- rounding errors) meaning all the color components add up to 1 for all elements. Using your code with the sqrt does not sum to 1 for every pixel

EDIT

Now that I better understand your question, you are trying to compare the two images (the red one and the grey one) and make them as similar as possible. I dont think there is a easy way to do this in RGB colorspace. One alternative is the YCbCr colorspace. Like RGB it has 3 channels they are Luminance (greyscale image)red difference and blue difference you will see that the luminance channel is nearly the same for both images. The real differences are in the color channels. Here is some sample code to show you what the YCbCrSpace looks like

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1