bwboundaries but returns pixel edges rather than pixel centers
2 次查看(过去 30 天)
显示 更早的评论
Context
I have a set of point coordinates (2D) and a set of different regions of interest (ROI, binary image). I want to classify the point coordinates according to in which ROI they are located.
Variables:
- ROIimg: binary image with ROI's
- spots: n-by-2 matrix where each row hold the x- and y-coordinates of a spot
My first try was to use bwboundaries() and then for each point use inpolygon():
[boundaries,LabeledROI,num] = bwboundaries(ROIimg);
spotLabels=nan(size(spots,1),1);
for roiIdx=1:num
foundcell=inpolygon(spots(:,1),spots(:,2),boundaries{roiIdx,1}(:,2),boundaries{roiIdx,1}(:,1));
spotLabels(foundcell)=roiIdx;
end
Problem
This approach works for most spots, however, the boundaries that are produced by bwboundaries consist of the pixel centers of the boundary pixels, not of their edges/boundaries. Consequently, for spots that lie in a pixel on the edge of the ROI, but on the outer halve of the pixel, the spot will not be labeled as inside the boundary of that ROI.
Illustration
figure, hold on,
imagesc([0 127],[0 127],ROIimg)
axis image
for roiIdx=1:num
plot(boundaries{roiIdx,1}(:,2)-1,boundaries{roiIdx,1}(:,1)-1)
end
plot(spots(:,1),spots(:,2),'+')
As a temporary workaround, I am using https://nl.mathworks.com/matlabcentral/fileexchange/21925-accurate-polygon-extension This is a better approximation, but it does not work for pixel corners.
0 个评论
回答(0 个)
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!