【问题标题】:Drawing the major and minor axis of an elliptical object in MATLAB在 MATLAB 中绘制椭圆对象的长轴和短轴
【发布时间】:2012-04-16 14:19:45
【问题描述】:

此程序当前输入硬币图像,对其进行阈值化、二值化,并使用 regionprops 函数找到分段椭圆的长轴和短轴长度。如何在原始图像上绘制用于计算“MajorAxisLength”和“MinorAxisLength”的轴的子图?

我已附上我的代码供您阅读。

% Read in the image.
folder = 'C:\Documents and Settings\user\My Documents\MATLAB\Work';
baseFileName = 'coin2.jpg';
fullFileName = fullfile(folder, baseFileName);
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
    fullFileName = baseFileName; % No path this time.
    if ~exist(fullFileName, 'file')
        %Alert user.
        errorMessage = sprintf('Error: %s does not exist.', fullFileName);
        uiwait(warndlg(errorMessage));
        return;
    end
end

rgbImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows columns numberOfColorBands] = size(rgbImage);
% Display the original color image.
subplot(2, 3, 1);
imshow(rgbImage, []);
title('Original color Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Position', get(0,'Screensize'));

% Extract the individual red color channel.
redChannel = rgbImage(:, :, 1);
% Display the red channel image.
subplot(2, 3, 2);
imshow(redChannel, []);
title('Red Channel Image', 'FontSize', fontSize);
% Binarize it
binaryImage = redChannel < 100;
% Display the image.
subplot(2, 3, 3);
imshow(binaryImage, []);
title('Thresholded Image', 'FontSize', fontSize);

binaryImage = imfill(binaryImage, 'holes');
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;

【问题讨论】:

    标签: matlab image-processing


    【解决方案1】:

    查看Orientation 属性的文档,regionprops() 可以返回给您。

    这给出了正 x 轴和椭圆长轴之间的角度。您应该能够根据该角度推导出长轴线的方程,然后制作一个 x 轴点网格,并计算网格中所有点的长轴线值,然后将其绘制出来就像您在 MATLAB 中绘制任何其他曲线一样。

    要对短轴做同样的事情,只需注意它会从长轴逆时针旋转 90 度,然后重复上述步骤。

    【讨论】:

    • 如果我的回答或上面更详细的回答确实提供了帮助,请考虑为他们投票(点击左侧的向上箭头)以表明他们很有帮助。
    • 很遗憾,我还没有投票所需的声誉。
    【解决方案2】:

    通常通过计算特征向量来完成,如维基百科文章 Image moment 中“示例”下的说明。这才是正确的方法。

    但我想知道,如果您从 MATLAB 中知道质心和边界框,那么长轴的端点必须在左上角或右上角。所以检查(除了噪声)这两个角,如果有像素,会给你主轴。然后,短轴就与质心正交。

    很抱歉没有准备好 MATLAB 代码。

    推理没有那么错误,但也不是那么好,使用上面写的方向更好;)

    【讨论】:

    • 感谢您抽出宝贵时间给我一些意见。
    【解决方案3】:

    我在 2 年前完成的某个项目中的任务与您相同。我已经修改了我在下面为您使用的代码。它涉及计算数据点的协方差矩阵并找到它们的特征值/特征向量。请注意,由于圆对称,短轴和长轴会有些“随机”。另请注意,我以一种非常幼稚的方式制作了图像二进制文件以保持代码简单。

    % Load data and make bw
    clear all;close all; clc; 
    set(0,'Defaultfigurewindowstyle','docked')
    
    I = imread('american_eagle_gold_coin.jpg');
    Ibw = im2bw(I,0.95);
    Ibw = not(Ibw);
    
    figure(1);clf
    imagesc(Ibw);colormap(gray)
    
    %% Calculate axis and draw
    
    [M N] = size(Ibw);
    [X Y] = meshgrid(1:N,1:M);
    
    %Mass and mass center
    m = sum(sum(Ibw));
    x0 = sum(sum(Ibw.*X))/m;
    y0 = sum(sum(Ibw.*Y))/m;
    
    %Covariance matrix elements
    Mxx = sum(sum((X-x0).^2.*Ibw))/m;
    Myy = sum(sum((Y-y0).^2.*Ibw))/m;
    Mxy = sum(sum((Y-y0).*(X-x0).*Ibw))/m;
    
    MM = [Mxx Mxy; Mxy Myy];
    
    [U S V] = svd(MM);
    
    W = V(:,1)/sign(V(1,1)); %Extremal directions (normalized to have first coordinate positive)
    H = V(:,2);
    W = 2*sqrt(S(1,1))*W; %Scaling of extremal directions to give ellipsis half axis
    H = 2*sqrt(S(2,2))*H;
    
    figure(1)
    hold on
        plot(x0,y0,'r*');
        quiver(x0,y0,W(1),H(1),'r')
        quiver(x0,y0,W(2),H(2),'r')
    hold off
    

    【讨论】:

    • 感谢您写下这一切,这正是我正在寻找的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多