How to resample an edge of an image in MATLAB? -
How to resample an edge of an image in MATLAB? -
i trying decrease number of points of detected border of image didn't obtain result. want result contain 200 pixels of edges, points must chosen shape remain clear. how can this?
here's illustration image of i'm working with:
here results have received code wrote:
code writtenfunction y = echantillonnage(x) contour_image = x; [lignes,colonnes]=size(x); n = nombre/200; contour_image_200px = contour_image; ok=0; i=1:lignes j=1:colonnes if (contour_image_200px(i,j)>0 ) ok=ok+1; if ( mod(ok,round(n))>0 ) contour_image_200px(i,j)=0; end end end end figure,imshow(contour_image_200px); %résultat y = contour_image_200px; end
what can utilize bwboundaries
trace boundaries of objects / edges sample array of points decrease number of border points. tracing done in clockwise order, sure when subsample array, semblance of order. however, bwboundaries
returns both outer , inner contour boundaries, need amount of traces output. talk later.
bwboundaries
works multiple objects, you'd have iterate through each object, sample border points , write output result. note using bwboundaries
doesn't require edges found... long object clean isn't necessary. however, i'm not sure purpose of you're doing, let's operate on border detected result.
let's had next example:
>> = false(256, 256); >> a(100:170,100:170) = true; >> a(3:40,3:40) = true; >> a(190:220,200:230) = true; >> imshow(a)
we image:
if performed border detection:
>> b = edge(a, 'canny'); >> imshow(b);
we this:
now, if want subsampling, phone call bwboundaries
way:
[bound,l,n] = bwboundaries(b);
bwboundaries
returns cell array bound
of boundaries each cell n x 2
array of spatial coordinates define boundary. first column row locations , sec column column locations of boundary points. l
label matrix tells point each boundary belongs to. don't need purposes might talk it. n
of import parameter. defines how many object boundaries there are. tells first n
cells of bound
tells belong outer object boundaries.
as such, can next subsample border points , set them new matrix, assuming border image stored in b
. also, stated want have 200 points per edge. let's define parameter num_edge_points
. however, if have edges less amount, assume you'll want have of border points selected.
out = false(size(b)); %// initialize output image num_edge_points = 200; %// define number of border points %// each object boundary idx = 1 : n boundary = bound{idx}; %// boundary %// determine how many points have num_pts = size(boundary,1); %// generate indices sampling boundary %// if there less minimum, take amount if num_pts < num_edge_points ind = 1:num_pts; else ind = floor(linspace(1,num_pts,num_edge_points)); end %// subsample border points pts = boundary(ind,:); %// mark points in output out(sub2ind(size(b), pts(:,1), pts(:,2))) = true; end
out
contain border image subsampled. illustrate got right, let's create new rgb image have edges , subsampled border points on top of each other subsampled edges in red:
out_red = 255*uint8(b); out_greenblue = out_red; out_greenblue(out) = 0; out_rgb = cat(3, out_red, out_greenblue, out_greenblue); imshow(out_rgb);
this (zoomed-in):
as can see, top , bottom rectangles have total border points show there less 200 border points. however, 1 in middle sparsely sampled there more 200 border points, displaying 200 of them.
if function help facilitate this, can utilize following. i've copied of code above , input binary image edges , output binary image subsampled edges:
function [out] = subsample_edge(b) %// obtain boundaries border image [bound,l,n] = bwboundaries(b); out = false(size(b)); %// initialize output image num_edge_points = 200; %// define number of border points %// each object boundary idx = 1 : n boundary = bound{idx}; %// boundary %// determine how many points have num_pts = size(boundary,1); %// generate indices sampling boundary %// if there less minimum, take amount if num_pts < num_edge_points ind = 1:num_pts; else ind = floor(linspace(1,num_pts,num_edge_points)); end %// subsample border points pts = boundary(ind,:); %// mark points in output out(sub2ind(size(b), pts(:,1), pts(:,2))) = true; end end
if want phone call function, do:
out = subsample_edge(b);
image matlab image-processing edge-detection
Comments
Post a Comment