Foreground detection and Blob detection

조회 수: 9 (최근 30일)
Han
Han 2018년 2월 22일
댓글: Han 2018년 2월 24일
Hi all ..
I'm trying to detect white objects only, but my code detect all objects
I don't know if I'm using foreground detection right or I sholud try something else !
Here is my code:
clc;
clear;
BackgroundImage = imread('frame1.jpg');
object = imread('frame2.jpg');
subplot(3,3,1);
imshow(BackgroundImage);
title('Background Frame 1');
subplot(3,3,2);
imshow(object);
title('Frame 2');
ga = rgb2gray(object);
BW = im2bw(ga);
subplot(3,3,3);
imshow(BW)
title('convert im2bw');
gb = rgb2gray(BackgroundImage);
foregroundDetector = vision.ForegroundDetector('InitialVariance',(30/255)^2);
foreground = step(foregroundDetector, gb);
subplot(3,3,4);
imshow(foreground);
title('foreground frame 1');
foreground1 = step(foregroundDetector, ga);
subplot(3,3,5);
imshow(foreground1);
title('foreground frame 2');
BlobAnalysis = vision.BlobAnalysis('MinimumBlobArea',100,'MaximumBlobArea',50000);
[area,centroid,bbox] = step(BlobAnalysis,foreground1);
Ishape = insertShape(object,'rectangle',bbox,'Color', 'green','Linewidth',6);
subplot(3,3,6);
imshow(Ishape);
title('Detect');
subplot(3,3,7);
title('Histogram');
[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 = object(y :(y+h),x:(x+w), :);
r = TestImage(:,:,1);
g = TestImage(:,:,2);
b = TestImage(:,:,3);
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])
thresholding =128;
rth=graythresh(TestImage(:,:,1))*255
gth=graythresh(TestImage(:,:,2))*255
bth=graythresh(TestImage(:,:,3))*255
if ( rth*gth*bth >= thresholding )
msgbox('Alarm it is white !');
load gong.mat;
while ( rth*gth*bth >= thresholding)%endless loop
sound(y);
lastTime = clock;
while etime(clock, lastTime) < 5
pause(0.03);
end
break;
end
else
msgbox('ok');
end
clear TestImage;
end
Here is the RESULT ! :
  댓글 수: 3
Han
Han 2018년 2월 23일
I attach the two images
I want to detect white object after using foreground detection (Background subtraction)
Thank you
Han
Han 2018년 2월 24일
Should I use something other than Foreground detection?
Can you please advise me?

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

채택된 답변

Image Analyst
Image Analyst 2018년 2월 23일
Try just comparing:
foregroundFrame2 = frame2 > backgroundFrame2;
  댓글 수: 4
Image Analyst
Image Analyst 2018년 2월 24일
Try this:
BackgroundImage = imread('frame1.jpg');
object = imread('frame2.jpg');
subplot(3,3,1);
imshow(BackgroundImage);
title('Background Frame 1');
subplot(3,3,2);
imshow(object);
title('Frame 2');
foregroundFrame2 = object > BackgroundImage;
if ndims(foregroundFrame2) > 1
% Convert from color to 2-D logical
foregroundFrame2 = max(foregroundFrame2, [], 3);
end
subplot(3,3,3);
imshow(foregroundFrame2);
title('foreground Frame 2');
Han
Han 2018년 2월 24일
Thank you so much

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

추가 답변 (0개)

Community Treasure Hunt

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

Start Hunting!

Translated by