参考这个problem,给定一个连续的正方形网格单元(黄色单元格),我们必须通过连接外围单元的中心来提取周边多边形。
我的问题是如何提取由外围网格单元边缘形成的周边多边形,这意味着下图中的绿色多边形。
我不认为这是一个图像处理问题。这纯粹是一个几何问题,因为输入是以阵列形式知道的黄色网格单元格,如:indices=[64,65,88,89,90....
。输入不是粉红色背景中黄色单元格的图像。那只是一个可视化。
此处给出了从单元格中心生成外多边形的代码: -
clear;
close all;
A=ones(25,25);
indices=[64,65,88,89,90,91,111,112,113,114,115,116,117,135,136,137,138,139,140,141,142,143,158,159,160,161,162,163,164,165,166,167,168,169,182,183,184,185,186,187,188,189,190,191,192,193,194,195,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,257,258,259,260,261,262,263,264,265,266,267,268,269,270,271,282,283,284,285,286,287,288,289,290,291,292,293,294,295,307,308,309,310,311,314,315,316,317,318,319,320,341,342,343,344,345,367,368,369,393,394];
A(indices)=2;
figure
hold on
axis equal
axis ij
xx=10:20:500;
yy=10:20:500;
imagesc(xx,yy,A);
colormap([1.0000 0 0.7931;
1.0000 0.8276 0]);
rt=[0,500];
gcs=0:20:500;
gds=ones(size(rt,2),size(gcs,2));
gds = bsxfun(@times,gds,gcs);
plot(rt,gds,'Color','k','LineStyle','-');
plot(gds,rt,'Color','k','LineStyle','-');
axis([0 350 100 450])
A=A-1;
% we identify patterns with edges over 2x2 patches, describing with
% the first 4 binary values what pixels are set, and with the next 2
% the edge with 2 indices over the 2x2 patch
patterns = [
0,1,0,1, 3,4 % vertical edge at rhe right
1,0,1,0, 1,2 % vertical edge at the left
0,0,1,1, 2,4 % horizontal edge at the bottom
1,1,0,0, 1,3 % horizontal edge at the top
1,0,0,1, 1,4 % diagonal edge
0,1,1,0, 2,3 % diagonal edge
1,0,1,1, 1,4 % diagonal edge, extra pixel set
1,1,0,1, 1,4 % diagonal edge, extra pixel set
1,1,1,0, 2,3 % diagonal edge, extra pixel set
0,1,1,1, 2,3 % diagonal edge, extra pixel set
];
% 2x2 patches (matrix form)
P00 = A(1:end-1,1:end-1);
P10 = A(2:end,1:end-1);
P01 = A(1:end-1,2:end);
P11 = A(2:end,2:end);
% edge unique identifier using powers of 2
id = @(p00,p01,p10,p11) 1*p00 + 2*p10 + 4*p01 + 8*p11;
P = id(P00,P01,P10,P11); % vectorized pattern identification
% edges
e0 = []; % from (i,j)
e1 = []; % to (i,j)
for i = 1:size(patterns, 1) % small loop over the 10 patterns
p = patterns(i, :);
E = (P == id(p(1),p(2),p(3),p(4))); % pattern search, vectorized
[c,r] = ind2sub(size(E), find(E));
[c0,r0] = ind2sub([2,2], p(5));
[c1,r1] = ind2sub([2,2], p(6));
e0 = [e0; c+c0, r+r0];
e1 = [e1; c+c1, r+r1];
end
X = [e0(:,2) e1(:,2)]';
Y = size(A,1) - [e0(:,1) e1(:,1)]';
X=X*20; X=X-30;
Y=Y*20; Y=470-Y;
XY_from = [X(1, :)' Y(1, :)' zeros(size(X, 2), 1)];
XY_to = [X(2, :)' Y(2, :)' zeros(size(X, 2), 1)];
new_poly = zeros(size(X, 2)+1, 2);
new_poly(1, :) = XY_from(1, 1:2);
for i=2:size(new_poly, 1)
index = find(all([new_poly(i-1, :) 0] == XY_from, 2), 1);
if isempty(index)
index = find(all([new_poly(i-1, :) 0] == XY_to, 2), 1);
new_poly(i, :) = XY_from(index, 1:2);
else
new_poly(i, :) = XY_to(index, 1:2);
end
XY_from(index, 3) = 1;
XY_to(index, 3) = 1;
end
hold on
plot(new_poly(:, 1), new_poly(:, 2), '--b', 'LineWidth', 2);
我提到here但它不是Matlabby解决方案,似乎是针对图像处理。