Sunday, February 21, 2010

Video Spatial Transformation - Cropping


To extract a rectangular portion of an image, use the imcrop function. imcrop accepts two primary arguments:
  • The image to be cropped
  • The coordinates of a rectangle that defines the crop area
If you call imcrop without specifying the crop rectangle, you can specify the crop rectangle interactively.
In this case, the cursor changes to crosshairs when it is over the image. Position the crosshairs over a corner of the crop region and press and hold the left mouse button. When you drag the crosshairs over the image you specify the rectangular crop region. imcrop draws a rectangle
around the area you are selecting. When you release the mouse button, imcrop creates a new image from the selected region.
In this example, you display an image and call imcrop. The imcrop function displays the image in a figure window and waits for you to draw the cropping rectangle on the image. In the figure, the rectangle you select is shown in red.

The example then calls imshow to view the cropped image.
imshow circuit.tif
I = imcrop;
imshow(I);
In Video:

fin = 'rawVideo.avi'; fout = 'test.avi'; fileinfo = aviinfo(fin); nframes = fileinfo.NumFrames; aviobj = avifile(fout, 'compression', 'none', 'fps',fileinfo.FramesPerSecond); for i = 1:20 %Read frames from input video mov_in = aviread(fin,i); im_in = frame2im(mov_in); %Do processing on each frame of the video
%----------------------------------------------------------------------
%In this example - Image Cropping
im_out = imcrop(im_in,[60 40 100 90]);
%----------------------------------------------------------------------
%Write frames to output video frm = im2frame(im_out); aviobj = addframe(aviobj,frm); %i %Just to display frame number end; %Don't forget to close output file aviobj = close(aviobj); msgbox('DONE'); return;

Result:

No comments:

Post a Comment