Draw rectangle on optical flow points

조회 수: 1 (최근 30일)
Nataliya
Nataliya 2015년 6월 18일
Hello, I am detecting the moving object using optical flow. Here is the code to find the vectors of optical flow.
%%Load images
clear all;
load traffic_frames
figure();
subplot 211
imshow(fr1);
im1 = im2double(rgb2gray(fr1));
subplot 212
imshow(fr2);
im2 = im2double(rgb2gray(fr2));
%%Find corners
% Define the window size for Lucas-Kanade method
ww = 40;
w = round(ww/2);
% Reduce the size of the image
sc = 2;
im2c = imresize(im2, 1/sc);
C1 = corner(im2c);
C1 = C1*sc;
% Discard coners near the margin of the image
k = 1;
for i = 1:size(C1,1)
x_i = C1(i, 2);
y_i = C1(i, 1);
if x_i-w>=1 && y_i-w>=1 && x_i+w<=size(im1,1)-1 && y_i+w<=size(im1,2)-1
C(k,:) = C1(i,:);
k = k+1;
end
end
% Plot corners on the image
figure();
imshow(fr2);
hold on
plot(C(:,1), C(:,2), 'r*');
%%Implementing Lucas Kanade Method
% for each point, calculate I_x, I_y, I_t
Ix_m = conv2(im1,[-1 1; -1 1], 'valid'); % partial on x
Iy_m = conv2(im1, [-1 -1; 1 1], 'valid'); % partial on y
It_m = conv2(im1, ones(2), 'valid') + conv2(im2, -ones(2), 'valid'); % partial on t
u = zeros(length(C),1);
v = zeros(length(C),1);
% within window ww * ww
for k = 1:length(C(:,2))
i = C(k,2);
j = C(k,1);
Ix = Ix_m(i-w:i+w, j-w:j+w);
Iy = Iy_m(i-w:i+w, j-w:j+w);
It = It_m(i-w:i+w, j-w:j+w);
Ix = Ix(:);
Iy = Iy(:);
b = -It(:); % get b here
A = [Ix Iy]; % get A here
nu = pinv(A)*b;
u(k)=nu(1);
v(k)=nu(2);
end;
%%Draw the optical flow vectors
figure();
imshow(fr2);
hold on;
quiver(C(:,1), C(:,2), u,v, 1,'r')
Now I want to draw a rectangle around moving area. How can I do this. Thanks in advance.

답변 (0개)

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by