matlab - How to remove that digit/non character which is at farthest distance from remaining characters in binary image -
i want remove digit/non character @ farthest distance remaining characters in binary image. renaming characters , digits have fixed distance. in image:
i want remove "5" , in image:
i want remove non-characters.
i want when image have 2 rows (like image1 , image2). in single row image don't want anything.
couldn't quite figure out mark doing, decided create own method. if have statistics toolbox, i'd apply kmeans
clustering of centroids of characters , remove centroid has least amount of characters - namely isolated island 1 character.
given first image, had pre-processing remove green bounding box surrounded each character. when read in image stackoverflow, had associated colour map , read in indexed image. had convert indexed image colour image, extracted out green channel light green bounding box removed.
[x,map] = imread('http://s17.postimg.org/5quw8e6m7/plate2.png'); out = ind2rgb(x,map); im = out(:,:,2);
i invert image , centroids of each character. inverting required because object pixels white in matlab, black in image. i'll bounding boxes you'll need later when want remove single character:
im_invert = ~im; s = regionprops(im_invert, 'centroid', 'boundingbox'); centroids = reshape([s.centroid], 2, []).';
you should aware of regionprops
, because used in 1 of previous posts. measure blob properties each detected blob in image. blob in our case character. now, apply k
-means clustering on centroids. have extract out centroid each character, unpack of centroids in regionprops
structure comma-separated list , reshape array each row centroid , each column dimension. first column x
/column coordinate second column y
/row coordinate.
given images, i'm going assume 3 centroids - 1 of them consists of first row of characters, 1 consists of second row of characters, , last 1 has isolated one. i'm going run few times sure right centroids, we'll @ centroid contains 1 member , remove it:
[idx,c] = kmeans(centroids, 3, 'replicates', 10);
idx
consists of cluster membership id particular centroid belongs , c
representative centroid collects of characters belong cluster together.
when run kmeans
, get:
>> idx idx = 3 1 3 1 3 1 3 2 >> c c = 178.7152 74.3279 310.7455 67.1364 191.5493 149.3756
ok, looks cluster #2 has isolated centroid. of other ones assigned either cluster #1 or cluster #3. need figure out cluster has single centroid, , can tallying how many characters belong each cluster , isolating out 1 has single member. can use histc
or histcounts
that:
cnts = histc(idx, 1:3); cnt_one = find(cnts == 1); one_idx = find(idx == cnt_one);
we first count how many characters belonged each cluster, determine of cluster ids has single member. that's stored in cnt_one
. once find cluster id, figure out character isolated cluster. one_idx
contains detected index of isolated character. such, we're going use character's 'boundingbox
' property set region white can remove character:
s_remove = floor(s(one_idx).boundingbox); im(s_remove(2):s_remove(2)+s_remove(4), s_remove(1):s_remove(1)+s_remove(3)) = 1;
s_remove
contains bounding box information of isolated character. 4 element array first 2 elements x
/column coordinate , y
/row coordinate of top-left corner of bounding box, , third , fourth element width , height of bounding box respectively. need take floor
because can potentially have floating point values , if want index image, need remove decimal places.
removing decimal places, set bounding box spanned above information 1s, removing character image.
showing new image gives us:
imshow(im);
let's repeat second image. there's no colour map associated this, can use straight imread
:
im = imread('http://s10.postimg.org/4ww3bjia1/plate.png'); im_invert = ~im;
you can use same code wrote above , get:
to reproduce have, here's code can copy , paste matlab , run it:
%// image #1 [x,map] = imread('http://s17.postimg.org/5quw8e6m7/plate2.png'); out = ind2rgb(x,map); im = out(:,:,2); %// image #2 %im = imread('http://s10.postimg.org/4ww3bjia1/plate.png'); %// invert image, find centroids of each character , bounding boxes im_invert = ~im; s = regionprops(im_invert, 'centroid', 'boundingbox'); centroids = reshape([s.centroid], 2, []).'; %' %// find clustering memberships of each character [idx,c] = kmeans(centroids, 3, 'replicates', 10); %// count how many characters belonged each cluster cnts = histc(idx, 1:3); %// figure out cluster id gave 1 cluster cnt_one = find(cnts == 1); %// find character gave 1 cluster one_idx = find(idx == cnt_one); %// access character's boundingbox property s_remove = floor(s(one_idx).boundingbox); %// fill in image @ bounding box location white remove im(s_remove(2):s_remove(2)+s_remove(4), s_remove(1):s_remove(1)+s_remove(3)) = 1; %// show image imshow(im);
Comments
Post a Comment