Making one histogram with multiple images
Afficher commentaires plus anciens
Hello!
I need to make histograms of varying conditions. I have 10 images that I would like to make a single histogram out of. The code I have been working with makes three histograms for every image. I would like to add on to this code by changing it so I can "stack" the images and make the two histograms for every 10 images. Is that possible? Here is the code I've been working with.
function [im2]=flhisto(numloops);
for i=1:numloops
global im
[filename, pathname]=uigetfile( {'*.tif';'*.jpg';'*.jpeg';'*.gif';'*.png';'*.bmp'},'Select file');
MyImage = strcat(pathname, filename);
%This code checks if the user pressed cancel on the dialog.
if isequal(filename,0) || isequal(pathname,0)
uiwait(msgbox ('User pressed cancel','failed','modal') )
hold on;
else
hold off;
uiwait(msgbox('User selected image sucessfully','sucess','modal'));
end
im=imread(MyImage);
im2=im2double(im); %converts to double
%for backup process :)
%imshow(im);
subhisto=im;
subhisto(subhisto <= 120) = NaN;
figure(1)
subplot(3,2,i); imhist(im,4096);
axis([0 11000 0 1000000])
figure(1)
subplot(3,2,i+2); imhist(subhisto,4096);
axis([1500 12000 0 400])
figure(1)
subplot(3,2,i+4); imhist(subhisto,4096);
axis([1500 18000 0 50])
I=imadjust(im);
figure
imshow(I)
end
Réponses (1)
Image Analyst
le 22 Fév 2020
Try this demo to take the histogram of the original image, the masked image, and the contrast adjusted image. You're doing all three of those things. Adapt as needed.
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 = 18;
% Specify the folder where the files live.
% Specify standard MATLAB demo image folder.
myFolder = fileparts(which('cameraman.tif')); % Determine where demo folder is (works with all versions).
% myFolder = pwd; % or 'C:\Users\yourUserName\Documents\My Pictures'; or whatever.
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.jpg') % Change to whatever pattern you need.
% Get a file listing of files in myFolder from the operating system:
theFiles = dir(filePattern);
hFig = figure;
hFig.WindowState = 'maximized';
% Now loop over all found files, renaming each in turn with the new desired extension:
for k = 1 : length(theFiles)
% Get the input filename.
baseFileName = theFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
% Read in the image
theImage = imread(fullFileName);
% Convert to gra scale if needed.
if ndims(theImage) == 3
theImage = rgb2gray(theImage);
end
subplot(2, 3, 1);
imshow(theImage, []);
title('Original Image', 'FontSize', fontSize);
drawnow;
% Get the histogram.
subplot(2, 3, 4);
imhist(theImage);
grid on;
drawnow;
title('Original Image', 'FontSize', fontSize);
% Threshold it at 120. Mask it so that less than 120 shows up as black.
thresholdedImage = theImage;
thresholdedImage(theImage < 120) = 0;
subplot(2, 3, 2);
imshow(thresholdedImage, []);
title('Thresholded Image', 'FontSize', fontSize);
drawnow;
% Get the histogram.
subplot(2, 3, 5);
imhist(thresholdedImage);
grid on;
drawnow;
title('Histogram of Thresholded Image', 'FontSize', fontSize);
% Do contrast adjustmente with imadjust().
adjustedImage = imadjust(theImage);
subplot(2, 3, 3);
imshow(thresholdedImage, []);
title('Contrast Adjusted Image', 'FontSize', fontSize);
drawnow;
% Get the histogram.
subplot(2, 3, 6);
imhist(adjustedImage);
grid on;
drawnow;
title('Histogram of Contrast Adjusted Image', 'FontSize', fontSize);
promptMessage = sprintf('Do you want to Continue processing,\nor Quit processing?');
titleBarCaption = 'Continue?';
buttonText = questdlg(promptMessage, titleBarCaption, 'Continue', 'Quit', 'Continue');
if contains(buttonText, 'Quit', 'IgnoreCase', true)
return;
end
end

6 commentaires
Manuella Juwita
le 8 Août 2020
Dear Image Analyst,
I'm sorry if this is a stupid question. I tried the code you posted here for 3000 images, and my understanding is that the final plot shown(after MATLAB is done running) is like the average histogram of all of my images. Is my understanding correct?
Image Analyst
le 8 Août 2020
No, it would be the counts for the last image. If you want all of them then you're going to have to accumulate your counts into a summation variable, like
theseCounts = imhist(adjustedImage);
allCounts = allCounts + theseCounts; % Add counts for this image into the running total.
Manuella Juwita
le 9 Août 2020
Okay, Thank you very much for the reply
diederik sonneveld
le 20 Mar 2021
Modifié(e) : diederik sonneveld
le 20 Mar 2021
Hi there, im new to matlab and have some little experience with python. Thank you for this piece of code it has really helped me out. But I also want to get the avarage histogram of my images besides the individual histograms but when I use the summation varable i get a fault prompt:
Unrecognized function or variable 'allCounts'
It is probably something really simple byt how do solve this that the avarage histogram of my images is shown.
Image Analyst
le 20 Mar 2021
You can probably just define it as a vector of 256 before the loop starts:
allCounts = zeros(1, 256);
diederik sonneveld
le 22 Mar 2021
Thank you very much!
Catégories
En savoir plus sur Convert Image Type dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!