how to simplify connected blobs

조회 수: 4 (최근 30일)
Sukuchha
Sukuchha 2014년 4월 24일
댓글: Image Analyst 2014년 5월 3일
Hi, i have a binary image. I can find the boundaries with the bwboundaries and get point list of boundary of a each blobs and pass to some line simplification algorith e.g. Douglas Peucker algorithm (see fig in http://stackoverflow.com/questions/1849928/how-to-intelligently-degrade-or-smooth-gis-data-simplifying-polygons).
The Douglas peucker give back points that need to be retained after simplification, my problem is how to connect retained points to form a closed polygon.
Anybody has a hint, how it should be done.
THanks

채택된 답변

Sven
Sven 2014년 4월 24일
편집: Sven 2014년 4월 26일
Hi Sukuchha,
I think that you just need to do this:
XY = [0 0; 1 0; 1 1; 0 1]; % unclosed XY points
XY_closed = XY([1:end 1],:); % closed XY points
The Douglas Peucker algorithm (at least, the one used by reducem in the Mapping Toolbox) takes account of the shape as a polygon, so its result should be closed (or at least closeable via the code I gave above).
Furthermore, the output from bwboundaries supplies each boundary as closed (ie, the first XY boundary point is coincident with the last XY boundary point), so I think you can simply pass the results of bwboundary to a Douglas Peucker function, and then use that result directly.
BW = false(9)
BW(3:6,3:6) = true
bb = bwboundaries(BW)
bb{1}
ans =
2 2
2 3
2 4
3 4
4 4
4 3
4 2
3 2
2 2 % Note that this last coordinate is the same as the first
[newX, newY] = reducem(bb{1}(:,2),bb{1}(:,1));
[newX, newY]
ans =
2 2
4 2
4 3
4 4
2 4
2 2 % In the reduced result the first/last coords also match
If you want to turn this reduced polygon back into a mask that was the same size as the original BW, you can just use poly2mask() as follows:
newBW = poly2mask(newX,newY,size(BW,1),size(BW,2)) - BW
  댓글 수: 3
Sven
Sven 2014년 4월 26일
Ok, I've updated the answer with a call to poly2mask. Note that the end result ( newBW ) may have tiny pieces near the corners different (from your input) by up to 1 pixel as explained here.
Sukuchha
Sukuchha 2014년 4월 27일
Hi sven, Thank you very much your kind help, Poly2mask function is what i am missing. One last questions, can poly2msk can handle blobs with the holes as well. Some of my buildings have a rectangualar shape with a hole (courtyard), in this case how poly2mask works with reduced number of points given by douglas pecker algo. I will try and see if this works in the case i mentioned, and let you know tommorrow.
Thanks a heap.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Sven
Sven 2014년 5월 3일
Hi Sukuchha, please see this blog post or this file exchange entry. It does exactly what you're looking for.
  댓글 수: 1
Image Analyst
Image Analyst 2014년 5월 3일
Sukuchha, why do you want to simply the boundary, or represent it with fewer vertices, anyway? For what purpose do you want to do that? Why not just use the full resolution data?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Biomedical Imaging에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by