How to measure distance between two points from two different image?

4 views (last 30 days)
Sorry for asking again. I have very small knowledge about these process but I have to used them.
I have extracted the the images sequence from video.
I have applied ROI and detected the desired objects in an image.
Now I can find the centroid for the desired objected of an image within ROI.
I want to set this image to be a reference image and compare it to others images and measure the distance between their centroid in pixels.
So, how to set first image as reference image, how to fix the ROI assiged to first image to every others image, and how to measure the distance between two centroid between the reference image and other images.
I have read this page since I think it is a very similar situation but some error appeared.
Thank you in advanced.

Answers (1)

Marcel
Marcel on 7 Nov 2022
Well as KSSV pointed out, you can measure the distance between points using code like this
% Location 1 (X, Y)
x1 = 50;
y1 = 50;
% Location 2 (X, Y)
x2 = 100;
y2 = 100;
% Calculate the distance
d=sqrt((y2-y1)^2+(x2-x1)^2);
% Result: 70.7107 Pixel
(code from here)
Im made a software thats takes a picture, finds a specific circle and saves the circle radius and location data. This is my master image, tho im only saving the circle data in my case.
On every new image taken, im comparing the newly found circles with the master circles and calculate stuff like the difference of the current circles form the master circle.
If this is something you are trying to do as well, i can guide you a little bit. Sadly i cant really share the app or code for that as i made it for the company im working for and im sure they wouldnt be happy about that :p
  3 Comments
Marcel
Marcel on 8 Nov 2022
Edited: Marcel on 8 Nov 2022
So im going to post a few examples on how i did some stuff. This is for example on how im trying to find the circles and get the data like X and Y position of the circle in the image and the circle's radius.
% "frame" can be something like imread("image.png");
% "crop" is the value you get from cropping an image using imcrop. How im
% make a picture using the specified camera in "vid". see code below where
% i define "vid"
image = getsnapshot(vid);
% create a "popup" with the image taken so you can draw the rectangle for
% the area where your cirlce would be. make it not to close to the circle's
% supposed area (example below).
[~, rect] = imcrop(image);
close gcf; % Close the window afterwards as its not needed afterwards
% in my example im displaying the rectangle data in a UITable
app.UITable.Data(1,1) = cellstr(string(rect(1) + " " + rect(2) + " " + rect(3) + " " + rect(4)));
% This is a example on how im saving it to a .m file. since im using this
% in a button push event im getting the data again using UITable.Data
Crop = string(app.UITable.Data{1,1});
% this is how the data is being saved
vars = [Crop];
SaveJob(app, "example-file", vars);
% basically thats what "SaveJob" is doing
function SaveJob(~, filename, importJob)
save("path\to\dir\" + filename, "importJob");
end
Later in my code, when actually trying to "scan" an image, im using the exact data of the rect to automatically crop the image without any user input.
% later on when scanning the picture im loading the file where i saved all
% the data. in this case its the circle data.
load("path\to\dir\example.m");
% if you remember the variable "importJob" when we saved the file and
% variables, this will be the same in this case on how to read the data
% again after loading it
%
% the "regexp" might not be needed, but i ended up doing it this way for
% some reason for sure :p
% importJob(1,1) is how we can access data.
tmp_crop = regexp(importJob(1,1), " ", "split");
crop = [double(tmp_crop(1)) double(tmp_crop(2)) double(tmp_crop(3)) double(tmp_crop(4))];
okay so now we also have the crop data. we need to take a picture now using a camera as example, crop it like we did earlier and look for cirlces in the newly taken, cropped picture.
% To increase camera performence i used to keep the camera running, as
% starting and stopping the camera on every image i'd take would take way
% too long for the camera to start up, decreasing the time from about 4 - 6
% seconds to about 0.2 - 0.5 seconds per picture, depending on the size of
% the cropped location and size mostly.
try
% if the camera isnt running anymore, restart it. im using a camera via
% ethernet (GigE Camera Addon)
if isrunning(vid) == 0
vid = videoinput('gige', 1, 'BayerRG8'); % my specific camera
vid.FramesPerTrigger = 1;
triggerconfig(vid, 'manual');
start(vid);
end
catch ME
% handle the error
end
% get image
frame = getsnapshot(vid);
% since we now have the "rect" data that we saved when cropping the image,
% we can now use it to actually crop the image with the following line
[croppedBig, ~] = imcrop(frame, crop);
% now that we have the cropped image "croppedBig", we can now try to
% find the circles in the image
[centersBig, radiiBig, ~] = imfindcircles(croppedBig, [30 100], "Sensitivity", 0.9, "Method", "TwoStage", "EdgeThreshold", 0.2);
% Lets get the data. You will get an out of index error or something
% similar if no image was found! Keep that in mind. also the following two
% lines will only return the data for one cirlce.
centersBigFound = centersBig(1:1,:);
radiiBigFound = radiiBig(1:1);
Now that we have the image circle data, like location (x,y) and its radius, we can draw the circle onto the image using the following code. This is what worked for me the best and fastest.
croppedBig = insertShape(croppedBig, "Circle", ...
[centersBigFound(1,1) centersBigFound(1,2) radiiBigFound(1)], ...
"Color", "green", "LineWidth", 4);
if you want to add the cropped, edited image back to the original image, basically merging them, you can do this with the following line of code.
frame(crop(2):crop(2)+size(croppedBig,1)-1, crop(1):crop(1)+size(croppedBig,2)-1, :) = croppedBig;
Final words
I hope i was able to help you or give you some insights on how i made some of this stuff. i used to to some stuff myself and also build stuff together using many different things found on this forum as well. I was quite a bit of pain to get it all going but then it became easier.
once it starts rolling it'll be easier. Programming is fun, and i love challenges :p
I wish you the best on whatever you're trying to make :p . If you still have questions hit me up
References
Crop table reference
How i recommend cropping the area/ROI
How i wouldnt recommend cropping it:
Chanoknunt Sangsobhon
Chanoknunt Sangsobhon on 10 Nov 2022
Thank you for your guilding and tips! I will study it and adapt them to my case.
Thank you very much!

Sign in to comment.

Categories

Find more on MATLAB Support Package for IP Cameras in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!