Error in code: while trying to plot average histogram
2 次查看(过去 30 天)
显示 更早的评论
I have a folder in that folder I have some images and I want to plot a average histogram of that images. My code is given below:
myFolder = 'folder path';
filePattern = fullfile(myFolder, '*.jpg') ;
theFiles = dir(filePattern);
S=0;
for k = 1 : length(theFiles)
% Get the input filename.
baseFileName = theFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
% Read in the image
I = imread(fullFileName);
raw = im2double(I(:,:,1));
maxv=max(max(raw));
minv=min(min(raw));
theImage= (raw-minv)./(maxv-minv);
[N,edges]=histcounts(theImage,nbins); %nbins= 512
for i=1:length(N)
S(i,:)=S(i,:)+N; % the accumlator
S(i,:)=S(i,:)/length(theFiles);
S1=S(i,:);
end
end
in=linspace(0,1,512);
plot(in,S1,'linewidth',2);
But I am getting a error :
"Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 1-by-512.
S(i,:)=S(i,:)+N; % the accumlator "
How to solve this error can anyone help? what modification do I need to make in the code to get rid of this error?
0 个评论
采纳的回答
Image Analyst
2021-3-15
Try this:
% Computes average histogram of all images in a folder.
% Demo by Image Analyst, March, 2021.
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);
myFolder = pwd;
filePattern = fullfile(myFolder, '*.jpg') ;
theFiles = dir(filePattern);
numberOfBins = 256;
numberOfFiles = length(theFiles);
allPixelCounts = zeros(1, numberOfBins);
for k = 1 : numberOfFiles
% Get the input filename.
baseFileName = theFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
% Read in the image
theImage = imread(fullFileName);
% Look at red channel, or entire image (if it's already grayscale).
theImage = im2double(theImage(:,:,1));
% Display image
subplot(2, 1, 1);
imshow(theImage, []);
drawnow;
caption = sprintf('#%d of %d : %s', k, numberOfFiles, baseFileName);
title(caption, 'Interpreter', 'none');
% Get the histogram.
[thesePixelCounts, edges] = histcounts(theImage,numberOfBins); %nbins= 512
allPixelCounts = allPixelCounts + thesePixelCounts;
subplot(2, 1, 2);
bar(allPixelCounts, 1);
grid on;
drawnow;
end
subplot(2, 1, 2);
% Get average
allPixelCounts = allPixelCounts / numberOfFiles;
bar(allPixelCounts, 1);
grid on;
title('Average Histogram');
xlabel('Gray Level');
ylabel('Pixel Count');
grayLevel = 0 : (numberOfBins - 1);
% Optional. Plot lines from tip of bar to tip of bar.
% hold on;
% plot(grayLevel, allPixelCounts, 'r.-', 'LineWidth', 1);
% hold off;
fprintf('Done running %s.m\n', mfilename);
3 个评论
Image Analyst
2021-3-16
You can easily create a smoothed signal with movmean() or sgolayfilt()
allPixelCounts = sgolayfilt(allPixelCounts, 2, 9);
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Performance 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!