function RGB_Histogram_Demo()
if(~isdeployed)
cd(fileparts(which(mfilename)));
end
clc;
close all;
imtool close all;
workspace;
fontSize = 16;
try
imagesFolder = fullfile(matlabroot, '\toolbox\images\imdemos');
if ~exist(imagesFolder, 'dir')
message = sprintf('Please browse to your image folder');
button = questdlg(message, 'Specify Folder', 'OK', 'Cancel', 'OK');
drawnow;
if strcmpi(button, 'Cancel')
return;
else
imagesFolder = uigetdir();
if imagesFolder == 0
return;
end
end
end
filePattern = [imagesFolder, '\*.jpg'];
jpegFiles = dir(filePattern);
filePattern = [imagesFolder, '\*.tif'];
tifFiles = dir(filePattern);
filePattern = [imagesFolder, '\*.png'];
pngFiles = dir(filePattern);
filePattern = [imagesFolder, '\*.bmp'];
bmpFiles = dir(filePattern);
imageFiles = [jpegFiles; tifFiles; pngFiles; bmpFiles];
numberOfImagesProcessed = 0;
numberOfImagesToProcess = length(imageFiles);
if numberOfImagesToProcess <= 0
message = sprintf('I did not find any JPG, TIF, PNG, or BMP images in the folder\n%s\nClick OK to Exit.', imagesFolder);
uiwait(msgbox(message));
return;
end
figure;
set(gcf, 'Position', get(0,'Screensize'));
set(gcf,'name','Image Analysis Demo','numbertitle','off')
redChannel_Mean = zeros(numberOfImagesToProcess, 1);
greenChannel_Mean = zeros(numberOfImagesToProcess, 1);
blueChannel_Mean = zeros(numberOfImagesToProcess, 1);
numberOfImagesToProcess2 = numberOfImagesToProcess;
for k = 1 : numberOfImagesToProcess
baseFileName = imageFiles(k).name;
fullFileName = fullfile(imagesFolder, baseFileName);
rgbImage = imread(fullFileName);
if ndims(rgbImage) < 3
fprintf('Skipped %s. It is a grayscale or indexed image.\n', baseFileName);
numberOfImagesToProcess2 = numberOfImagesToProcess2 - 1;
continue;
end
subplot(3, 3, 1);
imshow(rgbImage, []);
[rows columns numberOfColorBands] = size(rgbImage);
caption = sprintf('Original Color Image\n%s\n%d rows by %d columns by %d color channels', ...
baseFileName, rows, columns, numberOfColorBands);
caption = strrep(caption, '_', ' ');
title(caption, 'FontSize', fontSize);
drawnow;
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
subplot(3, 3, 4);
imshow(redChannel, []);
redChannel_Mean(k) = mean(redChannel(:));
caption = sprintf('Red Image. Mean = %6.2f', redChannel_Mean(k));
title(caption, 'FontSize', fontSize);
pixelCountRed = PlotHistogramOfOneColorChannel(redChannel, 7, 'Histogram of Red Image', 'r');
subplot(3, 3, 5);
imshow(greenChannel, []);
greenChannel_Mean(k) = mean(greenChannel(:));
caption = sprintf('Green Image. Mean = %6.2f', greenChannel_Mean(k));
title(caption, 'FontSize', fontSize);
pixelCountGreen = PlotHistogramOfOneColorChannel(greenChannel, 8, 'Histogram of Green Image', 'g');
subplot(3, 3, 6);
imshow(blueChannel, []);
numberOfImagesProcessed = numberOfImagesProcessed + 1;
blueChannel_Mean(k) = mean(blueChannel(:));
caption = sprintf('Blue Image. Mean = %6.2f', blueChannel_Mean(k));
title(caption, 'FontSize', fontSize);
pixelCountBlue = PlotHistogramOfOneColorChannel(blueChannel, 9, 'Histogram of Blue Image', 'b');
subplot(3, 3, 2:3);
lineWidth = 2;
hold off;
plot(pixelCountRed, 'r', 'LineWidth', lineWidth);
hold on;
grid on;
plot(pixelCountGreen, 'g', 'LineWidth', lineWidth);
plot(pixelCountBlue, 'b', 'LineWidth', lineWidth);
title('All the Color Histograms (Superimposed)', 'FontSize', fontSize);
xlim([0 255]);
if k < numberOfImagesToProcess
promptMessage = sprintf('Currently displaying image #%d of a possible %d:\n%s\n\nDo you want to\nContinue processing, or\nCancel processing?',...
numberOfImagesProcessed, numberOfImagesToProcess2, baseFileName);
button = questdlg(promptMessage, 'Continue?', 'Continue', 'Cancel', 'Continue');
if strcmp(button, 'Cancel')
break;
end
end
end
redChannel_Mean = redChannel_Mean(1:numberOfImagesProcessed);
greenChannel_Mean = greenChannel_Mean(1:numberOfImagesProcessed);
blueChannel_Mean = blueChannel_Mean(1:numberOfImagesProcessed);
fprintf(1, ' Filename, Red Mean, Green Mean, Blue Mean\n');
for k = 1 : length(redChannel_Mean)
baseFileName = imageFiles(k).name;
fprintf(1, '%24s %6.2f, %6.2f, %6.2f\n', ...
baseFileName, redChannel_Mean(k), greenChannel_Mean(k), blueChannel_Mean(k));
end
if numberOfImagesProcessed == 1
caption = sprintf('Done with demo!\n\nProcessed 1 image.\nCheck out the command window for the results');
else
caption = sprintf('Done with demo!\n\nProcessed %d images.\nCheck out the command window for the results', numberOfImagesProcessed);
end
msgbox(caption);
catch ME
errorMessage = sprintf('Error in function RGB_Hist_Demo.\n.\n\nError Message:\n%s', ME.message);
uiwait(warndlg(errorMessage));
end
function pixelCount = PlotHistogramOfOneColorChannel(oneColorChannel, subplotNumber, caption, color)
try
[pixelCount grayLevels] = imhist(oneColorChannel, 256);
subplot(3, 3, subplotNumber);
bar(grayLevels, pixelCount, 'FaceColor', color);
title(caption, 'FontSize', 16);
grid on;
xlim([0 255]);
catch ME
errorMessage = sprintf('Error in function PlotHistogramOfOneColorChannel.\n.\n\nError Message:\n%s', ME.message);
uiwait(warndlg(errorMessage));
end
return;
Best Answer