Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在MATLAB中绘制椭圆物体的长轴和短轴_Matlab_Image Processing - Fatal编程技术网

在MATLAB中绘制椭圆物体的长轴和短轴

在MATLAB中绘制椭圆物体的长轴和短轴,matlab,image-processing,Matlab,Image Processing,该程序当前输入硬币图像,对其进行阈值设置,对其进行二值化,并使用函数查找分割椭圆的长轴和短轴长度。如何输出子图,在其中绘制用于计算原始图像上的“MajorAxisLength”和“MinorAxisLength”的轴 我已经附加了我的代码供您阅读 % Read in the image. folder = 'C:\Documents and Settings\user\My Documents\MATLAB\Work'; baseFileName = 'coin2.jpg'; fullFileN

该程序当前输入硬币图像,对其进行阈值设置,对其进行二值化,并使用函数查找分割椭圆的长轴和短轴长度。如何输出子图,在其中绘制用于计算原始图像上的“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;
%已在图像中读取。
文件夹='C:\Documents and Settings\user\My Documents\MATLAB\Work';
baseFileName='coin2.jpg';
fullFileName=fullfile(文件夹,baseFileName);
fullFileName=fullfile(文件夹,baseFileName);
if~存在(fullFileName,'file')
fullFileName=baseFileName;%这次没有路径。
if~存在(fullFileName,'file')
%提醒用户。
errorMessage=sprintf('错误:%s不存在',fullFileName);
uiwait(warndlg(errorMessage));
返回;
结束
结束
rgbImage=imread(完整文件名);
%获取图像的尺寸。NumberOfColorBand应为=3。
[rows columns numberOfColorBands]=大小(rgbImage);
%显示原始彩色图像。
子地块(2、3、1);
imshow(rgbImage,[]);
标题(“原始彩色图像”、“字体大小”、“字体大小”);
%将数字放大到全屏。
设置(gcf,“位置”,获取(0,“屏幕大小”);
%提取单个红色通道。
红色通道=rgbImage(:,:,1);
%显示红色通道图像。
子地块(2,3,2);
imshow(红色频道,[]);
标题(“红色通道图像”、“字体大小”、“字体大小”);
%二值化
二进制图像=红色通道<100;
%显示图像。
子地块(2、3、3);
imshow(二进制图像,[]);
标题(“阈值图像”、“字体大小”、“字体大小”);
binaryImage=imfill(binaryImage,‘孔’);
labeledImage=bwlabel(二进制图像);
面积测量=区域属性(标签图像,'area');
allAreas=[面积测量面积];
biggestBlobIndex=find(allreas==max(allreas));
keeperBlobsImage=ismember(labeledImage,biggestBlobIndex);
测量值=区域属性(keeperBlobsImage、'MajorAxisLength'、'MinorAxisLength')
%显示带有轮廓的原始彩色图像。
子地块(2、3、4);
imshow(rgbImage);
等等
标题(‘带轮廓的原始彩色图像’、‘FontSize’、‘FontSize’);
边界=边界(keeperBlobsImage);
blobBoundary=边界{1};
绘图(blobBoundary(:,2),blobBoundary(:,1),“g-”,“线宽”,1);
拖延;

通常是通过计算特征向量来实现的,如维基百科文章“示例”中所述。这是正确的方法

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

很抱歉没有准备好MATLAB代码


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

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

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


要对短轴执行相同的操作,只需注意它将与长轴逆时针方向成90度,然后重复上述步骤。

我在两年前的某个项目中执行了与您相同的任务。我在下面修改了我当时为您使用的代码。它涉及计算数据点的协方差矩阵,并找到它们的特征值/特征向量。请注意,由于圆对称,短轴和长轴在某种程度上是“随机”的。还要注意的是,为了保持代码的简单,我以一种非常幼稚的方式将图像二进制化

% 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

我很感激你抽出时间给我一些意见。谢谢你把这些都写出来,这正是我所寻求的帮助。如果我的答案或上面更详细的一个提供帮助,请考虑对他们进行投票(点击左上箭头)以表明它们是有用的。不幸的是,我还没有提升投票所需的声誉。