Filter löschen
Filter löschen

Error in code: while trying to plot average histogram

3 Ansichten (letzte 30 Tage)
Vaswati Biswas
Vaswati Biswas am 15 Mär. 2021
Kommentiert: Vaswati Biswas am 16 Mär. 2021
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?

Akzeptierte Antwort

Image Analyst
Image Analyst am 15 Mär. 2021
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 Kommentare
Image Analyst
Image Analyst am 16 Mär. 2021
You can easily create a smoothed signal with movmean() or sgolayfilt()
allPixelCounts = sgolayfilt(allPixelCounts, 2, 9);
Vaswati Biswas
Vaswati Biswas am 16 Mär. 2021
It works! thanks

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Graphics Performance finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by