Query regarding colour thresholder and mask formed

1 view (last 30 days)
i have been trying to extraxt the number of pixels in the image that adhere to the hsv colour channel thresholds given by colour thresholder toolbox. The thresholds were obtained from the function generated by the toolbox itself and even then, according to my understanding, the number of pixels that pass the threshold is coming up as 0.
this is the image i used
this is what the colour thresholder looked like at the end
and this is the function that was generated:
function [BW,maskedRGBImage] = createMask(RGB)
%createMask Threshold RGB image using auto-generated code from colorThresholder app.
% [BW,MASKEDRGBIMAGE] = createMask(RGB) thresholds image RGB using
% auto-generated code from the colorThresholder App. The colorspace and
% minimum/maximum values for each channel of the colorspace were set in the
% App and result in a binary mask BW and a composite image maskedRGBImage,
% which shows the original RGB image values under the mask BW.
% Auto-generated by colorThresholder app on 25-May-2020
%------------------------------------------------------
% Convert RGB image to chosen color space
I = rgb2hsv(RGB);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.616;
channel1Max = 0.696;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 0.495;
channel2Max = 1.000;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 0.613;
channel3Max = 1.000;
% Create mask based on chosen histogram thresholds
sliderBW = (I(:,:,1) >= channel1Min ) & (I(:,:,1) <= channel1Max) & ...
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
BW = sliderBW;
% Initialize output masked image based on input image.
maskedRGBImage = RGB;
% Set background pixels where BW is false to zero.
maskedRGBImage(repmat(~BW,[1 1 3])) = 0;
end
As i did not want to use a function, this is how i tweaked the code inorder to count the number of pixels that pass the threshold:
EyeDetect = vision.CascadeObjectDetector('EyePairBig');
FaceDetect = vision.CascadeObjectDetector();
B= imread('C:\Users\0wner\Desktop\VIT\image processing\project\cmyk.jpg');
data= imsharpen(B,'Radius',1,'Amount',2);
%I=imread('C:\Users\0wner\Desktop\CBIR project\dataset\normal\000002.jpg');
% BB=step(FaceDetect,I);
%for j= 1:size(BB,1)
%face = imcrop(I, BB(j,:));
%end
BB = step(FaceDetect,data);
for j=1:size(BB,1)
I= imcrop(data, BB(j,:));
end
EE=step(EyeDetect,data);
for k= 1:size(EE,1)
eye = imcrop(data, EE(k,:));
end
new_eye_image= rgb2hsv(eye);
%imshow(new_eye_image);
ewp=0;
%perc_wrinkle=0;
%perc_fwrinkle=0;
%if cyan pixel found, pixel with eye wrinkle. calculate percentage of eye
%wrinkle
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.620;
channel1Max = 0.667;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 0.489;
channel2Max = 1.000;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 0.568;
channel3Max = 1.000;
[rows, columns, numberOfColorChannels] = size(new_eye_image);
for x=1:rows
for y=1:columns
pixel= new_eye_image(x,y);
if(((pixel(:,:,1) >= channel1Min ) && (pixel(:,:,1) <= channel1Max)) && ...
((pixel(:,:,2) >= channel2Min ) && (pixel(:,:,2) <= channel2Max)) && ...
((pixel(:,:,3) >= channel3Min ) && (pixel(:,:,3) <= channel3Max)))
ewp = ewp+1;
end
end
end
the values of ewp always turn up as zero. Even the maskedRGBImage comes out as a zero matrix. How is this possible?

Answers (1)

Image Analyst
Image Analyst on 25 May 2020
You forgot to give the third dimension when you got pixel. Try this:
for row=1:rows
for col=1:columns
pixel = new_eye_image(row, col, :);
if(((pixel(:,:,1) >= channel1Min ) && (pixel(:,:,1) <= channel1Max)) && ...
((pixel(:,:,2) >= channel2Min ) && (pixel(:,:,2) <= channel2Max)) && ...
((pixel(:,:,3) >= channel3Min ) && (pixel(:,:,3) <= channel3Max)))
ewp = ewp+1;
end
end
end
fprintf('ewp = %d\n', ewp);

Community Treasure Hunt

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

Start Hunting!