Error in code: while trying to plot average histogram

조회 수: 1 (최근 30일)
Vaswati Biswas
Vaswati Biswas 2021년 3월 15일
댓글: Vaswati Biswas 2021년 3월 16일
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?

채택된 답변

Image Analyst
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
Image Analyst 2021년 3월 16일
You can easily create a smoothed signal with movmean() or sgolayfilt()
allPixelCounts = sgolayfilt(allPixelCounts, 2, 9);
Vaswati Biswas
Vaswati Biswas 2021년 3월 16일
It works! thanks

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by