How to separate and object and its background

1 view (last 30 days)
Getruda
Getruda on 13 May 2014
Answered: Image Analyst on 13 May 2014
Hello.
<<
>>
I have to identify and isolate an object from its background. I have tried this codes but doesnt give the desired results on the attached image. Any help?
close all; %Read Background Image Background=imread('C:\Users\Chando\Desktop\PROJECT\back\viptraffic_03.png');
%Read Current Frame CurrentFrame=imread('C:\Users\Chando\Desktop\PROJECT\back\viptraffic_02.png'); %Display Background and Foreground subplot(2,2,1);imshow(Background);title('BackGround'); subplot(2,2,2);imshow(CurrentFrame);title('CurrentFrame');
%Convert RGB 2 HSV Color conversion [Background_hsv]=round(rgb2hsv(Background)); [CurrentFrame_hsv]=round(rgb2hsv(CurrentFrame)); Out = bitxor(Background_hsv,CurrentFrame_hsv); %Convert RGB 2 GRAY Out=rgb2gray(Out);
%imshow(Out); %Read Rows and Columns of the Image [rows columns]=size(Out); %Convert to Binary Image for i=1:rows for j=1:columns if Out(i,j) >0 BinaryImage(i,j)=1; else BinaryImage(i,j)=0; end end end
%Apply Median filter to remove Noise FilteredImage=medfilt2(BinaryImage,[5 5]); %Boundary Label the Filtered Image [L num]=bwlabel(FilteredImage); STATS=regionprops(L,'all'); cc=[]; removed=0; %Remove the noisy regions for i=1:num dd=STATS(i).Area; if (dd < 500) L(L==i)=0; removed = removed + 1; num=num-1; else end end [L2 num2]=bwlabel(L); % Trace region boundaries in a binary image. [B,L,N,A] = bwboundaries(L2); %Display results subplot(2,2,3), imshow(L2);title('BackGround Detected');
subplot(2,2,8), imshow(L2);title('Blob Detected'); hold on; for k=1:length(B), if(~sum(A(k,:))) boundary = B{k}; plot(boundary(:,2), boundary(:,1),'r','LineWidth',2); for l=find(A(:,k)); boundary = B{l}; plot(boundary(:,2), boundary(:,1),'g','LineWidth',2); end end end

Answers (1)

Image Analyst
Image Analyst on 13 May 2014
The Computer Vision System Toolbox has code to do it. See the bottom of this page: http://www.mathworks.com/products/computer-vision/description4.html

Community Treasure Hunt

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

Start Hunting!