Making one histogram with multiple images
24 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
Image Analyst
2020-2-22
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 个评论
Image Analyst
2021-3-20
You can probably just define it as a vector of 256 before the loop starts:
allCounts = zeros(1, 256);
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!