www.gusucode.com > visionhdl 源码程序 matlab案例代码 > visionhdl/SelectARegionOfInterestExample.m

    %% Select Region of Interest
% Select a fixed region of interest (ROI) from an input frame.
%%
% Load a source image from a file. 
frmOrig = imread('coins.png');
[frmActiveLines,frmActivePixels] = size(frmOrig);
imshow(frmOrig)
title 'Input Image'
%%
% Create a serializer object and define inactive pixel regions. 
frm2pix = visionhdl.FrameToPixels(...
      'NumComponents',1,...
      'VideoFormat','custom',...
      'ActivePixelsPerLine',frmActivePixels,...
      'ActiveVideoLines',frmActiveLines,...
      'TotalPixelsPerLine',frmActivePixels+20,...
      'TotalVideoLines',frmActiveLines+20,...
      'StartingActiveLine',3,...     
      'FrontPorch',10);
%%
% Create an object to select a region of interest. Define a rectangular 
% region by the coordinates of the top-left corner and the dimensions.
hPos = 80;
vPos = 60;
hSize = 65;
vSize = 50;
roicoin = visionhdl.ROISelector('Regions',[hPos vPos hSize vSize])
%%
% Serialize the test image by calling |step| on the serializer object. |pixIn|
% is a vector of intensity values. |ctrlIn| is a vector of control signal 
% structures.
[pixIn,ctrlIn] = step(frm2pix,frmOrig);
%%
% Prepare to process pixels by preallocating output vectors. The output frame 
% is the same size as the input frame, but the control signals indicate a
% different active region.
[~,~,numPixelsPerFrame] = getparamfromfrm2pix(frm2pix);
pixOut = uint8(zeros(numPixelsPerFrame,1));
ctrlOut = repmat(pixelcontrolstruct,numPixelsPerFrame,1);
%%
% For each pixel in the padded frame, apply the region mask. 
for p = 1:numPixelsPerFrame  
    [pixOut(p),ctrlOut(p)] = step(roicoin,pixIn(p),ctrlIn(p));
end
%%
% Create a deserializer object with format matching the new region. Convert
% the pixel stream to an image frame by calling |step| on the deserializer 
% object. Display the resulting image. 
pix2frm = visionhdl.PixelsToFrame(...
      'NumComponents',1,...
      'VideoFormat','custom',...
      'ActivePixelsPerLine',hSize,...
      'ActiveVideoLines',vSize);
[frmOutput,frmValid] = step(pix2frm,pixOut,ctrlOut);
if frmValid
    figure
    imshow(frmOutput)
    title 'Output Image'
end