在MATLAB中绘制椭圆对象的长轴和短轴 [英] Drawing the major and minor axis of an elliptical object in MATLAB

查看:387
本文介绍了在MATLAB中绘制椭圆对象的长轴和短轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此程序当前输入硬币图像,对其进行阈值处理,对其进行二值化,并使用 regionprops 功能。如何输出一个子图,我绘制用于计算原始图像上的'MajorAxisLength'和'MinorAxisLength'的轴?

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. How do I output a subplot where I draw the axes used to calculate the 'MajorAxisLength' and 'MinorAxisLength' over the original image?

我附上了我的代码供你细读。

I have appended my code for your perusal.

% 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;


推荐答案

我和某些项目有同样的任务我做了2年前。我在下面修改了我用过的代码。它涉及计算数据点的协方差矩阵并找到它们的特征值/特征向量。请注意,由于圆对称,次轴和长轴将略微随机。另请注意,我已经以非常天真的方式制作了图像二进制文件以保持代码简单。

I had the same task as you for some project I did 2 years ago. I've modified the code I used then for you below. It involved calculating the covariance matrix for the datapoints and finding their eigenvalues/eigenvectors. Note here that because of circular symmetry, the minor and major axis will be somewhat "random". Also note that I have made the image binary in a very naïve way to keep the code simple.

% 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



这篇关于在MATLAB中绘制椭圆对象的长轴和短轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆