全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:2124
推到 Plurk!
推到 Facebook!

在Timer裡呼叫Thread後, 好像沒把程式跑完就結束了

 
Solaris2005
一般會員


發表:7
回覆:13
積分:4
註冊:2003-05-19

發送簡訊給我
#1 引用回覆 回覆 發表時間:2006-09-08 16:29:18 IP:220.130.xxx.xxx 訂閱

各位前輩,

我在Thread裡去比較兩張圖的差異性(想在15秒內做完)

然後在按鈕裡寫了Timer->OnTimer(Sender)去觸發Timer裡的code

Timer裡的code, 是 JudgeThread = new TJudgeThread(false);

可是....好像沒把程式跑完就結束了

Timer的interval 是 15000

如果不用Thread的方式做, 程式畫面會被鎖住而動不了

有其他的方法嗎?

aftcast
站務副站長


發表:81
回覆:1485
積分:1763
註冊:2002-11-21

發送簡訊給我
#2 引用回覆 回覆 發表時間:2006-09-09 19:15:37 IP:61.229.xxx.xxx 未訂閱
你好,請問 :
1/ 為何想在15秒內做完? 時間夠嗎?
2/Timer本身就是一種thread,理論上應該不會跑的時候就當死,除非timer裡的code花很多很多的時間。
3/ 可是你又一定要15秒完成? 這樣有矛盾。方便把Timer 裡的code貼一下嗎?

------


蕭沖
--All ideas are worthless unless implemented--

C++ Builder Delphi Taiwan G+ 社群
http://bit.ly/cbtaiwan
Solaris2005
一般會員


發表:7
回覆:13
積分:4
註冊:2003-05-19

發送簡訊給我
#3 引用回覆 回覆 發表時間:2006-09-10 23:32:18 IP:219.68.xxx.xxx 訂閱

1) 我的第一張圖是Golden Sample, 第二張圖是欲與Golden Sample比對的圖, 要知道這張圖和Golden Sample的差異, 15秒的原因是因為每15秒CCD就會取像並且產生一張新的圖, 這張圖就是要和Golden Sample比對的圖, 時間夠不夠, 我也在研究要怎麼樣才能在15秒內把程式跑完, 再做精簡

3) 程式碼如下, 寫得很笨, 不要笑我

void __fastcall TfrmInspection::TimerAutoModeTimer(TObject *Sender)
{
JudgeThread = new TJudgeThread(false);
}

void __fastcall TJudgeThread::Execute()
{
SetName();
//---- Place thread code here ----

while (!Terminated)
{
DIR *dir;
struct dirent *entry;
if ((dir=opendir(DIR_IMAGE_SOURCE)) == NULL)
{
frmLog->memoEvent->Lines->Add("Income image directory is not found.");
frmInspection->m_nThreadStatus = 100;
return;
}

sGoldenImageName = DIR_IMAGE_GOLDEN;
sGoldenImageName = g_RecipeInfo.sRecipeName;
sGoldenImageName = ".bmp";

while ((entry=readdir(dir)) != NULL)
{
if ((strcmp(entry->d_name,".")==0) | (strcmp(entry->d_name,"..")==0) | (ExtractFileExt(entry->d_name) != ".bmp"))
{
continue;
}

sIncomeImageName = DIR_IMAGE_SOURCE; // from c:\image
sIncomeImageName = entry->d_name;

sWHMImageName = DIR_IMAGE_WAREHOUSE;

sWHMImageName = entry->d_name;

sInspectImageName = DIR_IMAGE_INSPECTION;

sInspectImageName = entry->d_name;

frmInspection->stImageGenTime->Caption = "No define";
frmInspection->stImageMoveIn->Caption = Now().FormatString("yyyy-mm-dd hh:mm:ss");
frmInspection->imgInspection->Picture->LoadFromFile(sIncomeImageName);

frmInspection->sgImageInfo->Cells[1][1] = AnsiString(entry->d_name);
frmInspection->sgImageInfo->Cells[1][2] = g_RecipeInfo.sRecipeName;
frmInspection->sgImageInfo->Cells[1][3] = IntToStr(frmInspection->imgInspection->Picture->Width) " pixels / " IntToStr(frmInspection->imgInspection->Picture->Height) " pixels";
frmInspection->sgImageInfo->Cells[1][4] = g_RecipeInfo.sRecipeName ".bmp";
frmInspection->sgImageInfo->Cells[1][5] = "2006-09-04 20:20:50";

AnsiString sCounter = "";
int nCounterSum = 0;
AnsiString sImage = "";;
int nCounter = 0;

reg->RootKey = HKEY_LOCAL_MACHINE;
reg->OpenKey("Software\\ChipMOS\\AutoVI", false);

sCounter = reg->ReadString("SampleCounter");
if (sCounter.IsEmpty())
{
reg->WriteString("SampleCounter", "00000");
sCounter = "00000";
}
nCounterSum = sCounter.ToInt();
nCounterSum ;

nCounter = nCounterSum/10000;
sImage = DIR_ICON IntToStr(nCounter) ".bmp";
frmInspection->imgCounter1->Picture->LoadFromFile(sImage);
nCounter = (nCounterSum000)/1000;
sImage = DIR_ICON IntToStr(nCounter) ".bmp";
frmInspection->imgCounter2->Picture->LoadFromFile(sImage); // 4-
nCounter = (nCounterSum00)/100;
sImage = DIR_ICON IntToStr(nCounter) ".bmp";
frmInspection->imgCounter3->Picture->LoadFromFile(sImage); // 3-digit
nCounter = (nCounterSum0)/10;
sImage = DIR_ICON IntToStr(nCounter) ".bmp";
frmInspection->imgCounter4->Picture->LoadFromFile(sImage); // 2-digit
sImage = DIR_ICON IntToStr(nCounterSum) ".bmp";
frmInspection->imgCounter5->Picture->LoadFromFile(sImage); // 1-digit (right)
sCounter = IntToStr(nCounterSum);
reg->WriteString("SampleCounter", sCounter);
reg->CloseKey();

frmInspection->m_nThreadStatus = 50;
MakeSobelThresholdImage(sIncomeImageName, sInspectImageName);

FindTopLeft(0, sInspectImageName);
FindTopLeft(1, sGoldenImageName);

if (CompareWithSourceImage(sInspectImageName, sGoldenImageName) == 0)
{
closedir(dir);
frmInspection->m_nThreadStatus = 0;
return;
}

frmInspection->m_nThreadStatus = 1;

frmInspection->stImageMoveOut->Caption = Now().FormatString("yyyy-mm-dd hh:mm:ss");

if (FileExists(sWHMImageName))
{
remove(sWHMImageName.c_str());
}

MoveFile(sIncomeImageName.c_str(), sWHMImageName.c_str());

Sleep(50);
if (FileExists(sIncomeImageName))
{
remove(sIncomeImageName.c_str());
}

remove(m_sBuf.c_str());
}

closedir(dir);

frmInspection->m_nThreadStatus = -1;
}
}
//---------------------------------------------------------------------------

void __fastcall TJudgeThread::MakeSobelThresholdImage(AnsiString sIncomeImage, AnsiString sInspectImageName)
{
frmImage->imgSrc->Picture->Bitmap->LoadFromFile(sIncomeImage);
frmImage->imgSrc->Width = frmImage->imgSrc->Picture->Width;
frmImage->imgSrc->Picture->Bitmap->Canvas->Lock();

frmImage->imgDst->Picture->Bitmap->PixelFormat = pf8bit;
frmImage->imgDst->Width = frmImage->imgSrc->Width;
frmImage->imgDst->Height = frmImage->imgSrc->Height;
frmImage->imgDst->Picture->Bitmap->Canvas->Lock();

int p[10] = {0};
int GS;

int nWidthStart;
int nWidthEnd;
int nHeightStart;
int nHeightEnd;

nWidthStart = g_RecipeInfo.ScopeRange[0][0].x;
nWidthEnd = g_RecipeInfo.ScopeRange[0][1].x;
nHeightStart = g_RecipeInfo.ScopeRange[0][0].y;
nHeightEnd = g_RecipeInfo.ScopeRange[0][1].y;

for (int y=nHeightStart; y<=nHeightEnd; y )
{
for (int x=nWidthStart; x<=nWidthEnd; x )
{
p[1] = (frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x-1][y-1] & 0x000000ff);
p[2] = (frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x][y-1] & 0x000000ff);
p[3] = (frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x 1][y-1] & 0x000000ff);
p[4] = (frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x-1][y] & 0x000000ff);
p[5] = (frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x][y] & 0x000000ff);
p[6] = (frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x 1][y] & 0x000000ff);
p[7] = (frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x-1][y 1] & 0x000000ff);
p[8] = (frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x][y 1] & 0x000000ff);
p[9] = (frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x 1][y 1] & 0x000000ff);

GS = (abs((p[3] 2*p[6] p[9])-(p[1] 2*p[4] p[7])) abs((p[7] 2*p[8] p[9])-(p[1] 2*p[2] p[3])))/2;

GS >= 80 ? GS = 255 : GS = 0;

frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x][y] = (TColor)RGB(GS,GS,GS);
}
}

if (g_RecipeInfo.ScopeRange[1][0].x != 0)
{
nWidthStart = g_RecipeInfo.ScopeRange[1][0].x;
nWidthEnd = g_RecipeInfo.ScopeRange[1][1].x;
nHeightStart = g_RecipeInfo.ScopeRange[1][0].y;
nHeightEnd = g_RecipeInfo.ScopeRange[1][1].y;

for (int y=nHeightStart; y<=nHeightEnd; y )
{
for (int x=nWidthStart; x<=nWidthEnd; x )
{
p[1] = (frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x-1][y-1] & 0x000000ff);
p[2] = (frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x][y-1] & 0x000000ff);
p[3] = (frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x 1][y-1] & 0x000000ff);
p[4] = (frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x-1][y] & 0x000000ff);
p[5] = (frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x][y] & 0x000000ff);
p[6] = (frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x 1][y] & 0x000000ff);
p[7] = (frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x-1][y 1] & 0x000000ff);
p[8] = (frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x][y 1] & 0x000000ff);
p[9] = (frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x 1][y 1] & 0x000000ff);

GS = (abs((p[3] 2*p[6] p[9])-(p[1] 2*p[4] p[7])) abs((p[7] 2*p[8] p[9])-(p[1] 2*p[2] p[3])))/2;

GS >= 80 ? GS = 255 : GS = 0;

frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x][y] = (TColor)RGB(GS,GS,GS);
}
}
}

if (g_RecipeInfo.ScopeRange[2][0].x != 0)
{
nWidthStart = g_RecipeInfo.ScopeRange[2][0].x;
nWidthEnd = g_RecipeInfo.ScopeRange[2][1].x;
nHeightStart = g_RecipeInfo.ScopeRange[2][0].y;
nHeightEnd = g_RecipeInfo.ScopeRange[2][1].y;

for (int y=nHeightStart; y<=nHeightEnd; y )
{
for (int x=nWidthStart; x<=nWidthEnd; x )
{
p[1] = (frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x-1][y-1] & 0x000000ff);
p[2] = (frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x][y-1] & 0x000000ff);
p[3] = (frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x 1][y-1] & 0x000000ff);
p[4] = (frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x-1][y] & 0x000000ff);
p[5] = (frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x][y] & 0x000000ff);
p[6] = (frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x 1][y] & 0x000000ff);
p[7] = (frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x-1][y 1] & 0x000000ff);
p[8] = (frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x][y 1] & 0x000000ff);
p[9] = (frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x 1][y 1] & 0x000000ff);

GS = (abs((p[3] 2*p[6] p[9])-(p[1] 2*p[4] p[7])) abs((p[7] 2*p[8] p[9])-(p[1] 2*p[2] p[3])))/2;

GS >= 80 ? GS = 255 : GS = 0;

frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x][y] = (TColor)RGB(GS,GS,GS);
}
}
}

frmImage->imgDst->Picture->SaveToFile(sInspectImageName);
frmImage->imgDst->Picture->Bitmap->Canvas->Unlock();
frmImage->imgSrc->Picture->Bitmap->Canvas->Unlock();
}
//---------------------------------------------------------------------------

void __fastcall TJudgeThread::FindTopLeft(int nPicID, AnsiString sImageFile)
{
if (nPicID == 1)
frmImage->imgSrc->Picture->Bitmap->LoadFromFile(sImageFile); //golden
else
frmImage->imgDst->Picture->Bitmap->LoadFromFile(sImageFile);

int nValue = 0;
for (int y=g_RecipeInfo.ScopeRange[0][0].y; y<=g_RecipeInfo.ScopeRange[0][1].y; y )
{
for (int x=g_RecipeInfo.ScopeRange[0][0].x; x<=g_RecipeInfo.ScopeRange[0][1].x; x )
{
nValue = GetRValue(frmImage->imgSrc->Canvas->Pixels[x][y]);

if (nValue == 255)
{
tpTopLeft[nPicID][0].x = x;
tpTopLeft[nPicID][0].y = y;
break;
}
}
}

nValue = 0;
if (g_RecipeInfo.ScopeRange[1][0].x != 0)
{
for (int y=g_RecipeInfo.ScopeRange[1][0].y; y<=g_RecipeInfo.ScopeRange[1][1].y; y )
{
for (int x=g_RecipeInfo.ScopeRange[1][0].x; x<=g_RecipeInfo.ScopeRange[1][1].x; x )
{
nValue = GetRValue(frmImage->imgSrc->Canvas->Pixels[x][y]);

if (nValue == 255)
{
tpTopLeft[nPicID][1].x = x;
tpTopLeft[nPicID][1].y = y;
break;
}
}
}
}

nValue = 0;
if (g_RecipeInfo.ScopeRange[2][0].x != 0)
{
for (int y=g_RecipeInfo.ScopeRange[2][0].y; y<=g_RecipeInfo.ScopeRange[2][1].y; y )
{
for (int x=g_RecipeInfo.ScopeRange[2][0].x; x<=g_RecipeInfo.ScopeRange[2][1].x; x )
{
nValue = GetRValue(frmImage->imgSrc->Canvas->Pixels[x][y]);

if (nValue == 255)
{
tpTopLeft[nPicID][2].x = x;
tpTopLeft[nPicID][2].y = y;
break;
}
}
}
}
}
//---------------------------------------------------------------------------

int __fastcall TJudgeThread::CompareWithSourceImage(AnsiString sInspImageName, AnsiString sGoldenImageName)
{
frmImage->imgSrc->Picture->Bitmap->LoadFromFile(sGoldenImageName);
frmImage->imgDst->Picture->Bitmap->LoadFromFile(sInspImageName);

int nWidthStart;
int nWidthEnd;
int nHeightStart;
int nHeightEnd;

int nSrc[25] = {0};
int nDst[25] = {0};

int nSubSrc = 0;
int nSubDst = 0;

long nPercentSrc;
long nPercentDst;
long nRslt;

int nDistX = tpTopLeft[0][0].x - tpTopLeft[1][0].x;
int nDistY = tpTopLeft[0][0].y - tpTopLeft[1][0].y;

nWidthStart = g_RecipeInfo.ScopeRange[0][0].x;
nWidthEnd = g_RecipeInfo.ScopeRange[0][1].x;
nHeightStart = g_RecipeInfo.ScopeRange[0][0].y;
nHeightEnd = g_RecipeInfo.ScopeRange[0][1].y;

int nJudge = 1;

for (int y=(nHeightStart 2); y<=(nHeightEnd-2); y =5)
{
for (int x=(nWidthStart 2); x<=(nWidthEnd-2); x =5)
{
nSrc[0] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x-2][y-2]);
nSrc[1] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x-1][y-2]);
nSrc[2] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x][y-2]);
nSrc[3] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x 1][y-2]);
nSrc[4] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x 2][y-2]);
nSrc[5] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x-2][y-1]);
nSrc[6] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x-1][y-1]);
nSrc[7] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x][y-1]);
nSrc[8] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x 1][y-1]);
nSrc[9] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x 2][y-1]);
nSrc[10] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x-2][y]);
nSrc[11] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x-1][y]);
nSrc[12] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x][y]);
nSrc[13] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x 1][y]);
nSrc[14] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x 2][y]);
nSrc[15] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x-2][y 1]);
nSrc[16] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x-1][y 1]);
nSrc[17] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x][y 1]);
nSrc[18] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x 1][y 1]);
nSrc[19] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x 2][y 1]);
nSrc[20] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x-2][y 2]);
nSrc[21] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x-1][y 2]);
nSrc[22] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x][y 2]);
nSrc[23] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x 1][y 2]);
nSrc[24] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x 2][y 2]);

nDst[0] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x-2-nDistX][y-2-nDistY]);
nDst[1] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x-1-nDistX][y-2-nDistY]);
nDst[2] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x-nDistX][y-2-nDistY]);
nDst[3] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x 1-nDistX][y-2-nDistY]);
nDst[4] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x 2-nDistX][y-2-nDistY]);
nDst[5] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x-2-nDistX][y-1-nDistY]);
nDst[6] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x-1-nDistX][y-1-nDistY]);
nDst[7] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x-nDistX][y-1-nDistY]);
nDst[8] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x 1-nDistX][y-1-nDistY]);
nDst[9] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x 2-nDistX][y-1-nDistY]);
nDst[10] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x-2-nDistX][y-nDistY]);
nDst[11] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x-1-nDistX][y-nDistY]);
nDst[12] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x-nDistX][y-nDistY]);
nDst[13] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x 1-nDistX][y-nDistY]);
nDst[14] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x 2-nDistX][y-nDistY]);
nDst[15] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x-2-nDistX][y 1-nDistY]);
nDst[16] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x-1-nDistX][y 1-nDistY]);
nDst[17] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x-nDistX][y 1-nDistY]);
nDst[18] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x 1-nDistX][y 1-nDistY]);
nDst[19] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x 2-nDistX][y 1-nDistY]);
nDst[20] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x-2-nDistX][y 2-nDistY]);
nDst[21] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x-1-nDistX][y 2-nDistY]);
nDst[22] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x-nDistX][y 2-nDistY]);
nDst[23] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x 1-nDistX][y 2-nDistY]);
nDst[24] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x 2-nDistX][y 2-nDistY]);

nSubSrc = 0;
nSubDst = 0;
for (int x=0; x<25; x )
{
if (nSrc[x] == 255)
{
nSubSrc ;
}

if (nDst[x] == 255)
{
nSubDst ;
}
}

nSubSrc > 12 ? nPercentSrc = nSubSrc/25 : nPercentSrc = (25-nSubSrc)/25;
nSubDst > 12 ? nPercentDst = nSubDst/25 : nPercentDst = (25-nSubDst)/25;

nPercentSrc > nPercentDst ? nRslt = nPercentDst/nPercentSrc : nRslt = nPercentSrc/nPercentDst;

if (nRslt < 0.6)
{
nJudge = 0;
break;
}
}
}

if (nJudge == 0)
{
return 0;
}

if (g_RecipeInfo.ScopeRange[1][0].x != 0)
{
nWidthStart = g_RecipeInfo.ScopeRange[1][0].x;
nWidthEnd = g_RecipeInfo.ScopeRange[1][1].x;
nHeightStart = g_RecipeInfo.ScopeRange[1][0].y;
nHeightEnd = g_RecipeInfo.ScopeRange[1][1].y;

nDistX = tpTopLeft[0][1].x - tpTopLeft[1][1].x;
nDistY = tpTopLeft[0][1].y - tpTopLeft[1][1].y;

for (int y=(nHeightStart 2); y<=(nHeightEnd-2); y =5)
{
for (int x=(nWidthStart 2); x<=(nWidthEnd-2); x =5)
{
nSrc[0] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x-2][y-2]);
nSrc[1] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x-1][y-2]);
nSrc[2] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x][y-2]);
nSrc[3] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x 1][y-2]);
nSrc[4] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x 2][y-2]);
nSrc[5] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x-2][y-1]);
nSrc[6] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x-1][y-1]);
nSrc[7] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x][y-1]);
nSrc[8] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x 1][y-1]);
nSrc[9] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x 2][y-1]);
nSrc[10] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x-2][y]);
nSrc[11] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x-1][y]);
nSrc[12] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x][y]);
nSrc[13] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x 1][y]);
nSrc[14] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x 2][y]);
nSrc[15] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x-2][y 1]);
nSrc[16] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x-1][y 1]);
nSrc[17] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x][y 1]);
nSrc[18] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x 1][y 1]);
nSrc[19] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x 2][y 1]);
nSrc[20] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x-2][y 2]);
nSrc[21] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x-1][y 2]);
nSrc[22] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x][y 2]);
nSrc[23] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x 1][y 2]);
nSrc[24] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x 2][y 2]);

nDst[0] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x-2-nDistX][y-2-nDistY]);
nDst[1] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x-1-nDistX][y-2-nDistY]);
nDst[2] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x-nDistX][y-2-nDistY]);
nDst[3] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x 1-nDistX][y-2-nDistY]);
nDst[4] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x 2-nDistX][y-2-nDistY]);
nDst[5] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x-2-nDistX][y-1-nDistY]);
nDst[6] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x-1-nDistX][y-1-nDistY]);
nDst[7] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x-nDistX][y-1-nDistY]);
nDst[8] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x 1-nDistX][y-1-nDistY]);
nDst[9] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x 2-nDistX][y-1-nDistY]);
nDst[10] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x-2-nDistX][y-nDistY]);
nDst[11] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x-1-nDistX][y-nDistY]);
nDst[12] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x-nDistX][y-nDistY]);
nDst[13] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x 1-nDistX][y-nDistY]);
nDst[14] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x 2-nDistX][y-nDistY]);
nDst[15] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x-2-nDistX][y 1-nDistY]);
nDst[16] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x-1-nDistX][y 1-nDistY]);
nDst[17] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x-nDistX][y 1-nDistY]);
nDst[18] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x 1-nDistX][y 1-nDistY]);
nDst[19] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x 2-nDistX][y 1-nDistY]);
nDst[20] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x-2-nDistX][y 2-nDistY]);
nDst[21] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x-1-nDistX][y 2-nDistY]);
nDst[22] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x-nDistX][y 2-nDistY]);
nDst[23] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x 1-nDistX][y 2-nDistY]);
nDst[24] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x 2-nDistX][y 2-nDistY]);

nSubSrc = 0;
nSubDst = 0;
for (int x=0; x<25; x )
{
if (nSrc[x] == 255)
{
nSubSrc ;
}

if (nDst[x] == 255)
{
nSubDst ;
}
}

nSubSrc > 12 ? nPercentSrc = nSubSrc/25 : nPercentSrc = (25-nSubSrc)/25;
nSubDst > 12 ? nPercentSrc = nSubSrc/25 : nPercentSrc = (25-nSubSrc)/25;

nPercentSrc > nPercentDst ? nRslt = nPercentDst/nPercentSrc : nRslt = nPercentSrc/nPercentDst;

if (nRslt < 0.6)
{
nJudge = 0;
break;
}
}
}

if (nJudge == 0)
{
return 0;
}
}

if (g_RecipeInfo.ScopeRange[2][0].x != 0)
{
nWidthStart = g_RecipeInfo.ScopeRange[2][0].x;
nWidthEnd = g_RecipeInfo.ScopeRange[2][1].x;
nHeightStart = g_RecipeInfo.ScopeRange[2][0].y;
nHeightEnd = g_RecipeInfo.ScopeRange[2][1].y;

nDistX = tpTopLeft[0][2].x - tpTopLeft[1][2].x;
nDistY = tpTopLeft[0][2].y - tpTopLeft[1][2].y;

for (int y=(nHeightStart 2); y<=(nHeightEnd-2); y =5)
{
for (int x=(nWidthStart 2); x<=(nWidthEnd-2); x =5)
{
nSrc[0] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x-2][y-2]);
nSrc[1] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x-1][y-2]);
nSrc[2] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x][y-2]);
nSrc[3] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x 1][y-2]);
nSrc[4] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x 2][y-2]);
nSrc[5] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x-2][y-1]);
nSrc[6] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x-1][y-1]);
nSrc[7] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x][y-1]);
nSrc[8] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x 1][y-1]);
nSrc[9] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x 2][y-1]);
nSrc[10] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x-2][y]);
nSrc[11] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x-1][y]);
nSrc[12] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x][y]);
nSrc[13] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x 1][y]);
nSrc[14] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x 2][y]);
nSrc[15] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x-2][y 1]);
nSrc[16] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x-1][y 1]);
nSrc[17] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x][y 1]);
nSrc[18] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x 1][y 1]);
nSrc[19] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x 2][y 1]);
nSrc[20] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x-2][y 2]);
nSrc[21] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x-1][y 2]);
nSrc[22] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x][y 2]);
nSrc[23] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x 1][y 2]);
nSrc[24] = GetRValue(frmImage->imgSrc->Picture->Bitmap->Canvas->Pixels[x 2][y 2]);

nDst[0] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x-2-nDistX][y-2-nDistY]);
nDst[1] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x-1-nDistX][y-2-nDistY]);
nDst[2] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x-nDistX][y-2-nDistY]);
nDst[3] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x 1-nDistX][y-2-nDistY]);
nDst[4] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x 2-nDistX][y-2-nDistY]);
nDst[5] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x-2-nDistX][y-1-nDistY]);
nDst[6] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x-1-nDistX][y-1-nDistY]);
nDst[7] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x-nDistX][y-1-nDistY]);
nDst[8] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x 1-nDistX][y-1-nDistY]);
nDst[9] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x 2-nDistX][y-1-nDistY]);
nDst[10] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x-2-nDistX][y-nDistY]);
nDst[11] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x-1-nDistX][y-nDistY]);
nDst[12] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x-nDistX][y-nDistY]);
nDst[13] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x 1-nDistX][y-nDistY]);
nDst[14] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x 2-nDistX][y-nDistY]);
nDst[15] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x-2-nDistX][y 1-nDistY]);
nDst[16] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x-1-nDistX][y 1-nDistY]);
nDst[17] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x-nDistX][y 1-nDistY]);
nDst[18] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x 1-nDistX][y 1-nDistY]);
nDst[19] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x 2-nDistX][y 1-nDistY]);
nDst[20] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x-2-nDistX][y 2-nDistY]);
nDst[21] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x-1-nDistX][y 2-nDistY]);
nDst[22] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x-nDistX][y 2-nDistY]);
nDst[23] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x 1-nDistX][y 2-nDistY]);
nDst[24] = GetRValue(frmImage->imgDst->Picture->Bitmap->Canvas->Pixels[x 2-nDistX][y 2-nDistY]);

nSubSrc = 0;
nSubDst = 0;
for (int x=0; x<25; x )
{
if (nSrc[x] == 255)
{
nSubSrc ;
}

if (nDst[x] == 255)
{
nSubDst ;
}
}

nSubSrc > 12 ? nPercentSrc = nSubSrc/25 : nPercentSrc = (25-nSubSrc)/25;
nSubDst > 12 ? nPercentSrc = nSubSrc/25 : nPercentSrc = (25-nSubSrc)/25;

nPercentSrc > nPercentDst ? nRslt = nPercentDst/nPercentSrc : nRslt = nPercentSrc/nPercentDst;

if (nRslt < 0.6)
{
nJudge = 0;
break;
}
}
}

if (nJudge == 0)
{
return 0;
}
}

return 1;
}
//---------------------------------------------------------------------------
===================引 用 文 章===================
你好,請問 :
1/ 為何想在15秒內做完? 時間夠嗎?
2/Timer本身就是一種thread,理論上應該不會跑的時候就當死,除非timer裡的code花很多很多的時間。
3/ 可是你又一定要15秒完成? 這樣有矛盾。方便把Timer 裡的code貼一下嗎?

aftcast
站務副站長


發表:81
回覆:1485
積分:1763
註冊:2002-11-21

發送簡訊給我
#4 引用回覆 回覆 發表時間:2006-09-11 20:27:38 IP:61.229.xxx.xxx 未訂閱

你好,原來是因為CCD15秒取一張!
剛大概看了程式,一堆for,我頭都有點昏。不過,我猜應該是15秒不夠。
程式寫得很棒,雖未仔細的看完,但從風格等就可以看出。

我個人提出另一個覺得比較robust的方式參考 :

問題部份:
1/ 因為整個code花費時間很長,無論是否使用Timer都會造成其他MSG沒法取回而類似死當。
2/ 是否能控制在15秒內完成,這是很大的問題,多工環境因user使用情形不同,所需的時間可能更長。
3/ 若使用Timer create thread的方式則相當於multithread的情形,於是要考慮thread-safe的問題、存取global資源lock的問題。以你目前的實作來看,你說好像沒完成…這也許是資源dead lock,或是使用的function有些並非是thread-safe的。因為程式在15秒若未跑完,timer又起另一個thread,而第一個thread可能會lock住第二個thread,第二個也可能lock住第一個…

分析:
1/ 從你提供的需求,我覺得並不需要使用multithread來處理,因為每一次的比較過程中並沒有階段的要產生訊息給user。若以socket的程式來說,就需要multithread,因為每個連線都有好幾階段的訊息要和user 互動,總不能等某個thread整套完成才換另一個。而且multithread並不會提昇整個performance,反而整體會降效能。
2/ 15秒的問題,最好的方式是用queue的方式把要作的事queue住,然後循序處理

解決的model:
1/ 僅create 一個 thread來處理queue的工作。
2/ 主程式主要工作 : 偵測是否要增加工作至queue,比如說15秒後發現有新的圖時,則新增一個work item至queue,並通知thread開始/ 繼續工作。

實作部份:
1/ queue的部份 : 先定義一個struct的結構體,存放要處理的工作資料,比如說檔名等…再用STL裡的deque container來把這個struct放進去。這樣就有一個queue的結構了。當新增時,則用push_back( )把工作加至最後面,當一個工作完成時就用pop_front( )移除第一個工作。
2/ 在主Form建立一個 event handler 資料成員,參考win32 sdk 中的CreateEvent。此handler是為了要通知thread有新的工作來了,請開工。配合WaitForSingleObject這個api用的。如此才不會造成沒有工作時cpu overhead。當每次處理完一個工作時就檢查queue中是否還有工作,若沒有,則對event handler 使用ResetEvent()。這樣下個回圈時就會停住。相反的,當主程式有新的工作來時,也要把這個event用SetEvent()處理。這樣回圈就又開始跑…
3/ 主Form要加入一個TCriticalSection data member,這是為了主程式或是thread同時取share data時不會打架。比如說取存queue時,取或存event handler時…這些data都是在主Form中建立的,供自己和子緒get/set。
4/ thread中的execute方法改成: (pseudo-code)
while (!Terminated && WaitForSingleObject(globalEventHandler,INFINITE) == 0L)
{
.........
m_MyCriticalSection->Acquire(); //開始保護下面的code
data = m_Mydeque.front( ); //取queue的資料
.........................
m_MyCriticalSection->Release(); //結束保護
work_function(data); //處理工作
m_Mydeque.pop_front(); //把工作從queue中踢出
if (m_Mydeque.Empty()) //若是沒queue
{
m_MyCriticalSection->Acquire(); //開始保護下面的code
SetEvent(m_MyEventHandler); //等一下就會停在WaitForSingleObject那行
m_MyCriticalSection->Release(); //結束保護
}
......................

}

沒時間寫了,等下次有時間再補充一些。希望有些幫助!

===================引 用 文 章===================

1) 我的第一張圖是Golden Sample, 第二張圖是欲與Golden Sample比對的圖, 要知道這張圖和Golden Sample的差異, 15秒的原因是因為每15秒CCD就會取像並且產生一張新的圖, 這張圖就是要和Golden Sample比對的圖, 時間夠不夠, 我也在研究要怎麼樣才能在15秒內把程式跑完, 再做精簡

3) 程式碼如下, 寫得很笨, 不要笑我

------


蕭沖
--All ideas are worthless unless implemented--

C++ Builder Delphi Taiwan G+ 社群
http://bit.ly/cbtaiwan
Solaris2005
一般會員


發表:7
回覆:13
積分:4
註冊:2003-05-19

發送簡訊給我
#5 引用回覆 回覆 發表時間:2006-09-13 00:03:33 IP:219.68.xxx.xxx 訂閱

真是一語點醒我夢中人

你的分析真是精僻, 我的程式的確是還沒跑完, 就已經又再起了第二個Thread了

至於deque的方法, 我沒有用過, 是不是與之前有前輩提過的MSMQ一樣的東西?

可以先import MSMQ的元件, 再做設計?

還是有它(deque)的寫法呢?

因為我找不到deque的用法, 只找到MSMQ的一點點小例子, 感謝指教!

aftcast
站務副站長


發表:81
回覆:1485
積分:1763
註冊:2002-11-21

發送簡訊給我
#6 引用回覆 回覆 發表時間:2006-09-13 10:57:27 IP:61.229.xxx.xxx 未訂閱
你好,我已經寫了一個範例,你可以參考一下,有實作queue的部份。

依你的情形,就把我push button的 onlick事件改成 timer 比如說15秒,然後自動加入work item 應該就可以了!

有問題的話,也請到下面的討論post,謝謝。

http://delphi.ktop.com.tw/board.php?cid=168&fid=912&tid=84144
------


蕭沖
--All ideas are worthless unless implemented--

C++ Builder Delphi Taiwan G+ 社群
http://bit.ly/cbtaiwan
系統時間:2024-04-20 14:14:16
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!