Clear Filters
Clear Filters

Graph plotting of number of red pixels vs number of images?

1 view (last 30 days)
I would like to count the number of red pixels for every image in this folder. Next, plot the graph of no. of red pixels for each image vs the no. of images? How can this be done?
[Merged from duplicate question]
I would like to calculate the red pixel count for every image in this folder and then plot a graph of red pixel count vs the number of images. The red pixel count must correspond to the image no. This is my code.
%for loop
clc;
clear;
close all;
fontSize=10;
myFolder='G:\FYP2\Time Frames\Frame 24';
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
filePattern = fullfile(myFolder, '*.png');
theFiles = dir(filePattern);
numberOfImages=length(theFiles);
red_counts = zeros(1, numberOfImages);
redCount=0;
for k=1:numberOfImages
fullFileName = fullfile(myFolder, theFiles(k).name);
thisImage=double(imread(fullFileName));
[rows, columns, numberOfColorBands] = size(thisImage);
redBand=thisImage(:,:,1);
%THRESHOLD LEVELS
redThresholdLow=215;
redThresholdHigh=255;
redMask=(redBand>=redThresholdLow) & (redBand<=redThresholdHigh);
%Count pixels
redCount=sum(redMask(:)>0);
red_counts(k)=redCount;
end
plot(red_counts, numberOfImages, '-r*');
set(gca, 'xTickLabel', 1:1:numberOfImages);
ylabel('Red pixel count');
xlabel('Number of images');

Accepted Answer

Walter Roberson
Walter Roberson on 13 Jan 2016
Everything is written for you in http://uk.mathworks.com/matlabcentral/answers/263333-how-to-plot-an-intensity-graph#comment_334704 (you will have to view old comments there to see the code again.)
The key line is
meets_red_threshold = thisimage(:,:,1) > 180 & thisimage(:,:,2) < 60 & thisimage(:,:,3) < 60; %strong red compared to G or B ??
You need to alter this line to match your definition of what a "red pixel" is. Is a pixel "red" if it has any non-zero R component and the G and B components are 0? Visually you would find it difficult to distinguish such a pixel from black. If you plot [0.8 0.01 0.01] on the same line graph as [1 0 0] you are going to have trouble telling the two apart. So you need to define exactly what "red" means to you, as there is no scientific definition of what is "red" and what is not.
  3 Comments

Sign in to comment.

More Answers (0)

Categories

Find more on Graph and Network Algorithms in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!