Auto fill an image based on edge line
4 views (last 30 days)
Show older comments
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 Comment
Image Analyst
on 30 Jun 2012
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.
Accepted Answer
Image Analyst
on 1 Jul 2012
Edited: Image Analyst
on 1 Jul 2012
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.
More Answers (1)
Walter Roberson
on 30 Jun 2012
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 Comments
Anton Semechko
on 30 Jun 2012
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
on 30 Jun 2012
Thanks, Anton -- I probably would have missed that possibility, as I have rarely used 4 connectivity.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!