distance between object moving

1 view (last 30 days)
Mehul Sompura
Mehul Sompura on 9 Oct 2019
Answered: Prabhan Purwar on 17 Oct 2019
How do i find the distance between these two cars??
inout both image, see for the cahnge and then find the distance.
a= imread( 'car1.png')
b = imread('car2.png')
c = corr2(a,b); %finding the correlation btwn two images
if c==1
disp('The images are same')%output display
else
disp('the images are not same')
end;
k=2; for i=1:1:length(g)-1 x(i) = g(i).Centroid(1); y(i) = g(i).Centroid(2);
x(k)=g(k).Centroid(1); y(k)=g(k).Centroid(2);
distance=sqrt((x(i)-x(k))^2+(y(i)-y(k))^2);

Answers (1)

Prabhan Purwar
Prabhan Purwar on 17 Oct 2019
The following code illustrates the working of regionprops()function to determine the boundary along with the connected components.
Assumption: It is assumed that the camera is fixed at a point and captures images.
clc
close all
clear
a= imread( 'car1.png'); %Read images
b = imread('car2.png');
a=rgb2gray(a);
b=rgb2gray(b);
d=a-b; %Background Substraction
BW=d;
BW=medfilt2(BW); %Median Filter
BW=edge(BW,'Canny',0.2); %Edge detection
se = strel('line',11,90); %Dilate Image
BW = imdilate(BW,se);
imshow(BW)
labeledImage = bwlabel(BW); %Labeling and Bounding Box around car
measurements = regionprops(labeledImage, 'BoundingBox', 'Area');
for k = 1 : length(measurements)
thisBB = measurements(k).BoundingBox;
rectangle('Position', [thisBB(1),thisBB(2),thisBB(3),thisBB(4)],...
'EdgeColor','r','LineWidth',2 )
end
b1=measurements(1).BoundingBox; %Measuring distance
b2=measurements(2).BoundingBox;
x1=b1(1,1)+b1(1,3)/2;
x2=b2(1,1)+b2(1,3)/2;
y1=b1(1,2)+b1(1,4)/2;
y2=b2(1,2)+b2(1,4)/2;
hold on
scatter(x1,y1);
scatter(x2,y2);
line([x1 x2], [y1 y2]);
distance=sqrt((x2-x1)^2 + (y2-y1)^2); %Distance
Output:
cardis1.jpg
cardis.jpg
Refer to the following link for further information:

Community Treasure Hunt

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

Start Hunting!