Normalizing RGB coordinates in an image

29 views (last 30 days)
I've tried to make a program to separate and normalize the colors of an image, and made the following code.
The intention is to make a binary image from the green one. But all my color-normalized images appear entirely black.
Would someone please help me figure out whats wrong?
%separating colors:
grassimg=imread('grass.jpg');
colorimgR=grassimg(:,:,1);
colorimgG=grassimg(:,:,2);
colorimgB=grassimg(:,:,3);
%All these images appeares in different kind of greyscales.
%Thresholding the "green" image:
thresh_green = colorimgG >100;
figure(2)
rez_threshg = imresize(thresh_green, 0.5);
imshow(rez_threshg)
%I get a quite ok binary image.
%Normalizing colors (this is what turns out wrong):
allcolors = colorimgR+colorimgG+colorimgB;
redimg = colorimgR./allcolors;
greenimg = colorimgG./allcolors;
blueimg = colorimgB./allcolors;
figure(3)
subplot(1,3,1);
imshow(redimg)
subplot(1,3,2);
imshow(greenimg)
subplot(1,3,3);
imshow(blueimg)
%All these single-colored images turnes out black
%Obviously it does'nt work to binarize a black image... :
threshGimg = greenimg > 100;
figure(4)
rez_greenimg = imresize(greenimg, 0.5);
imshow(rez_greenimg)

Accepted Answer

Stephen23
Stephen23 on 25 Jan 2021
Edited: Stephen23 on 25 Jan 2021
You did not take into account the integer class that you are using.
Most likely the image data is uint8, which supports values from 0 to 255. When you do this:
allcolors = colorimgR+colorimgG+colorimgB
allcolors will also have class uint8, but it is quite likely that many/most of the values saturate to 255 (or close to it) when you add them together. You then divide the individual color channels by 255 (or close to it) to get a very small uint8 value (either one or zero), which therefore appears very close to black:
uint8(198) / uint8(255)
ans = uint8 1
Note how the output is uint8 and its value is one, i.e. very very dark (when interpreted on a scale of 0..255).
I don't really understand how that "normalization" is supposed to work (it does not seem to take into account the sRGB color channel weighting or gamma correction, and if applied to floating point values would still darken them), but for a start you should probably convert the data to double before the addition and other operations.

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!