matlab - Motion vectors calculation -
i working on following code:
filename = 'c:\li_walk.avi'; hvidreader = vision.videofilereader(filename, 'imagecolorspace', 'rgb','videooutputdatatype', 'single'); hopticalflow = vision.opticalflow('outputvalue', 'horizontal , vertical components in complex form', 'referenceframedelay', 3); hmean1 = vision.mean; hmean2 = vision.mean('runningmean', true); hmedianfilt = vision.medianfilter; hclose = vision.morphologicalclose('neighborhood', strel('line',5,45)); hblob = vision.blobanalysis('centroidoutputport', false, 'areaoutputport', true, 'boundingboxoutputport', true, 'outputdatatype', 'double','minimumblobarea', 250, 'maximumblobarea', 3600, 'maximumcount', 80); herode = vision.morphologicalerode('neighborhood', strel('square',2)); hshapeins1 = vision.shapeinserter('bordercolor', 'custom', 'custombordercolor', [0 1 0]); hshapeins2 = vision.shapeinserter( 'shape','lines', 'bordercolor', 'custom','custombordercolor', [255 255 0]); htextins = vision.textinserter('text', '%4d', 'location', [1 1],'color', [1 1 1], 'fontsize', 12); sz = get(0,'screensize'); pos = [20 sz(4)-300 200 200]; hvideo1 = vision.videoplayer('name','original video','position',pos); pos(1) = pos(1)+220; % move next viewer right hvideo2 = vision.videoplayer('name','motion vector','position',pos); pos(1) = pos(1)+220; hvideo3 = vision.videoplayer('name','thresholded video','position',pos); pos(1) = pos(1)+220; hvideo4 = vision.videoplayer('name','results','position',pos); % initialize variables used in plotting motion vectors. linerow = 22; firsttime = true; motionvecgain = 20; borderoffset = 5; decimfactorrow = 5; decimfactorcol = 5; while ~isdone(hvidreader) % stop when end of file reached frame = step(hvidreader); % read input video frame grayframe = rgb2gray(frame); ofvectors = step(hopticalflow, grayframe); % estimate optical flow % optical flow vectors stored complex numbers. compute % magnitude squared later used thresholding. y1 = ofvectors .* conj(ofvectors); % compute velocity threshold matrix of complex velocities. vel_th = 0.5 * step(hmean2, step(hmean1, y1)); % threshold image , filter remove speckle noise. segmentedobjects = step(hmedianfilt, y1 >= vel_th); % thin-out parts of road , fill holes in blobs. segmentedobjects = step(hclose, step(herode, segmentedobjects)); % estimate area , bounding box of blobs. [area, bbox] = step(hblob, segmentedobjects); % select boxes inside roi (below white line). idx = bbox(:,1) > linerow; % based on blob sizes, filter out objects can not cars. % when ratio between area of blob , area of % bounding box above 0.4 (40%), classify car. ratio = zeros(length(idx), 1); ratio(idx) = single(area(idx,1))./single(bbox(idx,3).*bbox(idx,4)); ratiob = ratio > 0.4; count = int32(sum(ratiob)); % number of cars bbox(~ratiob, :) = int32(-1); % draw bounding boxes around tracked cars. y2 = step(hshapeins1, frame, bbox); % display number of cars tracked , white line showing roi. y2(22:23,:,:) = 1; % white line. y2(1:15,1:30,:) = 0; % background displaying count result = step(htextins, y2, count); % generate coordinates plotting motion vectors. if firsttime [r c] = size(ofvectors); % height , width in pixels rv = borderoffset:decimfactorrow:(r-borderoffset); cv = borderoffset:decimfactorcol:(c-borderoffset); [y x] = meshgrid(cv,rv); firsttime = false; sumu=0; sumv=0; end grayframe = rgb2gray(frame); [ra ca na] = size(grayframe); ofvectors = step(hopticalflow, grayframe); % estimate optical flow ua = real(ofvectors); ia = ofvectors - ua; va = ia/complex(0,1); sumu=ua+sumu; sumv=va+sumv; [xa ya]=meshgrid(1:1:ca,ra:-1:1); % calculate , draw motion vectors. tmp = ofvectors(rv,cv) .* motionvecgain; lines = [y(:), x(:), y(:) + real(tmp(:)), x(:) + imag(tmp(:))]; motionvectors = step(hshapeins2, frame, lines); % display results step(hvideo1, frame); % original video step(hvideo2, motionvectors); % video motion vectors step(hvideo3, segmentedobjects); % thresholded video step(hvideo4, result); % video bounding boxes quiver(xa,ya,sumu,sumv) end release(hvidreader);
please me understand following statements of above code:
ua = real(ofvectors); ia = ofvectors - ua; va = ia/complex(0,1);
these horizontal (ua) , vertical (va) components of motion vectors. real part of (ofvectors) be? please me in understanding code segment
when object hopticalflow
constructed in third line of code, outputvalue
property set 'horizontal , vertical components in complex form'
has effect when apply step
command hopticalflow
, image (frame), not magnitudes of flowvectors, complex numbers represent these planar flow vectors. compact way command return information. once have complex numbers in ofvectors
, output of step
command, command
ua = real(ofvectors);
stores horizontal component of each vector in ua
. after command
ia = ofvectors - ua;
is executed, ia
contains imaginary (i.e., vertical components of flow vectors) because real parts in ua
subtracted complex numbers in ofvectors
. however, need rid of imaginary units in ia
, divide 0+1i
. command
va = ia/complex(0,1);
does.
Comments
Post a Comment