Finding distance between centers of circles by manually selecting muliple pairs

2 次查看(过去 30 天)
I am having trouble finding the correct distances from circle to circle. Is there a way to select manually which circles to measure the distances between? I would need to select multiple pairs and a circle can be in a pair more than once. Then be able to advance the program when finished. Not all circles are necessary to be selected. The circle finding itself works nicely.
I included the entire code. It grabs images from a file to analyze. Thank you.
directory = uigetdir;
files = dir(fullfile(directory, '*.bmp'));
for k = 1:length(files)
fileName = files(k).name;
fullFilePath = fullfile(directory, fileName);
fprintf(1, 'Processing: %s\n', fullFilePath);
rgb = imread(fullFilePath);
rgb = imcrop(rgb,[950 0 400 750]);
rgb = rgb2hsv(rgb);
rgb = rgb(:,:,3);
rgb = imadjust(rgb, [0.1 0.85]);
%rgb = im2gray(rgb);
rgb = adapthisteq(rgb);
%crop
rgb = im2bw(rgb,0.3);
%binary conversion
imshow(rgb)
d = drawline;
pos = d.Position;
diffPos = diff(pos);
diameter = hypot(diffPos(1),diffPos(2))
[centers,radii] = imfindcircles(rgb,[10 400],'ObjectPolarity','dark','Sensitivity',0.8)
imshow(rgb)
h = viscircles(centers,radii);
[centers,radii] = imfindcircles(rgb,[10 400],'ObjectPolarity','dark', ...
'Sensitivity',0.8);
length(centers)
%
delete(h) % Delete previously drawn circles
h = viscircles(centers,radii);
[centers,radii] = imfindcircles(rgb,[10 400],'ObjectPolarity','dark', ...
'Sensitivity',0.8,'Method','twostage');
delete(h)
h = viscircles(centers,radii);
%hBright = viscircles(centersBright, radiiBright,'Color','b');
h = viscircles(centers,radii);
pix = 0.00001030927835;
%calibration for pix to meter
Yp(:) = centers(:,2);
%need to do every other circle, This does distance between every one
distance = abs(diff(Yp))
%distance between center in pix
Ydiff = distance*pix
%distance between center in meters
v = 0.142082
% change this variable 'v' for every test run
frequency = Ydiff/v
% units 1/s
diameter = 0.00635;
% units m
Strouhal = (frequency * diameter) / v
%final strouhal number
end

回答(1 个)

Sandeep Mishra
Sandeep Mishra 2025-1-17
编辑:Sandeep Mishra 2025-1-17
Hi CLAYTON,
You can utilize the ‘ginput’ MATLAB function to manually select circles.
Below is an example code snippet demonstrating how to select multiple circles and store the pairs in a vector:
% Example Centers of different circles
centres = [300,350; 400,450; 100,150; 200,250]
selectedPairs = [];
while true
[x, y, button] = ginput(2);
% Exit the function on pressing ‘Enter’ button
if isempty(x)
break;
end
% Finding nearest circles to the selected points
idx1 = knnsearch(centers, [x(1), y(1)]);
idx2 = knnsearch(centers, [x(2), y(2)]);
selectedPairs = [selectedPairs; idx1, idx2];
end
Refer to the following MathWorks Documentation to learn more about ‘ginput’ function: https://www.mathworks.com/help/matlab/ref/ginput.html
I hope this helps you in resolving your query!

类别

Help CenterFile Exchange 中查找有关 Data Exploration 的更多信息

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by