Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Zhang Suen Algorithm

Status
Not open for further replies.

julianluthor

Programmer
Nov 3, 2003
18
0
0
MY
i'm trying to use Zhang Suen thinning algorithm to thin my image but the result is the same before i use the thinning algorithm.can somebody help?

Code:
void __fastcall TForm1::Button2Click(TObject *Sender) 
{ 
//THINNING 
int count,count2,x,y; 
int num_transitions ; 
int transitions ; 
int tempimage[400][400]; 
int x1,y1; 
int DataWidth = Image1->Picture->Width; 
int DataHeight = Image1->Picture->Height; 
for(x = 1; x < DataHeight-1 ; x++) 

{ 

for(y = 1; y < DataWidth-1 ; y++) 

{ 

count = 0; 

num_transitions = 0; 

// check for N(p) 

if ((Byte)Image1->Canvas->Pixels[y][x] == 255) 

{ 

if ((Byte)Image1->Canvas->Pixels[y-1][x-1] != 0) 

count++; 

if ((Byte)Image1->Canvas->Pixels[y][x-1] != 0) 

count++; 

if ((Byte)Image1->Canvas->Pixels[y+1][x-1] != 0) 

count++; 

if ((Byte)Image1->Canvas->Pixels[y+1][x] != 0) 

count++; 

if ((Byte)Image1->Canvas->Pixels[y-1][x] != 0) 

count++; 

if ((Byte)Image1->Canvas->Pixels[y+1][x+1] != 0) 

count++; 

if ((Byte)Image1->Canvas->Pixels[y][x+1] != 0) 

count++; 

if ((Byte)Image1->Canvas->Pixels[y-1][x+1] != 0) 

count++; 

if (count != 8) 

{ 

// 2 <= N(p) <= 6 

if (count >= 2 && count <= 6) 

{ 

if((Byte)Image1->Canvas->Pixels[y-1][x] == 0 && 

(Byte)Image1->Canvas->Pixels[y-1][x+1] == 255) 

num_transitions++ ; 

if((Byte)Image1->Canvas->Pixels[y-1][x+1] == 0 && 

(Byte)Image1->Canvas->Pixels[y][x+1] == 255) 

num_transitions++ ; 

if((Byte)Image1->Canvas->Pixels[y][x+1] == 0 && 

(Byte)Image1->Canvas->Pixels[y+1][x+1] == 255) 

num_transitions++ ; 

if((Byte)Image1->Canvas->Pixels[y+1][x+1] == 0 && 

(Byte)Image1->Canvas->Pixels[y+1][x] == 255) 

num_transitions++ ; 

if((Byte)Image1->Canvas->Pixels[y+1][x] == 0 && 

(Byte)Image1->Canvas->Pixels[y+1][x-1] == 255) 

num_transitions++ ; 

if((Byte)Image1->Canvas->Pixels[y+1][x-1] == 0 && 

(Byte)Image1->Canvas->Pixels[y][x-1] == 255) 

num_transitions++ ; 

if((Byte)Image1->Canvas->Pixels[y][x-1] == 0 && 

(Byte)Image1->Canvas->Pixels[y-1][x-1] == 255) 

num_transitions++ ; 

if((Byte)Image1->Canvas->Pixels[y-1][x-1] == 0 && 

(Byte)Image1->Canvas->Pixels[y-1][x] == 255) 

num_transitions++ ; 

//S(p) = 1 

if (num_transitions == 1) 

{ 

// if p2 * p4 * p6 = 0 

if((Byte)Image1->Canvas->Pixels[y-1][x] == 0 || 

(Byte)Image1->Canvas->Pixels[y][x+1] == 0 || 

(Byte)Image1->Canvas->Pixels[y+1][x] == 0) 

{ 

// if p4 * p6 * p8 = 0 

if((Byte)Image1->Canvas->Pixels[y][x+1] == 0 || 

(Byte)Image1->Canvas->Pixels[y+1][x] == 0 || 

(Byte)Image1->Canvas->Pixels[y][x-1] == 0) 

tempimage[y][x] = 0; 

else 

tempimage[y][x] = 255; 

} 

else 

tempimage[y][x] = 255; 

} 

else 

tempimage[y][x] = 255; 

} 

else 

tempimage[y][x] = 255; 

} 

else 

tempimage[y][x] = 255; 

} 

else 

tempimage[y][x] =0;//255; 

} 

} 

//copy thinned image back to original 

for(x = 0; x < DataHeight ; x++) 

for(y = 0; y < DataWidth; y++) 

tempimage[y][x]=(Byte)Image1->Canvas->Pixels[y][x]; 

// step 2 of the thinning algorithm 

for(x1 = 1; x1 < DataHeight-1 ; x1++) 

{ 

for(y1 = 1; y1 < DataWidth-1 ; y1++) 

{ 

count2 = 0; 

transitions = 0; 

if ((Byte)Image1->Canvas->Pixels[y1][x1] == 255) 

{ 

if ((Byte)Image1->Canvas->Pixels[y1-1][x1-1] != 0) 

count2++; 

if ((Byte)Image1->Canvas->Pixels[y1][x1-1] != 0) 

count2++; 

if ((Byte)Image1->Canvas->Pixels[y1+1][x1-1] != 0) 

count2++; 

if ((Byte)Image1->Canvas->Pixels[y1+1][x1] != 0) 

count2++; 

if ((Byte)Image1->Canvas->Pixels[y1-1][x1] != 0) 

count2++; 

if ((Byte)Image1->Canvas->Pixels[y1+1][x1+1] != 0) 

count2++; 

if ((Byte)Image1->Canvas->Pixels[y1][x1+1] != 0) 

count2++; 

if ((Byte)Image1->Canvas->Pixels[y1-1][x1+1] != 0) 

count2++; 

if (count2 != 8) 

{ 

if (count2 >= 2 && count2 <= 6) 

{ 

if((Byte)Image1->Canvas->Pixels[y1-1][x1] == 0 && 

(Byte)Image1->Canvas->Pixels[y1-1][x1+1] == 255) 

transitions++ ; 

if((Byte)Image1->Canvas->Pixels[y1-1][x1+1] == 0 && 

(Byte)Image1->Canvas->Pixels[y1][x1+1] == 255) 

transitions++ ; 

if((Byte)Image1->Canvas->Pixels[y1][x1+1] == 0 && 

(Byte)Image1->Canvas->Pixels[y1+1][x1+1] == 255) 

transitions++ ; 

if((Byte)Image1->Canvas->Pixels[y1+1][x1+1] == 0 && 

(Byte)Image1->Canvas->Pixels[y1+1][x1] == 255) 

transitions++ ; 

if((Byte)Image1->Canvas->Pixels[y1+1][x1] == 0 && 

(Byte)Image1->Canvas->Pixels[y1+1][x1-1] == 255) 

transitions++ ; 

if((Byte)Image1->Canvas->Pixels[y1+1][x1-1] == 0 && 

(Byte)Image1->Canvas->Pixels[y1][x1-1] == 255) 

transitions++ ; 

if((Byte)Image1->Canvas->Pixels[y1][x1-1] == 0 && 

(Byte)Image1->Canvas->Pixels[y1-1][x1-1] == 255) 

transitions++ ; 

if((Byte)Image1->Canvas->Pixels[y1-1][x1-1] == 0 && 

(Byte)Image1->Canvas->Pixels[y1-1][x1] == 255) 

transitions++ ; 

if (transitions == 1) 

{ 

// if p2 * p4 * p8 = 0 

if((Byte)Image1->Canvas->Pixels[y1-1][x1] == 0 || 

(Byte)Image1->Canvas->Pixels[y1][x1+1] == 0 || 

(Byte)Image1->Canvas->Pixels[y1][x1-1] == 0) 

{ 

// if p2 * p4 * p6 

if((Byte)Image1->Canvas->Pixels[y1-1][x1] == 0 || 

(Byte)Image1->Canvas->Pixels[y1+1][x1] == 0 || 

(Byte)Image1->Canvas->Pixels[y1][x1-1] == 0) 

tempimage[y1][x1] = 0; 

else 

tempimage[y1][x1] = 255; 

} 

else 

tempimage[y1][x1] = 255; 

} 

else 

tempimage[y1][x1] = 255; 

} 

else 

tempimage[y1][x1] = 255; 

} 

else 

tempimage[y1][x1] = 255; 

} 

else 

tempimage[y1][x1] = 0; 

} 
} 

for(x1 = 0; x1 < DataHeight ; x1++) 

for(y1 = 0; y1 < DataWidth; y1++) 

tempimage[y1][x1]=(Byte)Image1->Canvas->Pixels[y1][x1]; 

tempimage[y][x] = tempimage[y1][x1]; 

TStringList* papar = new TStringList; 

AnsiString str_line; 

for(int j=0; j<400; j++) 

{ 

str_line = &quot;&quot;; 

for(int i=0; i<400; i++) 

{ 

if(i) 

str_line += &quot;, &quot;; 

Data[i][j]= (Byte)Image1->Canvas->Pixels[i][j]; 

if (Data[i][j]>127) 

{ 

Data[i][j] = 0; 

} 

else 

{ 

Data[i][j] = 1; 

} 

str_line += IntToStr(Data[i][j]); 

} 

papar->Add(str_line); 

} 

papar->SaveToFile(&quot;Julian.dat&quot;); 

delete papar; 

}
 
What 'thinning' is it that you want?

Totte
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top