1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
| Graphics::TBitmap* __fastcall Hough(Graphics::TBitmap *bitmap)
{
double theta1,p;
int max=30;
cellule **acc;
acc = new cellule*[183];
for(int i=0;i<183;i++) {
acc[i] = new cellule[361];
}
for(int i=0;i<183;i++)
{
for(int j=0;j<361;j++)
{
acc[i][j].vote=0;
acc[i][j].x1=255;
acc[i][j].x2=0;
}
}
Graphics::TBitmap *bitmap2=new Graphics::TBitmap;
bitmap2->LoadFromFile("E:\\noir.bmp"); //charger une image noir
int xPrime,yPrime;
int theta,rho;
for(int x=0;x<bitmap->Width;x++)
{
for(int y=0;y<bitmap->Height;y++)
{
if(bitmap->Canvas->Pixels[x][y]==clWhite)
{
xPrime=x-128;
yPrime=128-y;
for(int i=0; i<361; i+=1)
{
theta1=i*Pi/180;
p =(int)floor((xPrime*cos(theta1) + yPrime*sin(theta1))+0.5);
if(p<0)
{
rho=(int)-p;
theta=(i+180)%360;
}
else
{
rho=(int)p;
theta=i;
}
acc[rho][i].vote++;
if(acc[rho][i].x1>x)
{
acc[rho][i].x1=x;
acc[rho][i].y1=y;
}
if(acc[rho][i].x2<x)
{
acc[rho][i].x2=x;
acc[rho][i].y2=y;
}
}
}
}
}
int winRho=0,winTheta=0;
for(int i=0;i<183;i++)
{
for(int j=0;j<361;j++)
{
if(acc[i][j].vote>max)
{
int longueur=acc[i][j].vote;
winRho=i;
winTheta=j;
//---debut
int pointX1,pointX2,pointY1,pointY2;
pointX1=acc[winRho][winTheta].x1;
pointX2=acc[winRho][winTheta].x2;
pointY1=acc[winRho][winTheta].y1;
pointY2=acc[winRho][winTheta].y2;
if(pointX1==pointX2)
{
pointY2=pointY1+longueur;
}
bitmap2->Canvas->Pen->Color=clWhite;
bitmap2->Canvas->MoveTo(pointX1,pointY1);
bitmap2->Canvas->LineTo(pointX2,pointY2);
//------fin-----
}
}
}
Graphics::TBitmap *bitmapUnion=new Graphics::TBitmap;
bitmapUnion=UnionImage(bitmap,bitmap2);
return bitmapUnion;
delete bitmap2,bitmapUnion;
for(int i=0;i<183;i++) // et non for(i=0;i<m_TailleCode;i++)
{
delete acc[i]; // et non delete [] Matrice[i];
}
delete [] acc;
}
Graphics::TBitmap* __fastcall UnionImage(Graphics::TBitmap *bitmap,Graphics::TBitmap *bitmap2)
{
Graphics::TBitmap *bitmapUnion=new Graphics::TBitmap;
bitmapUnion->Width=bitmap->Width;
bitmapUnion->Height=bitmap->Height;
for(int i=0;i<bitmap->Width;i++)
{
for(int j=0;j<bitmap->Height;j++)
{
if(bitmap->Canvas->Pixels[i][j]==clWhite && bitmap2->Canvas->Pixels[i][j]==clWhite)
bitmapUnion->Canvas->Pixels[i][j]=clFuchsia;
else
bitmapUnion->Canvas->Pixels[i][j]=clBlack;
}
}
return bitmapUnion;
delete bitmapUnion;
} |