Foreground detection and Blob detection
15 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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
Réponse acceptée
Image Analyst
le 23 Fév 2018
Try just comparing:
foregroundFrame2 = frame2 > backgroundFrame2;
4 commentaires
Image Analyst
le 24 Fév 2018
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');
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Image Preview and Device Configuration dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!