背景相減~動態與靜態 |
答題得分者是:taishyang
|
kk1127pp
一般會員 發表:4 回覆:4 積分:1 註冊:2011-09-30 發送簡訊給我 |
各位大大~~我有一個問題
我先照了一張背景圖後~再用webcam照同一地方並加入物體 我想將我想將兩圖做相減~~並二質化~~但是我做出來都有錯 請各位大大幫我看看吧!! void __fastcall TForm1::Timer1Timer(TObject *Sender) { //影像做灰階 int k=0, B1=0, G1=0, R1=0, gray=0; TCanvas *csBuf1=new TCanvas; HDC hDC1=GetDC(Form1->Panel1->Handle); // 取得視窗的包含工作區的畫面 csBuf1->Handle=hDC1; Graphics::TBitmap *photo1 = new Graphics::TBitmap(); // 儲存欲處理的影像資料指標 photo1->Width=320; photo1->Height=240; photo1->PixelFormat = pf24bit; photo1->Canvas->CopyRect(Rect(0,0,320,240),csBuf1,Rect(0,0,320,240)); Application->ProcessMessages(); for(int y = 0 ; y < 240; y ) { Byte *ptr1=(Byte*)photo1->ScanLine[y]; for(int x = 0 ; x < 320 ; x ) { k=3*x; B1 = ptr1[k ]; G1 = ptr1[k 1]; R1 = ptr1[k 2]; gray=(B1 R1 G1)/3; ptr1[k ]=gray; ptr1[k 1]=gray; ptr1[k 2]=gray; } } Image1->Canvas->Draw(0,0,photo1); delete photo1; delete csBuf1; } //--------------------------------------------------------------------------- void __fastcall TForm1::ConnectClick(TObject *Sender) { hWndC_CCD1=capCreateCaptureWindow("Capture Window1",WS_CHILD | WS_VISIBLE,0,0,320,240,Panel1->Handle,0); capDriverConnect(hWndC_CCD1,0); //與驅動程式連結 capPreviewRate(hWndC_CCD1,10); //每秒影像變化張數 CAPDRIVERCAPS aCAPDRIVERCAPS; aCAPDRIVERCAPS.wDeviceIndex=0; capDriverConnect(hWndC_CCD1,aCAPDRIVERCAPS.wDeviceIndex); capPreview(hWndC_CCD1,true);//使影像在視窗上顯示 Timer1->Enabled=true; } //--------------------------------------------------------------------------- void __fastcall TForm1::Button2Click(TObject *Sender) { //靜態圖做灰階 Graphics::TBitmap *pBitmap = new Graphics::TBitmap; Byte *ptr; unsigned int B,G,R; int x,y; pBitmap->Assign(Image2->Picture); for (y = 0; y < pBitmap->Height; y )//掃圖先掃行在掃列 { //將圖的每BITE找出來 ptr = (BYTE*)pBitmap->ScanLine[y]; for (x = 0; x < pBitmap->Width; x ) { B=*(ptr x*3); G=*(ptr x*3 1); R=*(ptr x*3 2); *(ptr x*3)=(B G R)/3; //灰階的操作式(全部相加除3) *(ptr x*3 1)=(B G R)/3; *(ptr x*3 2)=(B G R)/3; } } Image2->Canvas->Draw(0,0,pBitmap);//畫出圖 delete pBitmap; } //--------------------------------------------------------------------------- void __fastcall TForm1::Button3Click(TObject *Sender) { //背景相減 Graphics::TBitmap *pBitmap2 = new Graphics::TBitmap; Byte *ptr2; Graphics::TBitmap *pBitmap3 = new Graphics::TBitmap; Byte *ptr3; Graphics::TBitmap *pBitmap4 = new Graphics::TBitmap; Byte *ptr4; pBitmap2->Assign(Image2->Picture); pBitmap3->Assign(Image1->Picture); pBitmap4->Assign(Image3->Picture); int x1,y1; for (y1 = 0; y1 < 240; y1 )//掃圖先掃行在掃列 { //將圖的每BITE找出來 ptr2 = (BYTE*)pBitmap2->ScanLine[y1]; ptr3 = (BYTE*)pBitmap3->ScanLine[y1]; ptr4 = (BYTE*)pBitmap4->ScanLine[y1]; for (x1 = 0; x1 < 320; x1 ) { int r1=ptr2[x1*3]; int g1=ptr2[x1*3 1]; int b1=ptr2[x1*3 2]; int r2=ptr3[x1*3]; int g2=ptr3[x1*3 1]; int b2=ptr3[x1*3 2]; int gray1 = (r1 b1 g1)/3; int gray2 = (r2 b2 g2)/3; int sub = abs( gray1 - gray2 ); if(sub >= ScrollBar1->Position && sub <= ScrollBar2->Position) { sub = 0; } else { sub = 255; } ptr4[x1*3 ] = (Byte)sub; ptr4[x1*3 1] = (Byte)sub; ptr4[x1*3 2] = (Byte)sub; } } Image2->Canvas->Draw(0,0,pBitmap2); Image1->Canvas->Draw(0,0,pBitmap3); Image3->Canvas->Draw(0,0,pBitmap4); delete pBitmap2; delete pBitmap3; delete pBitmap4; } //--------------------------------------------------------------------------- |
taishyang
站務副站長 發表:377 回覆:5490 積分:4563 註冊:2002-10-08 發送簡訊給我 |
|
kk1127pp
一般會員 發表:4 回覆:4 積分:1 註冊:2011-09-30 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |