Showing posts with label video processing. Show all posts
Showing posts with label video processing. Show all posts

Sunday, 6 July 2014

Skin color detection using YCbCr plane

I=imread('filename');
I=double(I);
[hue,s,v]=rgb2hsv(I);
cb =  0.148* I(:,:,1) - 0.291* I(:,:,2) + 0.439 * I(:,:,3) + 128;
cr =  0.439 * I(:,:,1) - 0.368 * I(:,:,2) -0.071 * I(:,:,3) + 128;
[w h]=size(I(:,:,1));
for i=1:w
    for j=1:h            
        if  140<=cr(i,j) && cr(i,j)<=165 && 140<=cb(i,j) && cb(i,j)<=195 && 0.01<=hue(i,j) && hue(i,j)<=0.1     
            segment(i,j)=1;            
        else       
            segment(i,j)=0;    
        end    
    end
end
% imshow(segment);
im(:,:,1)=I(:,:,1).*segment;   
im(:,:,2)=I(:,:,2).*segment; 
im(:,:,3)=I(:,:,3).*segment; 
figure,imshow(uint8(im));

Thursday, 3 July 2014

how to detect nose in matlab


vid = imaq.VideoDevice('winvideo', 1, 'YUY2_640x480','ROI',[1 1 640 480]);
videoFrame = step(vid);
bbox            = step(faceDetector, videoFrame);
noseDetector = vision.CascadeObjectDetector('Nose');
faceImage    = imcrop(videoFrame,bbox(1,:));
noseBBox     = step(noseDetector,faceImage);
noseBBox(1,1:2) = noseBBox(1,1:2) + bbox(1,1:2);
tracker = vision.HistogramBasedTracker;
initializeObject(tracker, hueChannel, noseBBox(1,:));

% Create a video player object for displaying video frames.
ROI = get(vid,'ROI');
videoSize = [ROI(3) ROI(4)];
videoPlayer  = vision.VideoPlayer('Position',[300 300 videoSize(1:2)+30]);

To detect the upper body part of an image

bodyDetector = vision.CascadeObjectDetector('UpperBody');
bodyDetector.MinSize = [25 25];
%bodyDetector.MaxSize=[90 90];
bodyDetector.ScaleFactor=1.02;
I2 = imread('untitled.jpg');
bboxBody = step(bodyDetector, I2);
disp(bboxBody);
IBody = insertObjectAnnotation(I2, 'rectangle',bboxBody,'Upper Body');
figure(1);
imshow(IBody);
title('Detected upper bodies');



 bodyDetector = vision.CascadeObjectDetector('UpperBody');
 bodyDetector.MinSize = [60 60];
 bodyDetector.MergeThreshold = 10;
 I2 = imread('untitled.jpg');
 bboxBody = step(bodyDetector, I2);
 IBody = insertObjectAnnotation(I2, 'rectangle',bboxBody,'Upper Body');
 figure, imshow(IBody), title('Detected upper bodies');




Wednesday, 2 July 2014

detect face and mouth using viola jones technique in computer vision tool box

a=imread('img.jpg');
MouthDetect = vision.CascadeObjectDetector('Mouth','MergeThreshold',16);
BB=step(MouthDetect,a);
figure,
imshow(I); hold on
for i = 1:size(BB,1)
rectangle('Position',BB(i,:),'LineWidth',4,'LineStyle','-','EdgeColor','r');
end
title('Mouth Detection');
hold off;

Saturday, 14 June 2014

Convert Video to frames in matlab

The videoReader takes the input video. It creates a folder called snaps. If the directory is not there, it creates using mkdir
The frames are read now and stored in Frame. Later the file is taken and written into the output folder. The frame is then incremented.


clc;
close all;
clear all;
filename = 'rhinos.avi';
mov = VideoReader(filename);
opFolder = fullfile(cd, 'snaps');
if ~exist(opFolder, 'dir')
      mkdir(opFolder);
end
numFrames = mov.NumberOfFrames;
numFramesWritten = 0;
for t = 1 : numFrames
    Frame = read(mov, t);
    opBaseFileName = sprintf('%3.3d.png', t);
     opFullFileName = fullfile(opFolder, opBaseFileName);
     imwrite(Frame, opFullFileName, 'png');
     Indicate = sprintf('Wrote frame %4d of %d.', t, numFrames);
     disp(Indicate);
     numFramesWritten = numFramesWritten + 1;
end
Indicate = sprintf('Wrote %d frames to folder "%s"',numFramesWritten, opFolder);
disp(Indicate);

Extract frames from video



The following code takes input as a video reader and read the number of frames and writes to a directory.
and the output is a video of the frames which are sorted in certain time intervals.

workingDir = tempname;
mkdir(workingDir);
mkdir(workingDir,'images');
shuttleVideo = VideoReader('shuttle.avi');
for ii = 1:shuttleVideo.NumberOfFrames
    img = read(shuttleVideo,ii);
    imwrite(img,fullfile(workingDir,'images',sprintf('img%d.jpg',ii)));
end
imageNames = dir(fullfile(workingDir,'images','*.jpg'));
imageNames = {imageNames.name}';
disp(imageNames(1:10));
imageStrings = regexp([imageNames{:}],'(\d*)','match');
imageNumbers = str2double(imageStrings);
[~,sortedIndices] = sort(imageNumbers);
sortedImageNames = imageNames(sortedIndices);
disp(sortedImageNames(1:10));
outputVideo = VideoWriter(fullfile(workingDir,'shuttle_out.avi'));
outputVideo.FrameRate = shuttleVideo.FrameRate;
open(outputVideo);
for ii = 1:length(sortedImageNames)
    img = imread(fullfile(workingDir,'images',sortedImageNames{ii}));
    writeVideo(outputVideo,img);
end
close(outputVideo);
shuttleAvi = VideoReader(fullfile(workingDir,'shuttle_out.avi'));
mov(shuttleAvi.NumberOfFrames) = struct('cdata',[],'colormap',[]);
for ii = 1:shuttleAvi.NumberOfFrames
    mov(ii) = im2frame(read(shuttleAvi,ii));
end
set(gcf,'position', [150 150 shuttleAvi.Width shuttleAvi.Height])
set(gca,'units','pixels');
set(gca,'position',[0 0 shuttleAvi.Width shuttleAvi.Height])
image(mov(1).cdata,'Parent',gca);
axis off;
movie(mov,1,shuttleAvi.FrameRate);
displayEndOfDemoMessage(mfilename)







How to acquire video from matlab in real time and convert to frames

To get the information about devices

info=imaqhwinfo
info.InstalledAdaptors
info = imaqhwinfo('winvideo')
dev_info = imaqhwinfo('winvideo', 1)
info.DeviceInfo
info.DeviceInfo.SupportedFormats
%celldisp(dev_info.SupportedFormats);
imaqtool
How to acquire video from matlab in real time

vid=videoinput('winvideo',1);
preview(vid);
start(vid);
% foto=getsnapshot(vid);
%image(foto);
im=getdata(vid,1);figure,imshow(im);
imwrite(im,'testimage.jpg');


  • imaqhwinfo gives information about the existing adaptors for your webcam device
  • winvideo is one of the adaptors.
  • Further information pertaining to the device can be obtained by imaqhwinfo('winvideo',1) where 1 is the Device ID you saw earlier
  • The resolution (800×600, 1024×768, 1600×1200, etc.), format (RGB, YUV, etc.) which needs to be selected when creating a video object.
  • create a videoinput object
    ->vidobj = videoinput('winvideo',1,'RGB_1024x768');
  • to start your video object start(vidobj)
  • You can obtain snapshots of capture by using the frame = getsnapshot(vidobj)
  • view the continuous stream of frames by saying preview(vidobj);
  • A stop(vidobj) followed by delete(vidobj) is the best way to follow.
  • All the options can be seen by imaqhelp(videoinput).