关于图像对比问题,得到了变化区域,但是出问题了
程序代码:
GetScreen1(); //得到第一张图片 Sleep(3000); //三秒是屏幕变化一下 GetScreen2(); //得到第二章图片 cmp(pbitmaps1 , pbitmaps2); //比较变化区域 kks(); //将变化区域填充到第一张图片里面 display(pbitmaps1); //显示图像
下面是函数的实现
程序代码:
void CalgrDlg::GetScreen1() //获取屏幕内存地址 { CDC *pdeskdc; pdeskdc = GetDesktopWindow()->GetWindowDC(); // CDC memdc; // memdc.CreateCompatibleDC(pdeskdc); bmp1.m_hObject = ::CreateDIBSection( memdc.m_hDC , &bi ,DIB_RGB_COLORS ,(LPVOID*)&pbitmaps1 , NULL , 0 ); CBitmap *oldbitmap = memdc.SelectObject(&bmp1); memdc.BitBlt( 0 , 0 ,nWidth , nHeith ,pdeskdc , 0,0,SRCCOPY ); memdc.SelectObject( oldbitmap ); // memdc.DeleteDC(); pdeskdc = NULL ; } void CalgrDlg::GetScreen2( ) //获取屏幕内存地址 { CDC *pdeskdc; pdeskdc = GetDesktopWindow()->GetWindowDC(); // CDC memdc; // memdc.CreateCompatibleDC(pdeskdc); bmp2.m_hObject = ::CreateDIBSection( memdc.m_hDC , &bi ,DIB_RGB_COLORS ,(LPVOID*)&pbitmaps2 , NULL , 0 ); CBitmap *oldbitmap = memdc.SelectObject(&bmp2); memdc.BitBlt( 0 , 0 ,nWidth , nHeith ,pdeskdc , 0,0,SRCCOPY ); memdc.SelectObject( oldbitmap ); // memdc.DeleteDC(); pdeskdc = NULL ; } void CalgrDlg::cmp( BYTE *pmap1,BYTE *pmap2 ) //对比图片的不同之处 { POINT p; int a[30]; memset(a , -1 , sizeof(a)); int ret = 0 ; int i = 0 , j = 0 ; int index; for (j = 0 ; j < nHeith ; j += 4) { for (i = 0 ; i < indexWidth ; ++i ) { if (a[i] <= j ) { index = int(j*iWidthbits + i*192) ; if ( i == indexWidth - 1) { ret = ret = memcmp( (void*)&pmap1[index] , (void*)&pmap2[index] , iWidthbits % 192 ); } else { ret = memcmp( (void*)&pmap1[index] , (void*)&pmap2[index] , 192 ); } if ( ret != 0 ) { p.x = i ; p.y = j / 96; vPoint.push_back( p ); //将变化添加到向量里面 //a[i] = (p.y+1) *96 ; } } } } } void CalgrDlg::display(BYTE *pmap) { CRect re; GetWindowRect(&re); StretchDIBits( GetWindowDC()->m_hDC , 0,0,re.Width()-50,re.Height() - 50 ,0,0,nWidth,nHeith,pmap , &bi, DIB_RGB_COLORS , SRCCOPY ); } void CalgrDlg::kks() //填充函数 { int index; for ( int j = 0 ; j < vPoint.size() ; ++j ) { for (int i = 0 ; i < 96 ; ++i ) { index = (vPoint[j].y+i) * iWidthbits + vPoint[j].x * 192 ; memcpy( (void*)&pbitmaps1[index] , (void*)&pbitmaps2[index] , 192 ); } } }