//---------------------------------------------------------------------------
#include <vcl.h>
#include <math.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//宣告左右兩圖之動態矩陣-------------------
unsigned char **left;
unsigned char **right;
double **ncc;
float **cp;
//----------------------------------------
//宣告罩窗大小lo
int lo;
//宣告分割成兩圖時所用之變數
//i1x=原圖之寬 i1y=原圖之長 i2x=分割後之圖的寬 i1x1原圖之寬的0.1倍 i1y3原圖之長的0.3倍
int i1x, i1y;
int i2x, i2y;
double abar = 0, bbar = 0;
double ncc1 = 0, ncc2 = 0, ncc3 = 0;
//下面是進行NCC計算之主要函式
double nccw(int ax, int ay, int bx, int by)
{
//將AB之平均歸零
abar = 0; bbar = 0;
//累加AB之罩窗內之所有值
for (int a = lo / - 2; a <= lo / 2; a++)
{
for (int b = lo / - 2; b <= lo / 2; b++)
{
abar = abar + left[abs(ax + a)][abs(ay + b)];
bbar = bbar + right[abs(bx + a)][abs(by + b)];
}
}
//將總和除以元素數
abar = abar / double(lo *lo);
bbar = bbar / double(lo *lo);
//宣告ncc1 ncc2 ncc3 此為NCC公式中之三個部份
ncc1 = 0; ncc2 = 0; ncc3 = 0;
for (int a = lo / - 2; a <= lo / 2; a++)
{
for (int b = lo / - 2; b <= lo / 2; b++)
{
ncc1 = ncc1 + (left[abs(ax + a)][abs(ay + b)] - abar)*(right[abs(bx + a)
][abs(by + b)] - bbar);
ncc2 = ncc2 + ((left[abs(ax + a)][abs(ay + b)] - abar)*(left[abs(ax + a)
][abs(ay + b)] - abar));
ncc3 = ncc3 + (right[abs(bx + a)][abs(by + b)] - bbar)*(right[abs(bx + a)
][abs(by + b)] - bbar);
}
}
double ncca = ncc1 / sqrt(ncc2) /sqrt(ncc3);
//回傳C值
return ncca;
}
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent *Owner): TForm(Owner){}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
if (OpenDialog2->Execute())
{
Form1->Visible = false;
Form1->Image1->Picture->Bitmap->LoadFromFile(OpenDialog2->FileName);
i1x = Image1->Picture->Bitmap->Width;
i1y = Image1->Picture->Bitmap->Height;
Form1->Visible = true;
Image1->Visible = false;
//建立一個矩陣K來儲存原圖之灰階值
left = new unsigned char *[i1y];
for (int q = 0; q < i1y; q++)
{
left[q] = new unsigned char[i1x];
}
Image1->Picture->Bitmap->PixelFormat=pf8bit;
unsigned char *ptr;
for (int a = 0; a < i1y; a++)
{
ptr = (Byte*)Image1->Picture->Bitmap->ScanLine[a];
for (int e = 0; e < i1x; e++)
{
left[a][e] = ptr[e];
ptr[e]=left[a][e];
}
}
Image1->Refresh();
Image1->Visible = true;
Button6->Enabled = true;
}
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall TForm1::Button6Click(TObject *Sender)
{
if (OpenDialog2->Execute())
{
Form1->Visible = false;
Form1->Image2->Picture->Bitmap->LoadFromFile(OpenDialog2->FileName);
i2x = Image2->Picture->Bitmap->Width;
i2y = Image2->Picture->Bitmap->Height;
Form1->Visible = true;
Image2->Visible = false;
//建立一個矩陣K來儲存原圖之灰階值
right = new unsigned char *[i2y];
for (int q = 0; q < i2y; q++)
{
right[q] = new unsigned char[i2x];
}
Image2->Picture->Bitmap->PixelFormat=pf8bit;
unsigned char *ptr;
for (int a = 0; a < i2y; a++)
{
ptr = (Byte*)Image2->Picture->Bitmap->ScanLine[a];
for (int e = 0; e < i2x; e++)
{
right[a][e] = ptr[e];
ptr[e]=right[a][e];
}
}
Image2->Refresh();
Image2->Visible = true;
PageControl1->ActivePage = TabSheet2;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button4Click(TObject *Sender)
{
if(OpenDialog1 -> Execute())
{
TStringList *Txt1 = new TStringList;
Txt1 -> LoadFromFile(OpenDialog1 -> FileName);
Txt1 -> Text = StringReplace( Txt1 -> Text, "\r\n", " ", TReplaceFlags());
Txt1 -> CommaText = Txt1 -> Text;
cp = new float *[Edit1->Text.ToInt()];
for(int p=0;p<Edit1->Text.ToInt();p++)
{
cp[p]=new float [4];
cp[p][0]=Txt1 -> Strings[p*4].ToDouble();
cp[p][1]=Txt1 -> Strings[p*4+1].ToDouble();
cp[p][2]=Txt1 -> Strings[p*4+2].ToDouble();
cp[p][3]=Txt1 -> Strings[p*4+3].ToDouble();
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
ncc=new double *[Edit2->Text.ToInt()];
for(int t=0;t<Edit2->Text.ToInt();t++)
ncc[t]=new double [Edit2->Text.ToInt()];
int range=Edit3->Text.ToInt();
lo =Edit2->Text.ToInt();
AnsiString mem="";
AnsiString mem2="";
AnsiString mem3="";
Memo1->Visible=false;
Memo1->Clear();
Memo2->Clear();
double temp1=0,temp2=0,temp3=0,result;
int x2,x3,y2,y3;
for(int t=0;t<Edit1->Text.ToInt();t++)
{
temp1=0;temp2=0;temp3=0;
mem=AnsiString(cp[t][0])+"\t"+AnsiString(cp[t][1])+"\t"+AnsiString(cp[t][2])+"\t"+AnsiString(cp[t][3]);
mem2=AnsiString(cp[t][0])+"\t"+AnsiString(cp[t][1])+"\t"+AnsiString(cp[t][2])+"\t"+AnsiString(cp[t][3]);
for(int w=cp[t][1]-range;w<=cp[t][1]+range;w++)
{
for(int s=cp[t][0]-range;s<=cp[t][0]+range;s++)
{
result=nccw(cp[t][1],cp[t][0],w,s);
mem=mem+"\t"+AnsiString(result);
if(result>=temp1)
{
temp3=temp2;
x3=x2;
y3=y2;
x2=s;
y2=w;
temp2=temp1;
temp1=result;
mem3="\t"+AnsiString(result)+"\t"+AnsiString(s)+"\t"+AnsiString(w)+"\t"+AnsiString(temp2)+"\t"+AnsiString(x2)+"\t"+AnsiString(y2)+"\t"+AnsiString(temp3)+"\t"+AnsiString(x3)+"\t"+AnsiString(y3);
}
}
}
Memo1->Lines->Add(mem);
Memo2->Lines->Add(mem2+mem3);
Memo1->Visible=true;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
if(SaveDialog1->Execute())
{
Memo2->Lines->SaveToFile(SaveDialog1->FileName);
}
}
//---------------------------------------------------------------------------