poster
2019-12-10, 20:30
例如,如果我有一个描述矩形的向量
xy=[165 88; 401 88; 401 278; 165 278]; 在图像上。
我如何获得以下向量
[165 88; % increase X - hold y 166 88; 167 88; ... ; 399 88; 400 88; 401 88; % hold x - increase y 401 89; 401 90; 401 91; ... ; 401 276; 401 277; 401 278; % decrease X - hold y 400 278; 399 278; 398 278; ... ; 167 278; 166 278; 165 278; % hold x - decrease y 165 277; 165 276; ... ; 165 87]; 使用MATLAB内置函数还是我需要使用FOR LOOPS编写它?
该算法必须适用于具有n点和xy坐标的通用矢量。
回答:
如果有图像处理工具箱,则可以通过创建多边形的图像,然后找到轮廓来实现:
xy=[165 88; 401 88; 401 278; 165 278]; %# create the image - check the help for impolygon for how to make sure that %# your line is inside the pixel img = poly2mask(xy(:,1),xy(:,2),max(xy(:,1))+3,max(xy(:,2))+3); figure,imshow(img) %# show the image %# extract the perimeter. Note that you have to inverse x and y, and that I had to %# add 1 to hit the rectangle - this shows one has to be careful with rectangular %# polygons boundary = bwtraceboundary(logical(img),xy(1,[2,1])+1,'n',8,inf,'clockwise'); %# overlay extracted boundary hold on, plot(boundary(:,2),boundary(:,1),'.r') 编辑以显示如何使用bwtraceboundary以及警告矩形的像素偏移。
更多&回答... (https://stackoverflow.com/questions/2358173)
xy=[165 88; 401 88; 401 278; 165 278]; 在图像上。
我如何获得以下向量
[165 88; % increase X - hold y 166 88; 167 88; ... ; 399 88; 400 88; 401 88; % hold x - increase y 401 89; 401 90; 401 91; ... ; 401 276; 401 277; 401 278; % decrease X - hold y 400 278; 399 278; 398 278; ... ; 167 278; 166 278; 165 278; % hold x - decrease y 165 277; 165 276; ... ; 165 87]; 使用MATLAB内置函数还是我需要使用FOR LOOPS编写它?
该算法必须适用于具有n点和xy坐标的通用矢量。
回答:
如果有图像处理工具箱,则可以通过创建多边形的图像,然后找到轮廓来实现:
xy=[165 88; 401 88; 401 278; 165 278]; %# create the image - check the help for impolygon for how to make sure that %# your line is inside the pixel img = poly2mask(xy(:,1),xy(:,2),max(xy(:,1))+3,max(xy(:,2))+3); figure,imshow(img) %# show the image %# extract the perimeter. Note that you have to inverse x and y, and that I had to %# add 1 to hit the rectangle - this shows one has to be careful with rectangular %# polygons boundary = bwtraceboundary(logical(img),xy(1,[2,1])+1,'n',8,inf,'clockwise'); %# overlay extracted boundary hold on, plot(boundary(:,2),boundary(:,1),'.r') 编辑以显示如何使用bwtraceboundary以及警告矩形的像素偏移。
更多&回答... (https://stackoverflow.com/questions/2358173)