How to measure distance between objects in a photo which should be detected?
조회 수: 10 (최근 30일)
이전 댓글 표시
Hi,
I have photos (taken by a camera) similar to the attached one. My purpose is to measure the distances d1 and d2 (between the centroids of the rectangles). As steps, I convert the rgb image to a grey and then I binarize it. The background is concrete which sometimes is smooth (like in this image) and in some cases very rough. I apply some filters so to reduce the number of objects that are detected (not experienced with image filtering). Then, I sort everything by area so that I have the stats of those 4 rectangles and subsequently I calculate the distance. My questions are:
- How can I focus only on those four rectangles and not detect every single object in the image? The shape and color are possible to change. Is there any way to capture the biggest contrast (black-white) directly?
- Instead of finding the centroids and then calculating the distance by the regular mathematical formula, is there another (smarter maybe :P) way to do it so?
Thank you very much in advance!
clear all
%% Read the image
rgb = imread('Image.png');
GR = rgb2gray(rgb);
filtered_image = imgaussfilt(GR,15);
BW = imbinarize(filtered_image,'adaptive','ForegroundPolarity','dark','Sensitivity',0.15);
BW = ~ BW;
[B,L] = bwboundaries(BW, 'noholes');
stats = regionprops(L, 'all');
figure,
imshow(rgb),
hold on
for i = 1 : length(stats)
centroid = stats(i).Centroid;
plot(centroid(1),centroid(2),'wO')
end
%% Filter the information for only the related objects
T = struct2table(stats);
Tsort = sortrows(T,-1);
for i = 1:4
Tnew(i,1:2) = table2array(Tsort(i,2)); %% 1st and 2nd column: centroinds of detected objects
Tnew(i,3) = table2array(Tsort(i,1)); %% 3rd column: Area
end
%% Calculation of distance
Calc_dist = sqrt((Tnew(2,2)-Tnew(1,2))^2 + (Tnew(2,1)-Tnew(1,1))^2);
Ref_dist = sqrt((Tnew(4,2)-Tnew(3,2))^2 + (Tnew(4,1)-Tnew(3,1))^2);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/239932/image.png)
댓글 수: 2
답변 (1개)
Constantino Carlos Reyes-Aldasoro
2019년 9월 26일
OK, before measuring the distances, you need to distinguish properly each of the objects of interest. If the images are going to be exactly like the one you show, then the squares are dark, so
1) use thresholding to detect those regions
2) Probably you will have some noise due to the small round regions (holes?) so label your objects with bwlabel and the measure the areas of each object with regionprops, then discard all the small objects and keep only the 4 largest, which should be your squares using ismember, that should leave only 4 regions
3) find the centroids of the four squares and there you can calculate the euclidean region directly with sqrt((x1-x2)^2 + (y1-y2)^2)
Hope this helps
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Image Segmentation and Analysis에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!