Auto fill an image based on edge line

조회 수: 9 (최근 30일)
Philip
Philip 2012년 6월 30일
Is there a way to automatically fill an image (as though using the paint bucket tool in Photoshop) based on a line that runs from one side of the image to another?
For example, if I have the following image:
edge_win =
0 0 0 0 0
1 1 0 0 0
0 0 1 1 1
0 0 0 0 0
0 0 0 0 0
where the ones represent the line, is it possible to produce the following image:
filled_win =
2 2 2 2 2
1 1 2 2 2
0 0 1 1 1
0 0 0 0 0
0 0 0 0 0
The fill value can be any value other than zero or one. However, the original line (ones) may run either from left to right, or from top to bottom, so it may be difficult to do this using a loop...
Many thanks.
  댓글 수: 1
Image Analyst
Image Analyst 2012년 6월 30일
This is ambiguous. I do have a Photoshop-like magic wand demo if you want it. But, for your example, do you want all connected zero regions to have a distinct label (if so, just call bwlabel(~edge_win, 4)) or do you want only the upper left zero region to be 2 and don't touch any other zero regions (leave them as zero as in your example)? You can do it either ways, but you need to clarify. If you want just one particular region changed, then there are several ways to do it (labeling, region growing) depending on what information you're going to start with. Let me know if you still want my 79 line magic_wand.m demo.

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

채택된 답변

Image Analyst
Image Analyst 2012년 7월 1일
편집: Image Analyst 2012년 7월 1일
After you've read my comment above, see if this result, gotten via imreconstruct(), is what you want. (I know it gives exactly what you asked for but that doesn't mean it's what you want.)
% Create original matrix.
edge_win =[...
0 0 0 0 0
1 1 0 0 0
0 0 1 1 1
0 0 0 0 0
0 0 0 0 0]
% Create a binary image from edge_win.
binaryImage = ~logical(edge_win)
% Get a marker image to reconstruct just the connected region
% and not all the other disconnected regions.
binaryMarkerImage = false(size(edge_win));
% Specify the starting point - a marker.
row = 1;
column = 3;
% Reconstruct the image.
binaryMarkerImage(row, column) = true;
% Mark ONLY that region that has our marker
% at location 1,3 in it.
reconImage = imreconstruct(binaryMarkerImage, binaryImage, 4)
% Now the upper right 0 region is 1.
% Make it 2 and add it to edge_win to
% get the image Philip wanted.
filled_win = edge_win + 2 * reconImage
In the command window:
edge_win =
0 0 0 0 0
1 1 0 0 0
0 0 1 1 1
0 0 0 0 0
0 0 0 0 0
binaryImage =
1 1 1 1 1
0 0 1 1 1
1 1 0 0 0
1 1 1 1 1
1 1 1 1 1
reconImage =
1 1 1 1 1
0 0 1 1 1
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
filled_win =
2 2 2 2 2
1 1 2 2 2
0 0 1 1 1
0 0 0 0 0
0 0 0 0 0
Exactly how you requested it.
  댓글 수: 1
Philip
Philip 2012년 7월 1일
Perfect - Thanks so much!

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2012년 6월 30일
filled_win = 1 + bwlabel( ~edge_win );
This would fill one side of the line with 2's and the other side with 3's, and the line itself would be 1's.
  댓글 수: 4
Anton Semechko
Anton Semechko 2012년 6월 30일
Actually the solution is much easier. You just have to use pixel connectivity of 4 instead of 8, which is used by default. So for example:
a=[0 0 0 0 0
1 1 0 0 0
0 0 1 1 1
0 0 0 0 0
0 0 0 0 0];
b=1+bwlabel(~a,4)
b =
2 2 2 2 2
1 1 2 2 2
3 3 1 1 1
3 3 3 3 3
3 3 3 3 3
Walter Roberson
Walter Roberson 2012년 6월 30일
Thanks, Anton -- I probably would have missed that possibility, as I have rarely used 4 connectivity.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by