Reputation: 7
I have some pictures in my path.
My program has to count how many of the pictures that has red color.
I start a looping but I'm confused in how to detect whether the picture has red color or not.
Here is my loop:
sdirectory = 'E:\SEMESTER 7\Computer Vision\Tugas\UAS - Pencocokan Objek';
namafile = dir([sdirectory '/*.jpg']);
jml_obj = []
for k = 1:length(namafile)
filename = [sdirectory '/' namafile(k).name];
img = imread(filename);
red = img(:,:,1);
if(size(img,3)==3) %i think here is the key
jml_obj=[jml_obj 1]
else
jml_obj=[jml_obj 0]
end
end
jml_obj;
jumlah=sum(jml_obj);
textLabel1= sprintf('Jumlah %i ',jumlah);
set(handles.jml, 'String', textLabel1);
Upvotes: 0
Views: 181
Reputation: 745
If you are reading in colored images, then all images will have red, green, and blue channels, so size(img,3)
will always be equal to 3. You actually have to check the pixel contents of each image to figure out if it has red.
But before that, you have to define what it means for a pixel to be red. Are you talking about approximately red, or pure red like RGB(255,0,0)? Once you have a definition of red, you can loop over the pixels of each image and check if at least one pixel satisfies your definition. If it does, then you can increment your counter.
Upvotes: 1