取圖像的骨架,方法是不斷對圖像進行腐蝕而且標記每一個像素腐蝕到值再也不變化的次數。獲得的圖像能夠進一步被利用,如進行細化。優化
上代碼this
void erode(LPBYTE lpDst, LPBYTE lpDst1, int nW, int nH) { bool flag; for (int j = 0; j < nH; j++) { for (int i = 0; i < nW; i++) { //目標圖像中的當前點先賦成白色 lpDst[j*nW + i] = (unsigned char)255;//前景爲黑色,背景爲白色 if (lpDst1[j*nW + i] == 0) { flag = true; for (int sub_row = -1; sub_row < 2; sub_row++) { for (int sub_col = -1; sub_col < 2; sub_col++) { if (j + sub_row < nH&&j + sub_row >= 0 && i + sub_col < nW&&i + sub_col >= 0) { if (lpDst1[(j + sub_row)*nW + i + sub_col] != 0) { flag = false; break; } } } if (!flag) break; } if (flag) lpDst[j*nW + i] = 0; } } } for (int j = 0; j < nH; j++) { for (int i = 0; i < nW; i++) { lpDst1[j*nW + i] = lpDst[j*nW + i]; } } } void CImageColorProcess::DistanceTransform(LPBYTE lpSrc, LPBYTE lpDst, LPBYTE lpDst_, int nSrcCount, int nW, int nH) { this->OSTUThreshold(lpSrc, lpDst_, nSrcCount, nW, nH); int*array = new int[nW*nH];//存儲每一個像素點被腐蝕掉以前所經歷的腐蝕次數 //memset(array, 255, nW*nH);//初始化 for (int j = 0; j < nH; j++) { for (int i = 0; i < nW; i++) { array[j*nW + i] = 255; } } bool flag1 = true;//設置標誌 int count(0);//記錄腐蝕操做的次數 while (flag1) { erode(lpDst, lpDst_, nW, nH);//標記爲真進行腐蝕 for (int j = 0; j < nH; j++) { for (int i = 0; i < nW; i++) { if (lpDst[j*nW + i] == 0) array[j*nW + i] = array[j*nW + i]-5; } } for (int j = 0; j < nH; j++) { flag1 = false; for (int i = 0; i < nW; i++) { if (lpDst[j*nW + i] == 0) { flag1 = true; count++; break; } } if (flag1) break; } } for (int j = 0; j < nH; j++) { for (int i = 0; i < nW; i++) { lpDst[j*nW + i] = (array[j*nW + i] < 0 ? 0 : array[j*nW + i]); } } }還能夠繼續優化下
版權聲明:調試