www.gusucode.com > visionhdl 源码程序 matlab案例代码 > visionhdl/ChromaResamplerSysObjExample.m
%% Downsample a Y'CbCr Image % Resample a 4:4:4 Y'CbCr image to 4:2:2. The example also shows % how to convert a R'G'B' input image to Y'CbCr color space. %% % Prepare a test image by selecting a portion of an image file. frmActivePixels = 64; frmActiveLines = 48; frmOrig = imread('fabric.png'); frmInput = frmOrig(1:frmActiveLines,1:frmActivePixels,:); %% % Create a serializer and specify the size of inactive pixel regions. The % number of padding pixels on each line must be greater than the latency % of each pixel-processing object. frm2pix = visionhdl.FrameToPixels(... 'NumComponents',3,... 'VideoFormat','custom',... 'ActivePixelsPerLine',frmActivePixels,... 'ActiveVideoLines',frmActiveLines,... 'TotalPixelsPerLine',frmActivePixels+40,... 'TotalVideoLines',frmActiveLines+10,... 'StartingActiveLine',6,... 'FrontPorch',5); %% % Create a color space converter and resampler, using the default property % values. The default conversion is 'RGB to YCbCr'. The default resampling % mode is '4:4:4 to 4:2:2'. The default anti-aliasing filter is a 29-tap % lowpass filter. This gives the object a latency of 30 cycles. convert2ycbcr = visionhdl.ColorSpaceConverter(); downsampler = visionhdl.ChromaResampler(); %% % Serialize the test image using the serializer object. |pixIn| is a % |numPixelsPerFrame| -by-3 matrix. |ctrlIn| is a vector of control signal % structures. Preallocate vectors for the output signals. [pixIn,ctrlIn] = step(frm2pix,frmInput); [~,~,numPixelsPerFrame] = getparamfromfrm2pix(frm2pix); pix444 = zeros(numPixelsPerFrame,3,'uint8'); ctrl444 = repmat(pixelcontrolstruct,numPixelsPerFrame,1); pix422 = zeros(numPixelsPerFrame,3,'uint8'); ctrl422 = repmat(pixelcontrolstruct,numPixelsPerFrame,1); %% % For each pixel in the stream, convert to YCbCr, then downsample. for p = 1:numPixelsPerFrame [pix444(p,:),ctrl444(p)] = step(convert2ycbcr,pixIn(p,:),ctrlIn(p)); [pix422(p,:),ctrl422(p)] = step(downsampler,pix444(p,:),ctrl444(p)); end %% % Create deserializers with a format matching that of the serializer. % Convert the 4:4:4 and 4:2:2 pixel streams back to image frames. pix2frm444 = visionhdl.PixelsToFrame(... 'NumComponents',3,... 'VideoFormat','custom',... 'ActivePixelsPerLine',frmActivePixels,... 'ActiveVideoLines',frmActiveLines); pix2frm422 = visionhdl.PixelsToFrame(... 'NumComponents',3,... 'VideoFormat','custom',... 'ActivePixelsPerLine',frmActivePixels,... 'ActiveVideoLines',frmActiveLines); [frm444,frmValid] = step(pix2frm444,pix444,ctrl444); [frm422,frmValid] = step(pix2frm422,pix422,ctrl422); %% % There are the same number of pixels in the 4:2:2 and 4:4:4 pixel-streams % and frames. To examine the resampled data, regroup the pixel data for the % first 8 pixels of the first line. The first row is the Y elements of the % pixels, the second row is the Cb elements, and the third row is the Cr % elements. Notice that, in the 4:2:2 data, the Cb and Cr elements change % only every second sample. YCbCr444 = [frm444(1,1:8,1); frm444(1,1:8,2); frm444(1,1:8,3)] YCbCr422 = [frm422(1,1:8,1); frm422(1,1:8,2); frm422(1,1:8,3)] figure imshow(frm422,'InitialMagnification',300) title '4:2:2' figure imshow(frm444,'InitialMagnification',300) title '4:4:4'