Matlab code for formula of RGB image to Blue Ratio image conversion

4 views (last 30 days)
Hello i want matlab code for the formula of RGB image to Blue Ratio image. Formula is :
100*B/1+R+G * 256/1+R+G+B
i use the following code...Kindly check is there any problem in it.
img=imread('image.jpg');
R= img(:,:,1);
G=img(:,:,2);
B=img(:,:,3);
img_BlueRation=((100 * B)./(1+R+G)) .* (256./(1+B+R+G));
Regards

Accepted Answer

Image Analyst
Image Analyst on 22 Aug 2014
Not quite right. You forgot to cast to double to avoid clipping of uint8 numbers to 255. See corrected code below:
rgbImage = imread('peppers.png');
% Extract individual color channels and cast to double to avoid clipping.
R = double(rgbImage(:,:,1));
G = double(rgbImage(:,:,2));
B = double(rgbImage(:,:,3));
blueRatio = uint8(((100 * B)./(1+R+G)) .* (256./(1+B+R+G)));
% Display images
fontSize = 28;
subplot(2, 3, 1);
imshow(rgbImage);
title('Original RGB Image', 'FontSize', fontSize);
subplot(2, 3, 2);
imshow(uint8(R)); % Must be uint8 for display
title('R Image', 'FontSize', fontSize);
subplot(2, 3, 3);
imshow(uint8(G)); % Must be uint8 for display
title('G Image', 'FontSize', fontSize);
subplot(2, 3, 4);
imshow(uint8(B)); % Must be uint8 for display
title('B Image', 'FontSize', fontSize);
subplot(2, 3, 5);
imshow(blueRatio);
title('Blue Ratio Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
  2 Comments
Tahir
Tahir on 22 Aug 2014
Edited: Tahir on 22 Aug 2014
Thank you ... I also want to know that Why we convert it to double??...and you said that for display you have to convert it to unit8...i don't want to display.i want to apply Laplaicina of Guassian on this image....so kindly tell me what i will do to apply LOG code on the blueRation image?? Best Regards.
Image Analyst
Image Analyst on 22 Aug 2014
Let's say you just take the values as they come from the file - most likely uint8. A uint8 200 plus 200 will not give 400. It will give 255. That's because it will clip the sum to the max value a uint8 can have, which is 2^8-1 or 255. So cast to double to avoid this clipping. For display, if you have a floating point image, it expects it to be in the range of 0-1. 0 will go to 0 and 1 will display as 255 - the max your display can do. If it's not in the range 0-1, it will clip everything above 1 to 1 and below 0 to 0. So something that has values like 200 or 400 will get clipped to 1 and show up as white. You can cast to uint8 and then it will clip things over 255 to 255, so the 200 would display fine, but the 400 would get clipped to, and display as 255. Or you can have it scale everything so that it shows everything (no clipping) if you use [] and don't cast to uint8:
imshow(floatingPointImage, []); % The [] allows it to auto-scale to 0-255.

Sign in to comment.

More Answers (0)

Categories

Find more on Convert Image Type in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!