Matlab 计算多边形上的像素数
我一直在拍一张照片并在上面画一个轮廓。我需要在三个类别中计算像素的数量和位置(在MATLAB中)Matlab 计算多边形上的像素数,matlab,contour,boundary,point-in-polygon,Matlab,Contour,Boundary,Point In Polygon,我一直在拍一张照片并在上面画一个轮廓。我需要在三个类别中计算像素的数量和位置(在MATLAB中) 曲线外的像素 曲线内的像素 曲线边界上的像素 我尝试在MATLAB中使用inpolygon。它可以计算内部和外部的像素,但不能计算边界上的像素。在边界上,它只计算那些直接通过小网格中心的网格。我还需要计算轮廓通过小网格四条边中任意一条的像素数 请帮忙。 我提供了下面的代码 %Polygon Plotting clc;clear all;close all; I = imread('camerama
%Polygon Plotting
clc;clear all;close all;
I = imread('cameraman.tif');
I = imresize(I,[100 100]);
I = double(I(:,:,1));
imagesc(I,[0 255]);colormap(gray);
axis([1 size(I,1) 1 size(I,2)]);
[BW xi yi] = roipoly(); %select your own coordinates.
X=xi;
Y=yi;
hold on;
contour(BW,'r');
hold off;
xa = 1 : size(I,2);
ya = 1 : size(I,1);
[x,y] = meshgrid(xa,ya);
[in on] = inpolygon(x,y,X,Y);
count1 = sum(sum(in));
count2 = size(I,1)*size(I,2) - count1;
count3 = sum(sum(on));
%count1 = inside the polygon and on boundary
%count2 = outside the polygon
%count3 = on the boundary only
inside = zeros(count1,2);
outside = zeros(count2,2);
onthecurve = zeros(count3,2);
l=1;m=1;n=1;
for i = 1:size(I,1)
for j = 1:size(I,2)
if in(i,j)==1
inside(l,1)=i;
inside(l,2)=j;
l=l+1;
end
if in(i,j)==0
outside(m,1)= i;
outside(m,2)= j;
m = m+1;
end
if on(i,j)==1
onthecurve(n,1)= i;
onthecurve(n,2)= j;
n = n+1;
end
end
end
figure,
plot(inside(:,1),inside(:,2),'+g');
axis([1 size(I,1) 1 size(I,2)]);
hold on
plot(outside(:,1),outside(:,2),'+r');
hold on
plot(onthecurve(:,1),onthecurve(:,2),'+b');
hold off
如果图像显示不正确,请参阅链接:
1.
二,
可以看出,轮廓上的点没有用蓝色标记。实际上,count3几乎总是给出一个输出0。所以inpolygon在计算边界上的点时不是很有效
我如何修改我的代码以便也计算这些像素?
谢谢大家。您可以使用来检测黑白(BW
)图像的边界(本例中的输入多边形)
还要显示所有像素都包含在3组中:
prod(size(I)) - sum(sum(inBW + outBW + edBW))
应该变成零
希望有帮助。谢谢!但是我想在不使用边缘操作符的情况下完成这个函数。任何其他的建议我都很感激。另外一件事我真的很想计算出曲线内外的像素。通过使用for循环,我已经很容易地做到了。然后我想显示绘图,并将其与原始轮廓进行比较。然而,它们并不相似。这是因为在图像中,点(1,1)位于最左上角,而在Matlab中的矩阵中,(1,1)位于最左下角。如何在plot命令中反转y轴?如果您不想使用
edge
命令,您可以编写自己的边缘检测函数,我非常确定有许多用于边缘检测的算法。关于将矩阵的索引与图像进行匹配,请看。
prod(size(I)) - sum(sum(inBW + outBW + edBW))