MATLAB: Blob analysis and thresholding

blob analysiscolor histogramComputer Vision Toolboxthreshold

I have two white objects I detected by blob analysis the message should be "Alarm it is white !"
% Blob Analysis
BlobAnalysis = vision.BlobAnalysis('Connectivity',8);
[area,centroid,bbox] = step(BlobAnalysis,BW);
% Box
Ishape = insertShape(OriginalImage,'rectangle',bbox,'Linewidth',3);
subplot(2,2,2);
imshow(Ishape);
[row , col ] = size (bbox);
for i =1 : row
x = bbox(i,1);
y =bbox(i,2);
w=bbox(i,3);
h=bbox(i,4);
TestImage = OriginalImage(x:(x+w), y :(y+h),:);
r = TestImage(:,:,1);
g = TestImage(:,:,2);
b = TestImage(:,:,3);
subplot(2,2,3)
imshow(TestImage);
histogram2(r,g,'DisplayStyle','tile','ShowEmptyBins','on', ...
'XBinLimits',[0 255],'YBinLimits',[0 255]);
histogram(r,'BinMethod','integers','FaceColor','r','EdgeAlpha',0,'FaceAlpha',1)
hold on
histogram(g,'BinMethod','integers','FaceColor','g','EdgeAlpha',0,'FaceAlpha',0.7)
histogram(b,'BinMethod','integers','FaceColor','b','EdgeAlpha',0,'FaceAlpha',0.7)
xlabel('RGB value')
ylabel('Frequency')
title('Color Histogram')
xlim([0 257])
threshold =128;
a = r.*g .*b;
c = 128.*128.*128;
if a >= c
msgbox('Alarm it is white !');
else
msgbox('ok');
end
clear TestImage;
end
I have a threshold which is equal to 128 if the color histogram for each object is greater than threshold the the message "Alarm .." is displayed !!
I think my mistake is here I don't know how can I do it:
threshold =128;
a = r.*g .*b;
c = 128.*128.*128;
if a >= c
msgbox('Alarm it is white !');
else
msgbox('ok');
end
Also I get some errors:
Index exceeds matrix dimensions.
Error in Tracking(line 25)
TestImage = OriginalImage(x:(x+w), y :(y+h),:);
THANK YOU !

Best Answer

Make sure bbox is integers. Call round() if necessary. Often the bounding box is a half pixel outside of the pixel centers.