Senin, 27 Juni 2011 | By: ilin.blogspot.com

OPTICAL FLOW DI MATLAB

dari hasil pacaran ama source code selama seminggu ternyata membuahkan hasil juga, xixixixi...

code:

clear all
clc
hbfr = video.MultimediaFileReader('tugas.mp4');
hcr = video.ChromaResampler('Resampling', '4:2:0 (MPEG1) to 4:4:4', 'InterpolationFilter', 'Pixel replication');
hcsc1 = video.ColorSpaceConverter('Conversion', 'YCbCr to RGB');
hcsc2 = video.ColorSpaceConverter('Conversion', 'RGB to intensity');
hidtc = video.ImageDataTypeConverter('OutputDataType', 'single');

hof = video.OpticalFlow('OutputValue', 'Horizontal and vertical components in complex form', 'ReferenceFrameDelay', 3);

hmean1 = video.Mean;
hmean2 = video.Mean('RunningMean', true);
hmedianfilt = video.MedianFilter2D;
hclose = video.MorphologicalClose('Neighborhood', strel('line',5,45));
hblob = video.BlobAnalysis(...
'CentroidOutputPort', false,...
'AreaOutputPort', true, ...
'BoundingBoxOutputPort', true, ...
'OutputDataType', 'double', ...
'NumBlobsOutputPort', false, ...
'MinimumBlobAreaSource', 'Property', ...
'MinimumBlobArea', 250, ...
'MaximumBlobAreaSource', 'Property', ...
'MaximumBlobArea', 3600, ...
'FillValues', -1, ...
'MaximumCount', 80);

herode = video.MorphologicalErode('Neighborhood', strel('square',2));

hshapeins1 = video.ShapeInserter('BorderColor', 'Custom', 'CustomBorderColor', [0 1 0]);
hshapeins2 = video.ShapeInserter('Shape','Lines', 'BorderColor', 'Custom', 'CustomBorderColor', [255 255 0]);

htextins = video.TextInserter('Text', '%4d', 'Location', [0 0], 'Color', [1 1 1], 'Font', 'Arial', 'FontSize', 12);

hVideo1 = video.VideoPlayer('WindowCaption', 'Original Video');
hVideo1.WindowPosition(1) = round(0.4*hVideo1.WindowPosition(1)) ;
hVideo1.WindowPosition(2) = round(1.5*(hVideo1.WindowPosition(2))) ;
hVideo1.WindowPosition([4 3]) = [300 300];

hVideo2 = video.VideoPlayer('WindowCaption', 'Motion Vector');
hVideo2.WindowPosition(1) = hVideo1.WindowPosition(1) + 350;
hVideo2.WindowPosition(2) =round(1.5* hVideo2.WindowPosition(2));
hVideo2.WindowPosition([4 3]) = [300 300];

hVideo3 = video.VideoPlayer('WindowCaption', 'Thresholded Video');
hVideo3.WindowPosition(1) = hVideo2.WindowPosition(1) + 350;
hVideo3.WindowPosition(2) = round(1.5*(hVideo3.WindowPosition(2))) ;
hVideo3.WindowPosition([4 3]) = [300 300];

hVideo4 = video.VideoPlayer('WindowCaption', 'Results');
hVideo4.WindowPosition(1) = hVideo1.WindowPosition(1);
hVideo4.WindowPosition(2) = round(0.3*(hVideo4.WindowPosition(2))) ;
hVideo4.WindowPosition([4 3]) = [300 300];

% inisialisasi beberapa variabel digunakan untuk plot motion vektor
MotionVecGain = 20;
line_row = 22;
borderOffset = 5;
decimFactorRow = 5;
decimFactorCol = 5;
firstTime = true;


while ~isDone(hbfr)
%[y, cb, cr] = step(hbfr);

my_rgb_frame = step(hbfr); % Read input video frame
my_rgb_frame = step(hbfr); % Read input video frame
image = step(hidtc, my_rgb_frame);
I = step(hcsc2, image);
of = step(hof, I); % Estimate optical flow

y1 = of .* conj(of);
vel_th = 0.5 * step(hmean2, step(hmean1, y1));

filteredout = step(hmedianfilt, y1 >= vel_th);
th_image = step(hclose, step(herode, filteredout));
[area, bbox] = step(hblob, th_image);
Idx = bbox(1,:) > line_row;
ratio = zeros(1, length(Idx));
ratio(Idx) = single(area(1,Idx))./single(bbox(3,Idx).*bbox(4,Idx));
ratiob = ratio > 0.4;
count = int32(sum(ratiob));
bbox(:, ~ratiob) = int32(-1);

y2 = step(hshapeins1, image, bbox);
y2(22:23,:,:) = 1;
y2(1:15,1:30,:) = 0;
image_out = step(htextins, y2, count);

if firstTime
[R C] = size(of);
RV = borderOffset:decimFactorRow:(R-borderOffset);
CV = borderOffset:decimFactorCol:(C-borderOffset);
[Y X] = meshgrid(CV,RV);
firstTime = false;
end

tmp = of(RV,CV) .* MotionVecGain;
lines = [X(:)';Y(:)';X(:)' + imag(tmp(:))';Y(:)' + real(tmp(:))'];
mv_video = step(hshapeins2, image, lines);

step(hVideo1, image);
step(hVideo2, mv_video);
step(hVideo3, th_image);
step(hVideo4, image_out);
end



hasilnya running:

0 komentar:

Posting Komentar