www.gusucode.com > visionhdl 源码程序 matlab案例代码 > visionhdl/EdgeDetectorSysObjExample.m
%% Edge Detection Using Sobel Method % Detect edges in a thumbnail image using the Sobel method. %% % Prepare a test image by selecting a portion of an image file. frmActivePixels = 64; frmActiveLines = 48; frmOrig = imread('rice.png'); frmInput = frmOrig(1:frmActiveLines,1:frmActivePixels); figure imshow(frmInput,'InitialMagnification',300) title 'Input Image' %% % Create a serializer and specify the size of inactive pixel regions. frm2pix = visionhdl.FrameToPixels(... 'NumComponents',1,... 'VideoFormat','custom',... 'ActivePixelsPerLine',frmActivePixels,... 'ActiveVideoLines',frmActiveLines,... 'TotalPixelsPerLine',frmActivePixels+10,... 'TotalVideoLines',frmActiveLines+10,... 'StartingActiveLine',6,... 'FrontPorch',5); %% % Create an edge detection object with the default property values. The % default detection method is Sobel. edgeDetectSobel = visionhdl.EdgeDetector(); %% % Serialize the test image using the object you created. |pixIn| is a vector % of intensity values. |ctrlIn| is a vector of control signal structures. % Preallocate vectors for the output signals. [pixIn,ctrlIn] = step(frm2pix,frmInput); [~,~,numPixelsPerFrame] = getparamfromfrm2pix(frm2pix); ctrlOut = repmat(pixelcontrolstruct,numPixelsPerFrame,1); edgeOut = false(numPixelsPerFrame,1); %% % For each pixel in the stream, compute whether it represents an edge. for p = 1:numPixelsPerFrame [edgeOut(p),ctrlOut(p)] = step(edgeDetectSobel,pixIn(p),ctrlIn(p)); end %% % Create a deserializer with a format matching that of the serializer. Use % the deserializer to convert the output pixel stream to an image frame. pix2frm = visionhdl.PixelsToFrame(... 'NumComponents',1,... 'VideoFormat','custom',... 'ActivePixelsPerLine',frmActivePixels,... 'ActiveVideoLines',frmActiveLines); [frmOutput,frmValid] = step(pix2frm,edgeOut,ctrlOut); if frmValid figure imshow(frmOutput, 'InitialMagnification',300) title 'Output Image' end