How do I use this program to read the number of counting boxes and do something?

1 view (last 30 days)
like if # bounding box=1,
do task 1
like if bounding box=2,
do task 2
The program code I have used is:
clc;
clear all;
import java.awt.Robot;
import java.awt.event.*;
mouse = Robot;
mouse.mouseMove(0,0);
screenSize = get(0, 'screensize');
vid = videoinput('winvideo', 1,'YUY2_320X240');
set (vid, 'FramesPerTrigger',inf);
set (vid, 'ReturnedColorspace','rgb');
vid.FrameGrabInterval = 5;
start (vid);
while (vid.FramesAcquired <= 300)
data = getsnapshot(vid);
diff_imr = imsubtract (data(:,:,1), rgb2gray(data));
diff_imb = imsubtract (data(:,:,3), rgb2gray(data));
diff_imr = medfilt2(diff_imr, [3 3]);
diff_imb = medfilt2(diff_imb, [3 3]);
diff_imr = im2bw (diff_imr, 0.18);
diff_imb = im2bw (diff_imb, 0.18);
diff_imr = bwareaopen(diff_imr, 300);
diff_imb = bwareaopen(diff_imb, 300);
[bw , N] = bwlabel(diff_imr, 8);
[bw1 , N] = bwlabel(diff_imb, 8);
statsr = regionprops(bw , 'BoundingBox', 'Centroid');
statsb = regionprops(bw1 , 'BoundingBox', 'Centroid');
imshow (data)
%PopUpMenu;
hold on
for object = 1:length(statsr)
bb = statsr(object).BoundingBox;
bc = statsr(object).Centroid;
rectangle ('Position',bb,'Edgecolor','g','LineWidth',2);
plot(bc(1),bc(2),'-m+');
a = text(bc(1)+15 ,bc(2), strcat('X :',num2str(round(bc(1))),' Y:',num2str(round(bc(2)))));
set (a,'FontName','Arial','FontWeight','bold','FontSize',12,'Color','yellow');
mouse.mouseMove(bc(1),bc(2));
pause(0.00001);
%mouse.mousePress/Release(InputEvent.BUTTON1_MASK);
end
for object = 1:length(statsb)
bb1 = statsb(object).BoundingBox;
bc1 = statsb(object).Centroid;
rectangle ('Position',bb1,'Edgecolor','g','LineWidth',2);
plot(bc1(1),bc1(2),'-m+');
b = text(bc1(1)+15 ,bc1(2), strcat('X :',num2str(round(bc1(1))),' Y:',num2str(round(bc1(2)))));
set (b,'FontName','Arial','FontWeight','bold','FontSize',12,'Color','yellow');
mouse.mousePress(InputEvent.BUTTON1_MASK);
pause(0.00001);
mouse.mouseRelease(InputEvent.BUTTON1_MASK);
end
hold off;
end
stop(vid);
delete(vid)

Answers (1)

Image Analyst
Image Analyst on 3 Sep 2017
Don't use the same N for both images - they may be different. So have
[bw , N] = bwlabel(diff_imr, 8);
[bw1 , N1] = bwlabel(diff_imb, 8);
Now N and N1 are the number of bounding boxes. So you can just do
if N == 1
% Do task 1
elseif N ==2
% Do task 2
elseif N == 3
% Do something else.
end
Repeat for N1 if you need to.

Community Treasure Hunt

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

Start Hunting!