在MATLAB中绘制椭圆对象的长轴和短轴

mg6*_*011 2 matlab image-processing

该程序当前输入硬币的图像,对其进行阈值处理,对其进行二值化,并使用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;
Run Code Online (Sandbox Code Playgroud)

Vid*_*dar 5

对于我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
Run Code Online (Sandbox Code Playgroud)

原始图像 从二进制图像中找到轴 在此输入图像描述