Blob analysis and thresholding

조회 수: 2 (최근 30일)
Han
Han 2018년 2월 6일
댓글: Image Analyst 2018년 2월 6일
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 !

채택된 답변

Image Analyst
Image Analyst 2018년 2월 6일
Make sure bbox is integers. Call round() if necessary. Often the bounding box is a half pixel outside of the pixel centers.
  댓글 수: 2
Han
Han 2018년 2월 6일
bbox =
2×4 int32 matrix
201 67 168 150
436 293 147 119
Image Analyst
Image Analyst 2018년 2월 6일
x is NOT the first index. It is row, which is y, not x. To correct:
TestImage = OriginalImage(y :(y+h), x:(x+w), :);

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Computer Vision with Simulink에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by