필터 지우기
필터 지우기

Drawing the major and minor axis of an elliptical object in Matlab

조회 수: 18 (최근 30일)
LoserG G
LoserG G 2012년 4월 2일
답변: Image Analyst 2024년 6월 3일
This program currently inputs an image of a coin, thresholds it, binarizes it, and finds the major and minor axis lengths of the segmented elliptical using the regionprops function. What I want to do is output a subplot where I draw the axes used to calculate the 'MajorAxisLength' and 'MinorAxisLength' over the original image. Would really appreciate help with this.
I have appended the code for your perusal.
rgbImage = imread(coin2.jpg);
subplot(2, 3, 1);
imshow(rgbImage, []);
title('Original Image');
redChannel = rgbImage(:, :, 1);
binaryImage = redChannel < 100;
subplot(2, 3, 3);
imshow(binaryImage, []);
title('Binarized Image');
labeledImage = bwlabel(binaryImage);
area_measurements = regionprops(labeledImage,'Area');
allAreas = [area_measurements.Area];
biggestBlobIndex = find(allAreas == max(allAreas));
keeperBlobsImage = ismember(labeledImage, biggestBlobIndex);
measurements = regionprops(keeperBlobsImage,'MajorAxisLength','MinorAxisLength')
% Display the original color image with outline.
subplot(2, 3, 4);
imshow(rgbImage);
hold on;
title('Original Color Image with Outline', 'FontSize',fontSize);
boundaries = bwboundaries(keeperBlobsImage);
blobBoundary = boundaries{1};
plot(blobBoundary(:,2), blobBoundary(:,1), 'g-', 'LineWidth', 1);
hold off;

채택된 답변

Rick Rosson
Rick Rosson 2012년 4월 3일
Here's a start:
ctr = regionprops(keeperBlobsImage,'centroid');
theta = regionprops(keeperBlobsImage,'orientation');
xMajor = ctr(1) + [ -1 +1 ] * measurements(1)*cosd(theta)/2;
yMajor = ctr(2) + [ -1 +1 ] * measurements(1)*sind(theta)/2;
line(xMajor,yMajor);
  댓글 수: 4
ahmet ardahanli
ahmet ardahanli 2019년 8월 2일
it was very useful. thanks but how to minor axis?
DGM
DGM 2024년 6월 3일
% a logical image with multiple blobs
mask = imread('tiltellipses.png')>128;
imshow(mask); hold on
% get the properties of all blobs
S = regionprops(mask,'centroid','orientation','MajorAxisLength','MinorAxisLength');
% annotate each blob
for k = 1:numel(S)
% major
xj = S(k).Centroid(1) + [-1 1] * S(k).MajorAxisLength*cosd(S(k).Orientation)/2;
yj = S(k).Centroid(2) - [-1 1] * S(k).MajorAxisLength*sind(S(k).Orientation)/2;
% minor
xn = S(k).Centroid(1) + [-1 1] * S(k).MinorAxisLength*cosd(S(k).Orientation + 90)/2;
yn = S(k).Centroid(2) - [-1 1] * S(k).MinorAxisLength*sind(S(k).Orientation + 90)/2;
% each pair of lines is one object
plot([xj NaN xn],[yj NaN yn],'-o','linewidth',2)
end

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2024년 6월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by