Foreground detection and Blob detection

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 commentaires

Image Analyst
Image Analyst le 22 Fév 2018
You forgot to attach 'frame1.jpg'. Please do so and then we can help.
Han
Han le 23 Fév 2018
I attach the two images
I want to detect white object after using foreground detection (Background subtraction)
Thank you
Han
Han le 24 Fév 2018
Should I use something other than Foreground detection?
Can you please advise me?

Connectez-vous pour commenter.

 Réponse acceptée

Image Analyst
Image Analyst le 23 Fév 2018
Try just comparing:
foregroundFrame2 = frame2 > backgroundFrame2;

4 commentaires

Han
Han le 23 Fév 2018
Thank you for your answer but sorry I didn't understand
See the images posted above.
Thank you again
Shounak Shastri
Shounak Shastri le 24 Fév 2018
@Han
Image Analyst's code is correct. You would get a logical (Binary) image which highlights the white area in frame2.jpg.
Also, your images are not grayscale images, they are coloured. So you might want to add a rgb2gray before using this code as you have done in your original program.
Here's the explanation of the code:
You have, overall, 3 gray levels in your images. One is the gray which is the background in both images; the second is the white which you want to detect; and the third is the black which you don't want to detect.
The way the gray levels work is that black represents a "0" and white represents "255" for a uint8 (8-bit) image. ImageAnalyst's code creates a new image called foregroundFrame2 which contains all the pixels whose pixel values are greater than the gray in your background image marked as "1" and rest marked as "0". This separates the white block which you wanted.
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 le 24 Fév 2018
Thank you so much

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by