MATLAB Answers

Measure the displacement of an object between two figures or more.

3 views (last 30 days)
Dear all,
I am completely new in image processing and I do not know how to start. I have a set of pictures where only one object has different positions, the rest of the things in the pictures are static. I want to keep the object position of the first picture as a reference and measure how much that object has moved in the other pictures.
I think there must already be a straightforward example, but I could not find it.

Accepted Answer

yanqi liu
yanqi liu on 5 Feb 2021
clear all; clc; close all;
rects = [];
for i = [2 6 12]
img = imread(sprintf('./G%d.JPG', i));
img = imresize(img, 600/size(img,1), 'bilinear');
J = rgb2lab(img);
a = mat2gray(J(:,:,1));
b = im2bw(a, 0.7);
b = imclose(b, strel('disk', 20));
b = imfill(b, 'holes');
b = imclearborder(b);
b = bwareafilt(b,1);
[r, c] = find(b);
rect = [min(c) min(r) max(c)-min(c) max(r)-min(r)];
rects = [rects; rect];
figure(1); cla;
imshow(img, []);
hold on; rectangle('Position', rect, 'EdgeColor', 'm', 'LineWidth', 2);
plot(rects(:,1)+rects(:,3)/2, rects(:,2)+rects(:,4)/2, 'w--', 'LineWidth', 4);
dis = norm([rects(end,1)+rects(end,3)/2 rects(end,2)+rects(end,4)/2]-[rects(1,1)+rects(1,3)/2 rects(1,2)+rects(1,4)/2]);
title(sprintf('moved %.2f', dis));
  1 Comment
Carlos Imbaquingo
Carlos Imbaquingo on 7 Feb 2021
Yes, Thank you very much Yanqi Liu!
That is exactly what I needed.
Kind regards,

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!