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 Center および File ExchangeComputer Vision with Simulink についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by