How can I create a luminosity histogram of an image without the green background?

3 views (last 30 days)
This is an image like the ones I will be trying to apply this with. The section I'm trying to make the histogram for is always going to be grayscale.
This is what I tried but it creates a histogram that is obviously wrong:
rgbImage2 = imread('Screenshot 2022-01-18 150342._SubCu1#01-25-2022 16-08.bmp');
redChannel2 = rgbImage2(:, :, 1);
greenChannel2 = rgbImage2(:, :, 2);
blueChannel2 = rgbImage2(:, :, 3);
binaryRed = redChannel2 == 0;
binaryGreen = greenChannel2 == 255;
mask = binaryRed & binaryGreen;
isnan(mask);
histogram(binaryImage(~mask), 50)
%histogramArea = ~mask;
%hist(histogramArea)
Would appreciate any help, Thanks!

Accepted Answer

Image Analyst
Image Analyst on 28 Jan 2022
Try this:
rgbImage = imread('image.bmp');
subplot(2, 2, 1);
imshow(rgbImage);
impixelinfo;
title('Original RGB Image')
% Get green mask
[r,g,b] = imsplit(rgbImage);
mask = r == 0 & g == 255 & b == 0;
subplot(2, 2, 2);
imshow(mask);
impixelinfo;
title('Green Mask Image')
% Get "luminosity image"
labImage = rgb2lab(rgbImage);
luminosityImage = labImage(:, :, 1);
subplot(2, 2, 3);
imshow(luminosityImage, []);
impixelinfo;
title('Luminosity Image')
% Get the histogram where the mask is false (the hole in the green mask);
edges = 0 : 256;
pixelCounts = histcounts(luminosityImage(~mask), edges);
subplot(2, 2, 4);
bar(edges(1:end-1), pixelCounts);
grid on;
title('Luminosity Histogram of Masked Image')
  7 Comments
Image Analyst
Image Analyst on 28 Jan 2022
If you'd rather use
% Get "luminosity image"
luminosityImage = rgb2gray(labImage);
instead of
% Get "luminosity image"
labImage = rgb2lab(rgbImage);
luminosityImage = labImage(:, :, 1);
then you can do that. It gives you values in gray levels according to a weighted sum of the separate color channels, instead of a "percent reflectance" like rgb2lab() gives.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!