问题
I have a number plate which is a binary image.
I performed dilation to the image to thicken the edges then "flood filling", lastly erosion for thinning:
But i want my output to be like this:
Can anyone help me, please? And show me how to get the desired output.
ab=imread('test1.png');
level=graythresh(ab);
ab=im2bw(ab,level);
se=strel('disk',1);
ab=imdilate(ab,se);
ab=imfill(ab,'holes');
ab=bwmorph(ab,'thin',1);
ab=imerode(ab,strel('line',3,90));
figure();imshow(ab,[]); title('floodFilling');
回答1:
You can do this with a few other clever calls to imfill
. Here is a way, assuming your binary image is in the array BW
:
Tmp = imfill(BW, 'holes');
Tmp2 = imfill(Tmp-BW, 'holes');
Res = Tmp - imfill(BW & Tmp2, 'holes');
and Res
is a binary image that contains the desired output:
回答2:
Text is just single layer fill so I do it in C++ like this:
int x,y,e;
int c0=0x00000000; // space color
int c1=0x00FFFFFF; // edge color
int co=0x00FF0000; // outside tmp color
int ci=0x000000FF; // inside tmp color
// grow/flood fill c0 neigbouring c1 with c2
#define fill(c0,c1,c2)\
for (e=1;e;)\
for (e=0,y=1;y<pic1.ys-1;y++)\
for ( x=1;x<pic1.xs-1;x++)\
if (pic1.p[y][x].dd==c0)\
if ((pic1.p[y-1][x].dd==c1)\
||(pic1.p[y+1][x].dd==c1)\
||(pic1.p[y][x-1].dd==c1)\
||(pic1.p[y][x+1].dd==c1)) { e=1; pic1.p[y][x].dd=c2; }
// copy data pic0 is source pic1 is output
pic1=pic0;
// 0. draw border rectangle for growth fill start with co
for (x=0 ,y=0;y<pic1.ys;y++) pic1.p[y][x].dd=co;
for (x=pic1.xs-1,y=0;y<pic1.ys;y++) pic1.p[y][x].dd=co;
for (x=0,y=0 ;x<pic1.xs;x++) pic1.p[y][x].dd=co;
for (x=0,y=pic1.ys-1;x<pic1.xs;x++) pic1.p[y][x].dd=co;
fill(c0,co,co); // 1. grow outer border
fill(c1,co,co); // 2. grow outer edges
fill(c0,co,ci); // 3. create outer fill edge
fill(c0,ci,ci); // 4. fill the inside
// 5. merge / recolor
for (y=0;y<pic1.ys;y++)
for (x=0;x<pic1.xs;x++)
{
e=c0;
if ((pic0.p[y][x].dd==c1)||(pic1.p[y][x].dd==ci)) e=c1;
pic1.p[y][x].dd=e;
}
#undef fill
Here are how the stages looks like:
Algorithm is like this:
- draw border rectangle for growth fill start with
co
(some unused color) - grow outer border to nearest text edges with
co
- fill outer edges (they disappear) with
co
- create outer fill edge with
ci
(some unused color) - fill the inside with
ci
merge / recolor source and result images
copy edges from source image and inside from result image the rest is space
I use my own picture class for images so some members are:
xs,ys
size of image in pixels
p[y][x].dd
is pixel at (x,y) position as 32 bit integer type
clear(color)
- clears entire image
resize(xs,ys)
- resizes image to new resolution
If you have special characters that have more layers
then just add more stages ... layer
is how many layers of space there can be from most inner space/hole to the outer space also you can loop until no more layer found ...
回答3:
let use this code it will use for you . thanks to @ spektre .
ab=imread('test1.png');
level=graythresh(ab);
ab=im2bw(ab,level);
se=strel('disk',1);
ab=imdilate(ab,se);
figure();imshow(ab,[]); title('floodFilling');
ab1=imfill(ab,'holes');
ab1(ab==1)=0;
figure();imshow(ab1,[]); title('floodFilling');
来源:https://stackoverflow.com/questions/29411250/flood-filling-for-number-plate-recognition