Question

I have a binary image, and for simplicity lets assume it's a simple straight line:

00000000000000
00000000000000
00000000000000
11111111110000
00000000000000
00000000000000
00000000000000

and I want to extend it for 2px.

For that purpose I applied dilation with two iterations and as expected got this:

00000000000000
11111111111100
11111111111100
11111111111100
11111111111100
11111111111100
00000000000000

then, my idea was to simply apply thinning and get initial line extended by two pixels. But instead, I got shortened line, probably because of line thickness:

00000000000000
10000000000000
10000000000000
11111111100000
10000000000000
10000000000000
00000000000000

I then thought to apply dilation with diamond shape structuring footprint instead 3x3 ones(), but result was worse - line was shortened for one more pixel.

For thinning I used Zhang-Suen algorithm and Guo-Hall algorithm, and I also tried Otsu method exposed by skimage, which yielded similar result.

Can someone tell me about existence of thinning algorithm that can get me where I headed, or maybe different approach?


Update (to address @marinus comment):

As mentioned, I like to extend the initial line for 2px and get:

00000000000000
00000000000000
00000000000000
11111111111100
00000000000000
00000000000000
00000000000000

while I can't with my thinning approach, because thinning seems to work this way:

img

So by just dilating initial line, I can't possibly extend it with thinning.

Was it helpful?

Solution

The answer lies in the morphological kernel that you are using. In your examples you mention that you simply want to extend the line two pixels in the horizontal direction. The kernel you apply should then match your goal.

For dialtion, every "on" pixel is 'or'd the current pixel and its neighbors with the kernel. The 3x3 kernel will produce 1's all around the current pixel.

3x3:

111
111
111

If instead you applied a kernel that was 5x1:

00000
11111
00000

Applying it to your original signal you would get your desired result. If you only want pixels added to the right, do the kernel of 111 with the center of the kernel being on the first pixel.

OTHER TIPS

Zhang-Suen thinning algorithm in javascript.js

for(x=0;x<5;x++){
for(n=0;n<2;n++){
for(i=0;i<len;){
p1=pp[i];p2=pp[i-width];p3=pp[i-width+1];
p8=pp[i-1];p9=pp[i-width-1];p4=pp[i+1];
p7=pp[i+width-1];p6=pp[i+width];p5=pp[i+width+1];
/* pixels position  
9   2   3
8   1   4   
7   6   5


*/
zn=0;
b=p2+p3+p4+p5+p6+p7+p8+p9;
if(p2<p3){zn++;}
if(p3<p4){zn++;}
if(p4<p5){zn++;}
if(p5<p6){zn++;}
if(p6<p7){zn++;}
if(p7<p8){zn++;}
if(p8<p9){zn++;}
if(p9<p2){zn++;}
if(b>=2&&b<=6&&zn==1&&p2*p4*p6==0&&p4*p6*p8==0&&n==0){id.push(i);}
if(b>=2&&b<=6&&zn==1&&p2*p4*p8==0&&p2*p6*p8==0&&n==1){id.push(i);}
i++;}
for(i=0;i<id.length;i++){pp[id[i]]=0;}id=[];
}}

result :// https://lh6.googleusercontent.com/-rtDafT3tJ-Q/UqT6thazqJI/AAAAAAAABPE/b2dc7PQNZ24/w973-h220-no/Screenshot+from+2013-12-09+00%253A58%253A36.png

REFERENCES :

jsAscii - ASCII art from images with Javascript and Canvas convert image to dots http://blog.nihilogic.dk/2008/03/jsascii.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top