//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include"math.h"
#include "Unit1.h"
#define PI 3.1415926
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
int w=Image1->Width;
int h=Image1->Height;
int i,j;
int Dist2,k;
int Dist=ceil(sqrt(w*w+h*h));
int Alpha=180;// -------------
int (*a)[180];
a=new int[Dist][90*2]; //allocate memory dydamicly
for(j=0;j<Dist;j++)
for(i=0;i<Alpha;i++)
{
a[j][i]=0;
}
int sum1=Dist*Alpha;
MYLINE *TempLine ,*lpMyline,Maxline,Maxline2;
// TempLine=new MYLINE[sum1];//allocate memory dydamically!!
lpMyline=new MYLINE[sum1];
for(i=0;i<sum1;i++)
{
TempLine=(MYLINE*)(lpMyline+i); //??
(*TempLine).boty=32767;
}
//ptr = (Byte *)Image1->Picture->Bitmap->ScanLine[j];
for(i=0;i<w;i++)
for(j=0;j<h;j++)
{
if(Image1->Canvas->Pixels[i][j]==TColor(RGB(0,0,0)))
{
for(k=0;k<180;k+=1)
{
Dist2=(int)fabs((i*cos(k*PI/180.0))+(j*sin(k*PI/180)));
a[Dist2][k]++; //??
TempLine=(MYLINE *)(lpMyline+Dist2*Alpha+k);
if(j>(*TempLine).topy)
{
(*TempLine).topx=i;
(*TempLine).topy=j;
}
if(j<(*TempLine).boty)
{
(*TempLine).botx=i;
(*TempLine).boty=j;
}
}
}
}
int maxd=0;
int maxr=0;
int maxk=0;
for(i=0;i<Dist;i++)
for(j=0;j<Alpha;j++)
{
if(a[i][j]>maxd)
{
maxd=a[i][j];
maxr=i;maxk=j;
TempLine=(MYLINE*)(lpMyline+i*Alpha+j) ;
Maxline.topx= (*TempLine).topx;
Maxline.topy= (*TempLine).topy;
Maxline.botx= (*TempLine).botx;
Maxline.boty= (*TempLine).boty;
}
}
//将第一个最大值点附近清零
for(int i=-9;i<10;i++)
{
for(int k=-1;k<2;k++)
{
if(i+maxr>=0&&i+maxr<=Dist&&k+maxk>=0&&k+maxk<=Alpha)
{
a[i+maxr][k+maxk]=0;
}
}
}
//找到第二个最大值点
int maxd2=0;
int maxr2=0;
int maxk2=0;
for(i=Dist-1;i>=0;i--)
for(j=Alpha-1;j>=0;j--)
{
if(a[i][j]>maxd2)
{
maxd2=a[i][j];
maxr2=i;maxk2=j;
TempLine=(MYLINE*)(lpMyline+i*Alpha+j) ;
Maxline2.topx= (*TempLine).topx;
Maxline2.topy= (*TempLine).topy;
Maxline2.botx= (*TempLine).botx;
Maxline2.boty= (*TempLine).boty;
}
}
Image1->Canvas->Pen->Color=clRed;
Image1->Canvas->Pen->Width=1;
Image1->Canvas->MoveTo(Maxline.botx,Maxline.boty);
Image1->Canvas->LineTo(Maxline.topx,Maxline.topy);
Image1->Canvas->MoveTo(Maxline2.botx,Maxline2.boty);
Image1->Canvas->LineTo(Maxline2.topx,Maxline2.topy);
Edit1->Text=IntToStr(maxd);
AnsiString TopLeftxy,BotLeftxy,TopRightxy,BotRightxy;
TopLeftxy="(x,y)=("+IntToStr(Maxline.topx) + ","+IntToStr(Maxline.topy)+")";
BotLeftxy="(x,y)=("+IntToStr(Maxline.botx) + ","+IntToStr(Maxline.boty)+")";
Edit2->Text=TopLeftxy;
Edit3->Text=BotLeftxy;
TopRightxy="(x,y)=("+IntToStr(Maxline2.topx) + ","+IntToStr(Maxline2.topy)+")";
BotRightxy="(x,y)=("+IntToStr(Maxline2.botx) + ","+IntToStr(Maxline2.boty)+")";
Edit4->Text=TopRightxy;
Edit5->Text=BotRightxy;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if(OpenPictureDialog1->Execute())
{
Image1->Picture->LoadFromFile(OpenPictureDialog1->FileName);
}
}
//---------------------------------------------------------------------------