一、图片裁剪
int CJSAutoWidget :: GetHouseNo ( cv:: Mat matMap)
{ cv:: imwrite ( m_strPath + "/Data/map.png" , matMap) ; for ( int i = 0 ; i < 4 ; ++ i) { for ( int j = 0 ; j < 6 ; ++ j) { cv:: Rect roi ( j * 20 , i * 17 , 20 , 17 ) ; cv:: Mat smallImage = matMap ( roi) ; cv:: imwrite ( m_strPath + "/Data/role_" + std:: to_string ( i * 6 + j + 1 ) + ".png" , smallImage) ; std:: cout << " " << i * 6 + j + 1 << std:: endl; if ( IsRed ( smallImage) ) { std:: cout << "红色房间" << std:: endl; } else { std:: cout << "不是红色房间" << std:: endl; } } } return 0 ;
}
二、平均哈希相似度判断
int CJSAutoWidget :: GetDiffAvgHash ( cv:: Mat matSrc1, cv:: Mat matSrc2)
{ cv:: Mat matDst1, matDst2; cv:: resize ( matSrc1, matDst1, cv:: Size ( 8 , 8 ) , 0 , 0 , cv:: INTER_CUBIC) ; cv:: resize ( matSrc2, matDst2, cv:: Size ( 8 , 8 ) , 0 , 0 , cv:: INTER_CUBIC) ; cv:: cvtColor ( matDst1, matDst1, cv:: COLOR_BGR2GRAY) ; cv:: cvtColor ( matDst2, matDst2, cv:: COLOR_BGR2GRAY) ; int nAvg1 = 0 , nAvg2 = 0 ; int nArr1[ 64 ] , nArr2[ 64 ] ; for ( int i = 0 ; i < 8 ; i++ ) { uchar* data1 = matDst1. ptr < uchar> ( i) ; uchar* data2 = matDst2. ptr < uchar> ( i) ; int nTmp = i * 8 ; for ( int j = 0 ; j < 8 ; j++ ) { int nTmp1 = nTmp + j; nArr1[ nTmp1] = data1[ j] / 4 * 4 ; nArr2[ nTmp1] = data2[ j] / 4 * 4 ; nAvg1 += nArr1[ nTmp1] ; nAvg2 += nArr2[ nTmp1] ; } } nAvg1 /= 64 ; nAvg2 /= 64 ; for ( int i = 0 ; i < 64 ; i++ ) { nArr1[ i] = ( nArr1[ i] >= nAvg1) ? 1 : 0 ; nArr2[ i] = ( nArr2[ i] >= nAvg2) ? 1 : 0 ; } int nDiffNum = 0 ; for ( int i = 0 ; i < 64 ; i++ ) { if ( nArr1[ i] != nArr2[ i] ) { ++ nDiffNum; } } return nDiffNum;
}
三、获取游戏窗口图片
QImage CJSAutoWidget :: GetDNFImage ( )
{ HWND hwnd = FindWindowA ( NULL , m_strGameName. c_str ( ) ) ; if ( hwnd == NULL ) { std:: cout << "无法找到游戏窗口" << std:: endl; return QImage ( ) ; } RECT rect; :: GetWindowRect ( hwnd, & rect) ; QImage screenImage = QGuiApplication :: primaryScreen ( ) -> grabWindow ( ( WId) hwnd) . toImage ( ) ; return screenImage;
} void CJSAutoWidget :: SlotCaptureImage ( )
{ m_pTimer-> stop ( ) ; QImage image = GetDNFImage ( ) ; if ( image. isNull ( ) ) { return ; } QImage qImageMain = image. scaled ( ui-> label_Map-> size ( ) , Qt:: KeepAspectRatio) ; ui-> label_Main-> setPixmap ( QPixmap :: fromImage ( qImageMain) ) ; int x = image. width ( ) - 125 ; int y = 40 ; int width = 120 ; int height = 70 ; QImage imageMap = image. copy ( x, y, width, height) ; ui-> label_Map-> setPixmap ( QPixmap :: fromImage ( imageMap. scaled ( ui-> label_Map-> size ( ) , Qt:: KeepAspectRatio) ) ) ; cv:: Mat matMap = QImageToMat ( imageMap) ; GetHouseNo ( matMap) ;
}