calculation of histogram only from the foreground

조회 수: 9 (최근 30일)
Malini Bakthavatchalam
Malini Bakthavatchalam 2020년 6월 12일
댓글: Walter Roberson 2021년 12월 29일
Hi,
I am stuck with this idea and code for the past three months, I have tried n number of codes but all was a failure. I would like to calculate the histogram only from the foreground image, and I have to split the histogram into two one below the middle value and one part above the middle value, all this has to be done without including the background. I am attaching my image with my code please do help with the code. I have also asked in this forum in different ways, but none of the help worked in my project, so I requesting to have a correction of my code.

답변 (1개)

Image Analyst
Image Analyst 2020년 6월 13일
Three months, oh my gosh. Well here's something I whipped together in about 15 minutes that does what I think you asked for. First it finds masks for the face and background. Then, for each color channel, it computes the mean and median. Then it masks the image by the values above and below the median and computes the histogram for each masked image.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clearvars;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
fprintf('Beginning to run %s.m ...\n', mfilename);
%-----------------------------------------------------------------------------------------------------------------------------------
% Read in image.
folder = pwd;
baseFileName = 'originalimg.jpg';
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(rgbImage);
% Display the RGB image full size.
subplot(4, 4, 1);
imshow(rgbImage, []);
axis('on', 'image');
caption = sprintf('Original Image : "%s"', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Set up figure properties:
% Enlarge figure to full screen.
hFig1 = gcf;
hFig1.Units = 'Normalized';
hFig1.WindowState = 'maximized';
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
hFig1.Name = 'Demo by Image Analyst';
% Threshold the image to find the background.
[backgroundMask, maskedRGBImage] = createMask(rgbImage);
% Take the largest 4 blobs only.
backgroundMask = bwareafilt(backgroundMask, 4);
% Fill holes
backgroundMask = imfill(backgroundMask, 'holes');
% Display the binary image.
subplot(4, 4, 2);
imshow(backgroundMask, []);
axis('on', 'image');
caption = sprintf('Background Mask Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Get the face mask
faceMask = imfill(~backgroundMask, 'holes');
% Display the binary image.
subplot(4, 4, 3);
imshow(faceMask, []);
axis('on', 'image');
caption = sprintf('Face Mask Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Extract the individual red, green, and blue color channels using imsplit() (introduced in R2018b).
[redChannel, greenChannel, blueChannel] = imsplit(rgbImage);
% Display the color images along the left edge of the figure.
% Display the red image.
subplot(4, 4, 5);
imshow(redChannel, []);
axis('on', 'image');
caption = sprintf('Red Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Display the green image.
subplot(4, 4, 9);
imshow(greenChannel, []);
axis('on', 'image');
caption = sprintf('Green Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Display the blue image.
subplot(4, 4, 13);
imshow(blueChannel, []);
axis('on', 'image');
caption = sprintf('Blue Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Get the histogram of each channel.
redCounts = imhist(redChannel);
greenCounts = imhist(greenChannel);
blueCounts = imhist(blueChannel);
% Display the red histogram.
subplot(4, 4, 6);
bar(redCounts);
title('Red Histogram', 'FontSize', fontSize);
xlabel('Red Gray Level', 'FontSize', fontSize);
ylabel('Counts', 'FontSize', fontSize);
grid on;
% Find the mean and median and plot the mean in red and the median in blue.
aveR = mean(redChannel(faceMask))
medR = median(redChannel(faceMask))
xline(aveR, 'Color', 'r', 'LineWidth', 2);
xline(double(medR), 'Color', 'm', 'LineWidth', 2);
% Display the green histogram.
subplot(4, 4, 10);
bar(greenCounts);
title('Green Histogram', 'FontSize', fontSize);
xlabel('Green Gray Level', 'FontSize', fontSize);
ylabel('Counts', 'FontSize', fontSize);
grid on;
% Find the mean and median and plot the mean in red and the median in blue.
aveG = mean(greenChannel(faceMask))
medG = median(greenChannel(faceMask))
xline(aveG, 'Color', 'r', 'LineWidth', 2);
xline(double(medG), 'Color', 'b', 'LineWidth', 2); % xline() cannot take integers for some bizarre reason so must cast it to double.
% Display the blue histogram.
subplot(4, 4, 14);
bar(blueCounts);
title('Blue Histogram', 'FontSize', fontSize);
xlabel('Blue Gray Level', 'FontSize', fontSize);
ylabel('Counts', 'FontSize', fontSize);
grid on;
% Find the mean and median and plot the mean in red and the median in blue.
aveB = mean(blueChannel(faceMask))
medB = median(blueChannel(faceMask))
xline(aveB, 'Color', 'r', 'LineWidth', 2);
xline(double(medB), 'Color', 'b', 'LineWidth', 2);
% Get the masks for each color above and below the median.
brightMaskR = (redChannel > medR) & faceMask;
brightMaskG = (greenChannel > medG) & faceMask;
brightMaskB = (blueChannel > medB) & faceMask;
darkMaskR = (redChannel <= medR) & faceMask;
darkMaskG = (greenChannel <= medG) & faceMask;
darkMaskB = (blueChannel <= medB) & faceMask;
% Mask the gray scale images.
% Mask the image using bsxfun() function to multiply the mask by each channel individually. Works for gray scale as well as RGB Color images.
brightR = bsxfun(@times, redChannel, cast(brightMaskR, 'like', redChannel));
brightG = bsxfun(@times, greenChannel, cast(brightMaskG, 'like', greenChannel));
brightB = bsxfun(@times, blueChannel, cast(brightMaskB, 'like', blueChannel));
darkR = bsxfun(@times, redChannel, cast(darkMaskR, 'like', redChannel));
darkG = bsxfun(@times, greenChannel, cast(darkMaskG, 'like', greenChannel));
darkB = bsxfun(@times, blueChannel, cast(darkMaskB, 'like', blueChannel));
% Display the the bright and dark mask images.
% Display bright and dark Red images.
subplot(4, 4, 7);
imshow(brightR);
axis('on', 'image');
caption = sprintf('Red Bright Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
subplot(4, 4, 8);
imshow(darkR);
axis('on', 'image');
caption = sprintf('Red Dark Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Display bright and dark Green images.
subplot(4, 4, 11);
imshow(brightG);
axis('on', 'image');
caption = sprintf('Green Bright Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
subplot(4, 4, 12);
imshow(darkG);
axis('on', 'image');
caption = sprintf('Green Dark Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Display bright and dark Blue images.
subplot(4, 4, 15);
imshow(brightB);
axis('on', 'image');
caption = sprintf('Blue Bright Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
subplot(4, 4, 16);
imshow(darkB);
axis('on', 'image');
caption = sprintf('Blue Dark Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Get the histogram above and below the median values for each color channel
upperHistR = imhist(redChannel(brightMaskR));
lowerHistR = imhist(redChannel(darkMaskR));
upperHistG = imhist(redChannel(brightMaskG));
lowerHistG = imhist(redChannel(darkMaskG));
upperHistB = imhist(redChannel(brightMaskB));
lowerHistB = imhist(redChannel(darkMaskB));
% % Measure the colors
% propsR = regionprops('table', backgroundMask, redChannel, 'MeanIntensity')
% propsG = regionprops('table', backgroundMask,greenChannel, 'MeanIntensity')
% propsB = regionprops('table', backgroundMask, blueChannel, 'MeanIntensity')
function [BW,maskedRGBImage] = createMask(RGB)
%createMask Threshold RGB image using auto-generated code from colorThresholder app.
% [BW,MASKEDRGBIMAGE] = createMask(RGB) thresholds image RGB using
% auto-generated code from the colorThresholder app. The colorspace and
% range for each channel of the colorspace were set within the app. The
% segmentation mask is returned in BW, and a composite of the mask and
% original RGB images is returned in maskedRGBImage.
% Auto-generated by colorThresholder app on 13-Jun-2020
%------------------------------------------------------
% Convert RGB image to chosen color space
I = rgb2hsv(RGB);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.000;
channel1Max = 1.000;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 0.000;
channel2Max = 0.026;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 0.918;
channel3Max = 1.000;
% Create mask based on chosen histogram thresholds
sliderBW = (I(:,:,1) >= channel1Min ) & (I(:,:,1) <= channel1Max) & ...
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
BW = sliderBW;
% Initialize output masked image based on input image.
maskedRGBImage = RGB;
% Set background pixels where BW is false to zero.
maskedRGBImage(repmat(~BW,[1 1 3])) = 0;
end
  댓글 수: 14
mahdi shajie
mahdi shajie 2021년 12월 29일
very bad and longly description
please express short and usefull!!!!
Walter Roberson
Walter Roberson 2021년 12월 29일
The description that was given was,
First it finds masks for the face and background. Then, for each color channel,
it computes the mean and median. Then it masks the image by the values above
and below the median and computes the histogram for each masked image.
And you are saying that is too long ??

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Get Started with Image Processing Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by