?? morph.cpp
字號:
//則將目標圖像中的當前點賦成白色 for (n = 0;n < 3;n++ ) { pixel = *(lpSrc+n-1); if (pixel == 255 ) { *lpDst = (unsigned char)255; break; } } } } } else if(nMode == 1) { //使用垂直方向的結構元素進行腐蝕 for(j = 1; j <lHeight-1; j++) { for(i = 0;i <lWidth; i++) { //由于使用1×3的結構元素,為防止越界,所以不處理最上邊和最下邊的兩列像素 // 指向源圖像倒數第j行,第i個象素的指針 lpSrc = (char *)lpDIBBits + lWidth * j + i; // 指向目標圖像倒數第j行,第i個象素的指針 lpDst = (char *)lpNewDIBBits + lWidth * j + i; //取得當前指針處的像素值,注意要轉換為unsigned char型 pixel = (unsigned char)*lpSrc; //目標圖像中含有0和255外的其它灰度值 if(pixel != 255 && *lpSrc != 0) return FALSE; //目標圖像中的當前點先賦成黑色 *lpDst = (unsigned char)0; //如果源圖像中當前點自身或者上下有一個點不是黑色, //則將目標圖像中的當前點賦成白色 for (n = 0;n < 3;n++ ) { pixel = *(lpSrc+(n-1)*lWidth); if (pixel == 255 ) { *lpDst = (unsigned char)255; break; } } } } } else { //使用自定義的結構元素進行腐蝕 for(j = 1; j <lHeight-1; j++) { for(i = 0;i <lWidth; i++) { //由于使用3×3的結構元素,為防止越界,所以不處理最左邊和最右邊的兩列像素 //和最上邊和最下邊的兩列像素 // 指向源圖像倒數第j行,第i個象素的指針 lpSrc = (char *)lpDIBBits + lWidth * j + i; // 指向目標圖像倒數第j行,第i個象素的指針 lpDst = (char *)lpNewDIBBits + lWidth * j + i; //取得當前指針處的像素值,注意要轉換為unsigned char型 pixel = (unsigned char)*lpSrc; //目標圖像中含有0和255外的其它灰度值 if(pixel != 255 && *lpSrc != 0) return FALSE; //目標圖像中的當前點先賦成黑色 *lpDst = (unsigned char)0; //如果原圖像中對應結構元素中為黑色的那些點中有一個不是黑色, //則將目標圖像中的當前點賦成白色 //注意在DIB圖像中內容是上下倒置的 for (m = 0;m < 3;m++ ) { for (n = 0;n < 3;n++) { if( structure[m][n] == -1) continue; pixel = *(lpSrc + ((2-m)-1)*lWidth + (n-1)); if (pixel == 255 ) { *lpDst = (unsigned char)255; break; } } } } } } // 復制腐蝕后的圖像 memcpy(lpDIBBits, lpNewDIBBits, lWidth * lHeight); // 重新初始化新分配的內存,設定初始值為255 //lpDst = (char *)lpNewDIBBits; //memset(lpDst, (BYTE)255, lWidth * lHeight); if(nMode == 0) { //使用水平方向的結構元素進行膨脹 for(j = 0; j <lHeight; j++) { for(i = 1;i <lWidth-1; i++) { //由于使用1×3的結構元素,為防止越界,所以不處理最左邊和最右邊的兩列像素 // 指向源圖像倒數第j行,第i個象素的指針 lpSrc = (char *)lpDIBBits + lWidth * j + i; // 指向目標圖像倒數第j行,第i個象素的指針 lpDst = (char *)lpNewDIBBits + lWidth * j + i; //取得當前指針處的像素值,注意要轉換為unsigned char型 pixel = (unsigned char)*lpSrc; //目標圖像中含有0和255外的其它灰度值 if(pixel != 255 && *lpSrc != 0) return FALSE; //目標圖像中的當前點先賦成白色 *lpDst = (unsigned char)255; //源圖像中當前點自身或者左右只要有一個點是黑色, //則將目標圖像中的當前點賦成黑色 for (n = 0;n < 3;n++ ) { pixel = *(lpSrc+n-1); if (pixel == 0 ) { *lpDst = (unsigned char)0; break; } } } } } else if(nMode == 1) { //使用垂直方向的結構元素進行膨脹 for(j = 1; j <lHeight-1; j++) { for(i = 0;i <lWidth; i++) { //由于使用1×3的結構元素,為防止越界,所以不處理最上邊和最下邊的兩列像素 // 指向源圖像倒數第j行,第i個象素的指針 lpSrc = (char *)lpDIBBits + lWidth * j + i; // 指向目標圖像倒數第j行,第i個象素的指針 lpDst = (char *)lpNewDIBBits + lWidth * j + i; //取得當前指針處的像素值,注意要轉換為unsigned char型 pixel = (unsigned char)*lpSrc; //目標圖像中含有0和255外的其它灰度值 if(pixel != 255 && *lpSrc != 0) return FALSE; //目標圖像中的當前點先賦成白色 *lpDst = (unsigned char)255; //源圖像中當前點自身或者上下只要有一個點是黑色, //則將目標圖像中的當前點賦成黑色 for (n = 0;n < 3;n++ ) { pixel = *(lpSrc+(n-1)*lWidth); if (pixel == 0 ) { *lpDst = (unsigned char)0; break; } } } } } else { //使用自定義的結構元素進行膨脹 for(j = 1; j <lHeight-1; j++) { for(i = 0;i <lWidth; i++) { //由于使用3×3的結構元素,為防止越界,所以不處理最左邊和最右邊的兩列像素 //和最上邊和最下邊的兩列像素 // 指向源圖像倒數第j行,第i個象素的指針 lpSrc = (char *)lpDIBBits + lWidth * j + i; // 指向目標圖像倒數第j行,第i個象素的指針 lpDst = (char *)lpNewDIBBits + lWidth * j + i; //取得當前指針處的像素值,注意要轉換為unsigned char型 pixel = (unsigned char)*lpSrc; //目標圖像中含有0和255外的其它灰度值 if(pixel != 255 && *lpSrc != 0) return FALSE; //目標圖像中的當前點先賦成白色 *lpDst = (unsigned char)255; //原圖像中對應結構元素中為黑色的那些點中只要有一個是黑色, //則將目標圖像中的當前點賦成黑色 //注意在DIB圖像中內容是上下倒置的 for (m = 0;m < 3;m++ ) { for (n = 0;n < 3;n++) { if( structure[m][n] == -1) continue; pixel = *(lpSrc + ((2-m)-1)*lWidth + (n-1)); if (pixel == 0 ) { *lpDst = (unsigned char)0; break; } } } } } } // 復制膨脹后的圖像 memcpy(lpDIBBits, lpNewDIBBits, lWidth * lHeight); // 釋放內存 LocalUnlock(hNewDIBBits); LocalFree(hNewDIBBits); return TRUE;}/************************************************************************* * * 函數名稱: * CloseDIB() * * 參數: * LPSTR lpDIBBits - 指向源DIB圖像指針 * LONG lWidth - 源圖像寬度(象素數,必須是4的倍數) * LONG lHeight - 源圖像高度(象素數) * int nMode - 閉運算方式,0表示水平方向,1表示垂直方向,2表示自定義結構元素。 * int structure[3][3] - 自定義的3×3結構元素。 * * 返回值: * BOOL - 閉運算成功返回TRUE,否則返回FALSE。 * * 說明: * 該函數用于對圖像進行開運算。結構元素為水平方向或垂直方向的三個點,中間點位于原點; * 或者由用戶自己定義3×3的結構元素。 * * 要求目標圖像為只有0和255兩個灰度值的灰度圖像。 ************************************************************************//************************************************************************* * * 函數名稱: * ThinDIB() * * 參數: * LPSTR lpDIBBits - 指向源DIB圖像指針 * LONG lWidth - 源圖像寬度(象素數,必須是4的倍數) * LONG lHeight - 源圖像高度(象素數) * * 返回值: * BOOL - 閉運算成功返回TRUE,否則返回FALSE。 * * 說明: * 該函數用于對圖像進行細化運算。 * * 要求目標圖像為只有0和255兩個灰度值的灰度圖像。 ************************************************************************/BOOL WINAPI CloseDIB(LPSTR lpDIBBits, LONG lWidth, LONG lHeight, int nMode , int structure[3][3]){ if (DilationDIB(lpDIBBits, lWidth, lHeight, nMode , structure)) { if (ErosionDIB(lpDIBBits, lWidth, lHeight, nMode , structure)) { // 返回 return TRUE; } } return FALSE; // 返回 return TRUE;}BOOL WINAPI ThiningDIB(LPSTR lpDIBBits, LONG lWidth, LONG lHeight){ // 指向源圖像的指針 LPSTR lpSrc; // 指向緩存圖像的指針 LPSTR lpDst; // 指向緩存DIB圖像的指針 LPSTR lpNewDIBBits; HLOCAL hNewDIBBits; //臟標記 BOOL bModified; //循環變量 long i; long j; int n; int m; //四個條件 BOOL bCondition1; BOOL bCondition2; BOOL bCondition3; BOOL bCondition4; //計數器 unsigned char nCount; //像素值 unsigned char pixel; //5×5相鄰區域像素值 unsigned char neighbour[5][5]; // 暫時分配內存,以保存新圖像 hNewDIBBits = LocalAlloc(LHND, lWidth * lHeight); if (hNewDIBBits == NULL) { // 分配內存失敗 return FALSE; } // 鎖定內存 lpNewDIBBits = (char * )LocalLock(hNewDIBBits); // 初始化新分配的內存,設定初始值為255 lpDst = (char *)lpNewDIBBits; memset(lpDst, (BYTE)255, lWidth * lHeight); bModified=TRUE; while(bModified) { bModified = FALSE; // 初始化新分配的內存,設定初始值為255 lpDst = (char *)lpNewDIBBits; memset(lpDst, (BYTE)255, lWidth * lHeight); for(j = 2; j <lHeight-2; j++) { for(i = 2;i <lWidth-2; i++) { bCondition1 = FALSE; bCondition2 = FALSE; bCondition3 = FALSE; bCondition4 = FALSE; //由于使用5×5的結構元素,為防止越界,所以不處理外圍的幾行和幾列像素 // 指向源圖像倒數第j行,第i個象素的指針 lpSrc = (char *)lpDIBBits + lWidth * j + i; // 指向目標圖像倒數第j行,第i個象素的指針 lpDst = (char *)lpNewDIBBits + lWidth * j + i; //取得當前指針處的像素值,注意要轉換為unsigned char型 pixel = (unsigned char)*lpSrc; //目標圖像中含有0和255外的其它灰度值 if(pixel != 255 && *lpSrc != 0) //return FALSE; continue; //如果源圖像中當前點為白色,則跳過 else if(pixel == 255) continue; //獲得當前點相鄰的5×5區域內像素值,白色用0代表,黑色用1代表 for (m = 0;m < 5;m++ ) { for (n = 0;n < 5;n++) { neighbour[m][n] =(255 - (unsigned char)*(lpSrc + ((4 - m) - 2)*lWidth + n - 2 )) / 255; } }// neighbour[][] //逐個判斷條件。 //判斷2<=NZ(P1)<=6 nCount = neighbour[1][1] + neighbour[1][2] + neighbour[1][3] \ + neighbour[2][1] + neighbour[2][3] + \ + neighbour[3][1] + neighbour[3][2] + neighbour[3][3]; if ( nCount >= 2 && nCount <=6) bCondition1 = TRUE; //判斷Z0(P1)=1 nCount = 0; if (neighbour[1][2] == 0 && neighbour[1][1] == 1) nCount++; if (neighbour[1][1] == 0 && neighbour[2][1] == 1) nCount++; if (neighbour[2][1] == 0 && neighbour[3][1] == 1) nCount++; if (neighbour[3][1] == 0 && neighbour[3][2] == 1) nCount++; if (neighbour[3][2] == 0 && neighbour[3][3] == 1) nCount++; if (neighbour[3][3] == 0 && neighbour[2][3] == 1) nCount++; if (neighbour[2][3] == 0 && neighbour[1][3] == 1) nCount++; if (neighbour[1][3] == 0 && neighbour[1][2] == 1) nCount++; if (nCount == 1) bCondition2 = TRUE; //判斷P2*P4*P8=0 or Z0(p2)!=1 if (neighbour[1][2]*neighbour[2][1]*neighbour[2][3] == 0) bCondition3 = TRUE; else { nCount = 0; if (neighbour[0][2] == 0 && neighbour[0][1] == 1) nCount++; if (neighbour[0][1] == 0 && neighbour[1][1] == 1) nCount++; if (neighbour[1][1] == 0 && neighbour[2][1] == 1) nCount++; if (neighbour[2][1] == 0 && neighbour[2][2] == 1) nCount++; if (neighbour[2][2] == 0 && neighbour[2][3] == 1) nCount++; if (neighbour[2][3] == 0 && neighbour[1][3] == 1) nCount++; if (neighbour[1][3] == 0 && neighbour[0][3] == 1) nCount++; if (neighbour[0][3] == 0 && neighbour[0][2] == 1) nCount++; if (nCount != 1) bCondition3 = TRUE; } //判斷P2*P4*P6=0 or Z0(p4)!=1 if (neighbour[1][2]*neighbour[2][1]*neighbour[3][2] == 0) bCondition4 = TRUE; else { nCount = 0; if (neighbour[1][1] == 0 && neighbour[1][0] == 1) nCount++; if (neighbour[1][0] == 0 && neighbour[2][0] == 1) nCount++; if (neighbour[2][0] == 0 && neighbour[3][0] == 1) nCount++; if (neighbour[3][0] == 0 && neighbour[3][1] == 1) nCount++; if (neighbour[3][1] == 0 && neighbour[3][2] == 1) nCount++; if (neighbour[3][2] == 0 && neighbour[2][2] == 1) nCount++; if (neighbour[2][2] == 0 && neighbour[1][2] == 1) nCount++; if (neighbour[1][2] == 0 && neighbour[1][1] == 1) nCount++; if (nCount != 1) bCondition4 = TRUE; } if(bCondition1 && bCondition2 && bCondition3 && bCondition4) { *lpDst = (unsigned char)255; bModified = TRUE; } else { *lpDst = (unsigned char)0; } } } // 復制腐蝕后的圖像 memcpy(lpDIBBits, lpNewDIBBits, lWidth * lHeight); } // 復制腐蝕后的圖像 memcpy(lpDIBBits, lpNewDIBBits, lWidth * lHeight); // 釋放內存 LocalUnlock(hNewDIBBits); LocalFree(hNewDIBBits); // 返回 return TRUE;}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -