フィルターのクリア

Lattice drawing using blob centroids

3 ビュー (過去 30 日間)
Sean Ivan Roxas
Sean Ivan Roxas 2022 年 3 月 3 日
コメント済み: Sean Ivan Roxas 2022 年 3 月 6 日
How do I draw a line/lattice in between centroids determined from blob analysis? I have made a blob detection for a network of coins and I would like to have a lattice be drawn to show which coins are connected in the network.
This is a sample of what I am trying to do. The lattice showwn here was just manually drawn.
And this is my code to get the centroids of the coins.
diskElem = strel('disk',110);
IBWopen = imopen(BW,diskElem);
hBlobAnalysis = vision.BlobAnalysis('MinimumBlobArea', 10000,'MaximumBlobArea',150000);
[objArea,objCentroid,bboxOut] = step(hBlobAnalysis, IBWopen);

採用された回答

Image Analyst
Image Analyst 2022 年 3 月 3 日
You can use pdist2(), in the stats toolbox, to find the distance between all centroids. From that you can easily find the smallest distance and I'd then extract all points that are closer than 1.5 times that distance and draw a line between them. Something like (untested)
props = regionprops(IBWopen , 'Centroid', 'EquivDiameter');
meanDiam = mean([props.EquivDiameter])
xy = vertcat(props.Centroid);
distances = pdist2(xy, xy);
hold on;
for k = 1 : size(distances, 1)
thisRow = distances(row, :);
closeIndexes = find(thisRow > 0 & thisRow < 1.5 * meanDiam);
% Draw lines
for k2 = 1 : length(closeIndexes)
x1 = xy(row, 1);
y1 = xy(row, 2);
x2 = xy(closeIndexes(k2), 1);
y2 = xy(closeIndexes(k2), 2);
plot([x1, x2], [y1, y2], 'k-', 'LineWidth', 2)
end
end
hold off;
  2 件のコメント
Sean Ivan Roxas
Sean Ivan Roxas 2022 年 3 月 5 日
Thanks for answering! I had just started using Matlab last week so I'm not quite sure how I will implement what you suggested. Will keep you posted if ever I do have a follow-up question.
Sean Ivan Roxas
Sean Ivan Roxas 2022 年 3 月 6 日
Hi Image Analyst! I'm having trouble trying to implement what you suggested. For now I would like some help on printing the centroids of the coins first similar to what you had answered before which is this image.
This is the entirety of the code that I've been able to create for the blob detection. Hoping to get a response from you
close all
clear all
pic=imread('sample.jpeg');
subplot(1,3,1);
imshow(pic);
%% Color Threshold
% Convert RGB image to chosen color space
I = rgb2hsv(pic);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.022;
channel1Max = 0.146;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 0.051;
channel2Max = 1.000;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 0.292;
channel3Max = 0.903;
% Create mask based on chosen histogram thresholds
sliderBW = (I(:,:,1) >= channel1Min ) & (I(:,:,1) <= channel1Max) & ...
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
BW = sliderBW;
subplot(1,3,2);
imshow(BW);
%% Remove Disturbances
diskElem = strel('disk',15);
IBWopen = imopen(BW,diskElem);
subplot(1,3,3);
imshow(IBWopen);
%% Blob Analysis
hBlobAnalysis = vision.BlobAnalysis('MinimumBlobArea', 10000,'MaximumBlobArea',150000);
[objArea,objCentroid,bboxOut] = step(hBlobAnalysis, IBWopen);
%% Print Centroid
%% I would like to have the centroids be printed on the original image
%center = regionprops(pic, 'Centroid');
%centroids = cat(1,center.Centroid);
%plot
%% Annotate Image
Ishape = insertShape(pic,'rectangle', bboxOut,'LineWidth',8, 'Color','red');
figure
subplot(1,1,1);
imshow(Ishape);
%% Cleaning
release(hBlobAnalysis)

サインインしてコメントする。

その他の回答 (0 件)

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by