image - Create mask from bwtraceboundary in Matlab -


i'm trying create mask (or similar result) in order erase pieces of binary image not attached object surrounded boundary. saw thread (http://www.mathworks.com/matlabcentral/answers/120579-converting-boundary-to-mask) bwboundaries, i'm having trouble making suitable changes it. goal use code isolate part of topography map connected, , rid of pieces. need retain structure inside of bounded area, going use bwboundaries create additional boundary lines of main object's "interior" structure.

enter image description here

the following code first create single boundary line searching bottom left pixel of black area begin trace. looks first column of image isn't white , selects last black pixel. second section create inner boundary lines. note attempting 2 step process, if there way 1 i'd hear solution well. want boundaries main, large black area , holes inside of it, while getting rid of pieces hanging around.

 figname='images/booktrace_1';  bw = imread([figname,'.png']);     bw=im2bw(bw);     imshow(bw,[]);      j=1:size(bw,2)         if sum(bw(:,j))~=sum(bw(:,1))             corner=bw(:,j);             c=j-1;             break         end     end     r=find(corner==0);     r=r(end);      outline = bwtraceboundary(bw,[r c],'w',8,inf,'counterclockwise');     hold on;     plot(outline(:,2),outline(:,1),'g','linewidth',2);   [b,l] = bwboundaries(bw); hold on k = 1:length(b)     boundary = b{k};     plot(boundary(:,2), boundary(:,1), 'g', 'linewidth', 2) end 

any suggestions or tips appreciated. if there questions, please let me know , i'll update post. thank you!

edit: clarification, end goal in below image. need trace of outer , inner boundaries attached main object, while eliminating spare small pieces not attached it. enter image description here

it's simple. wouldn't use code above , use image processing toolbox instead. there's built-in function remove white pixels touch border of image. use imclearborder function.

the function return new binary image pixels touching borders of image removed. given code, it's simply:

out = imclearborder(bw); 

using above image example, i'm going threshold green lines removed... or rather merged other white pixels, , i'll call above function:

bw = imread('http://i.stack.imgur.com/jhlow.png'); %// read stackoverflow bw = im2bw(bw); %// convert binary out = imclearborder(bw); %// remove pixels along border imshow(out); %// show image 

we get:

enter image description here


if want opposite effect, want retain boundaries , remove else inside, create new image copying original 1 , use output above null these pixel locations.

out2 = bw; %// make copy out2(out) = 0; %// set pixels not belonging boundary 0 imshow(out2); %// show image 

we get:

enter image description here

edit

given above desired output, believe know want now. wish fill in holes each group of pixels , trace along boundary of desired result. fact have split 2 categories going useful. objects in interior, use imfill function , specify holes option fill in of black holes they're white. objects exterior, need bit of work. invert image pixels black become white , vice-versa, use bwareaopen function clear away pixels area below amount. remove small isolated black regions along border of exterior regions. once you're done, re-invert image. effect of small holes eliminated. chose threshold of 500 pixels area... seems work well.

therefore, using above variables reference, this:

%// fill holes both regions separately out_fill = imfill(out, 'holes'); out2_fill = ~bwareaopen(~out2, 500);  %// merge final_out = out_fill | out2_fill;  

this get:

enter image description here

if want nice green border in example illustrate point, can this:

perim = bwperim(final_out); red = final_out; green = final_out; blue = final_out; red(perim) = 0; blue(perim) = 0; out_colour = 255*uint8(cat(3, red, green, blue)); imshow(out_colour); 

the above code finds perimeter of objects, create new image red , blue channels along perimeter set 0, while setting green channel 255.

we this:

enter image description here

you can ignore green pixel border surrounds image. that's side effect way i'm finding perimeter along objects in image. in fact, image supplied me had white pixel border surrounds whole region, i'm not sure if that's intended or if that's part of whole grand scheme of things.


to consolidate working example can copy , paste matlab, here's of code in 1 code block:

%// pre-processing bw = imread('http://i.stack.imgur.com/jhlow.png'); %// read stackoverflow bw = im2bw(bw); %// convert binary  out = imclearborder(bw); %// remove pixels along border  %// obtain pixels along border out2 = bw; %// make copy out2(out) = 0; %// set pixels not belonging boundary 0  %// fill holes both regions separately out_fill = imfill(out, 'holes'); out2_fill = ~bwareaopen(~out2, 500);  %// merge final_out = out_fill | out2_fill;   %// show final output figure; imshow(final_out);  %// bonus - show perimeter of output in green perim = bwperim(final_out); red = final_out; green = final_out; blue = final_out; red(perim) = 0; blue(perim) = 0; out_colour = 255*uint8(cat(3, red, green, blue)); figure; imshow(out_colour); 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -