Monday, 26 January 2015

fore ground and background subtraction

clear all;
close all;
clc;
threshold = 0.05;
file = 'Video.avi';
vfr = vision.VideoFileReader(file,'ImageColorSpace','RGB');
%vfr = vision.VideoFileReader(file, 'ImageColorSpace', 'gray');
imagebox = step(vfr);
backgnd = imagebox;
g_img = rgb2gray(backgnd);
imagesize = size(backgnd);
width = imagesize(2);
height = imagesize(1);
foregnd = zeros(height, width);
while ~isDone(vfr)
   imagebox1 = step(vfr);
   foregndgray= rgb2gray(imagebox1);
   f_gnddiff= abs(double(foregndgray) - double(g_img));
  for i=1:width
for j=1:height
if ((foregnddiff(j,i) > threshold))
     foregnd(j,i) = 255;
else
     foregnd(j,i) = 0;    
end
end
end
g_img = foregndgray;
figure(1),subplot(3,1,1),imshow(imagebox1)
subplot(3,1,2),imshow(foregndgray)
subplot(3,1,3),imshow(uint8(foregnd))

end

locate points in an image

NumberOfimages=4;       %chose the number of images you want to give input
srcFiles = dir('E:\frames12\*.png');  
for i = 1 : NumberOfimages
filename = strcat('E:\frames12\',srcFiles(i).name);
 I = imread(filename);  
 faceDetector = vision.CascadeObjectDetector();
fbox= step(faceDetector,I);
videoOutFace = insertObjectAnnotation(I,'rectangle',fbox,'Face');
%figure, imshow(videoOutFace), title('Detected face');
e=imcrop(I,fbox);
%figure,imshow(e);
imwrite(e,strcat('E:\frames13\',int2str(i),'.png'));
 imwrite(e,'acquired.jpg');
  a=imread('acquired.jpg');
I=rgb2gray(a);
%cornerDetector = vision.CornerDetector('Method','Minimum eigenvalue (Shi & Tomasi)','MaximumCornerCount',120);I = rgb2gray(a);
cornerDetector = vision.CornerDetector('Method','Local intensity comparison (Rosten & Drummond)','MaximumCornerCount',250);
pts = step(cornerDetector, I);
drawMarkers = vision.MarkerInserter('Shape','Plus','BorderColor','Custom','Size',2);
J = repmat(I,[1 1 3]);
J = step(drawMarkers, J, pts);
%imshow(J); title ('Corners detected in a grayscale image');
%imwrite(image,FullFileName,'png');
    imwrite(J,strcat('E:\frames12\frames\',int2str(i),'.png'));
end