julianluthor
Programmer
hi..
i have image edge detection function in button 2 and thinning function in button 3.how can i make so that my thinning function in button 3 can continue from button 2 (so it can thinning the edge image not the original image) without combine it in one button?below is my code..
i have image edge detection function in button 2 and thinning function in button 3.how can i make so that my thinning function in button 3 can continue from button 2 (so it can thinning the edge image not the original image) without combine it in one button?below is my code..
Code:
void __fastcall TForm1::Button2Click(TObject *Sender)
{
int n,m;
long max,min;
max = min = Data[0][0];
int DataWidth = Image1->Picture->Width;
int DataHeight = Image1->Picture->Height;
for (int y=0; y<DataHeight; y++)
for (int x=0; x<DataWidth; x++)
temp[y][x] = (Byte)Image1->Canvas->Pixels[x][y];
for (int j=0; j<400; j++)
for (int i=0; i<400; i++)
Data[i][j] = temp[i][j];
for(int j=0;j<400;j++)
for(int i=0;i<400;i++)
{
m=Data[j-1][i-1]+2*Data[j-1][i]+Data[j-1][i+1]-(Data[j+1][i-1]+2*Data[j+1][i]+Data[j+1][i+1]);
n=Data[j-1][i+1]+2*Data[j][i+1]+Data[j+1][i+1]-(Data[j-1][i-1]+2*Data[j][i-1]+Data[j+1][i-1]);
dataconv[j][i] = abs(m) + abs(n);
if(dataconv[j][i]>max)
max=dataconv[j][i];
if(dataconv[j][i]<min)
min=dataconv[j][i];
}
for(int j=0;j<400;j++)
for(int i=0;i<400;i++)
Data[i][j]=(unsigned char)(((float)dataconv[j][i]-min)/(max-min)*255);
TStringList* papar = new TStringList;
AnsiString str_line;
for(int j=0; j<400; j++)
{
str_line = "";
for(int i=0; i<400; i++)
{
if(i)
str_line += ", ";
temp[i][j]= Data[i][j];
if (temp[i][j]>127)
{
temp[i][j] = 0;
}
else
{
temp[i][j] = 1;
}
str_line += IntToStr(temp[i][j]);
}
papar->Add(str_line);
}
papar->SaveToFile("EgdeDetection.dat");
delete papar;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
int count,count2,i,j,a,b;
int num_transitions ;
int num_transitions2 ;
int temp_array[400][400];
int DataWidth = Image1->Picture->Width;
int DataHeight = Image1->Picture->Height;
for (int y=0; y<DataHeight; y++)
for (int x=0; x<DataWidth; x++)
Data[y][x] = (Byte)Image1->Canvas->Pixels[x][y];
TStringList* display = new TStringList;
AnsiString str_line;
for(j=0; j<400; j++)
{
str_line = "";
for(i=0; i<400; i++)
{
if(i)
str_line += " ";
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]);
display->Add(str_line);
count = 0;
num_transitions = 0;
// check for N(p)
if (Data[i][j] == 1)
{
if (Data[i-1][j-1] != 0)
count++;
if (Data[i][j-1] != 0)
count++;
if (Data[i+1][j-1] != 0)
count++;
if (Data[i+1][j] != 0)
count++;
if (Data[i-1][j] != 0)
count++;
if (Data[i+1][j+1] != 0)
count++;
if (Data[i][j+1] != 0)
count++;
if (Data[i-1][j+1] != 0)
count++;
if (count != 8)
{
// 2 <= N(p) <= 6
if (count >= 2 && count <= 6)
{
if(Data[i-1][j] == 0 &&
Data[i-1][j+1] == 1)
num_transitions++ ;
if(Data[i-1][j+1] == 0 &&
Data[i][j+1] == 1)
num_transitions++ ;
if(Data[i][j+1] == 0 &&
Data[i+1][j+1] == 1)
num_transitions++ ;
if(Data[i+1][j+1] == 0 &&
Data[i+1][j] == 1)
num_transitions++ ;
if(Data[i+1][j] == 0 &&
Data[i+1][j-1] == 1)
num_transitions++ ;
if(Data[i+1][j-1] == 0 &&
Data[i][j-1] == 1)
num_transitions++ ;
if(Data[i][j-1] == 0 &&
Data[i-1][j-1] == 1)
num_transitions++ ;
if(Data[i-1][j-1] == 0 &&
Data[i-1][j] == 1)
num_transitions++ ;
//S(p) = 1
if (num_transitions == 1)
{
// if p2 * p4 * p6 = 0
if (Data[i][j-1] == 0 ||
Data[i][j+1] == 0 ||
Data[i+1][j] == 0)
{
// if p4 * p6 * p8 = 0
if(Data[i][j+1] == 0 ||
Data[i+1][j] == 0 ||
Data[i][j-1] == 0)
temp_array[i][j] = 0;
else
temp_array[i][j] = 1;
}
else
temp_array[i][j] = 1;
}
else
temp_array[i][j] = 1;
}
else
temp_array[i][j] = 1;
}
else
temp_array[i][j] = 1;
}
else
temp_array[i][j] =0;
}}
//copy thinned image back to original
for(j=0; j<400; j++)
for(i=0; i<400; i++)
Data[i][j]=temp_array[i][j];
for(j=0; j<400; j++) {
for(i=0; i<400; i++)
{
count2 = 0;
num_transitions2 = 0;
// check for N(p)
if (Data[i][j] == 1)
{
if (Data[i-1][j-1] != 0)
count2++;
if (Data[i][j-1] != 0)
count2++;
if (Data[i+1][j-1] != 0)
count2++;
if (Data[i+1][j] != 0)
count2++;
if (Data[i-1][j] != 0)
count2++;
if (Data[i+1][j+1] != 0)
count2++;
if (Data[i][j+1] != 0)
count2++;
if (Data[i-1][j+1] != 0)
count2++;
if (count2 != 8)
{
// 2 <= N(p) <= 6
if (count2 >= 2 && count2 <= 6)
{
if(Data[i-1][j] == 0 &&
Data[i-1][j+1] == 1)
num_transitions2++ ;
if(Data[i-1][j+1] == 0 &&
Data[i][j+1] == 1)
num_transitions2++ ;
if(Data[i][j+1] == 0 &&
Data[i+1][j+1] == 1)
num_transitions2++ ;
if(Data[i+1][j+1] == 0 &&
Data[i+1][j] == 1)
num_transitions2++ ;
if(Data[i+1][j] == 0 &&
Data[i+1][j-1] == 1)
num_transitions2++ ;
if(Data[i+1][j-1] == 0 &&
Data[i][j-1] == 1)
num_transitions2++ ;
if(Data[i][j-1] == 0 &&
Data[i-1][j-1] == 1)
num_transitions2++ ;
if(Data[i-1][j-1] == 0 &&
Data[i-1][j] == 1)
num_transitions2++ ;
//S(p) = 1
if (num_transitions2 == 1)
{
// if p2 * p4 * p8 = 0
if (Data[i-1][j] == 0 ||
Data[i][j+1] == 0 ||
Data[i][j-1] == 0)
{
// if p2 * p6 * p8 = 0
if(Data[i-1][j] == 0 ||
Data[i+1][j] == 0 ||
Data[i][j-1] == 0)
temp_array[i][j] = 0;
else
temp_array[i][j] = 1;
}
else
temp_array[i][j] = 1;
}
else
temp_array[i][j] = 1;
}
else
temp_array[i][j] = 1;
}
else
temp_array[i][j] = 1;
}
else
temp_array[i][j] =0;
}}
//copy thinned image back to original
for(j=0; j<400; j++)
for(i=0; i<400; i++)
Data[i][j]=temp_array[i][j];
TStringList* display2 = new TStringList;
AnsiString str_line2;
for(int b=0; b<400; b++)
{
str_line2 = "";
for(int a=0; a<400; a++)
{
if(i)
str_line2 += " , ";
Data[a][b]= temp_array[a][b];
str_line2 += IntToStr(Data[a][b]);
}
display2->Add(str_line2);
}
display2->SaveToFile("Thinning.dat");
delete display2;
}