图像的轮廓提取在数字图像处理第三版(中文版 冈萨雷斯)第九章412页,讲得很清楚,在此就不赘述了。
将腐蚀和相减化作一步,上代码
BOOL CImageColorProcess::Contour(LPBYTE lpSrc, LPBYTE lpDst, LPBYTE lpDst_, int nSrcCount, int nW, int nH)
{
this->OSTUThreshold(lpSrc, lpDst_, nSrcCount, nW, nH);//首先大律法二值化
for (int i = 0; i < nH; i++)
{
for (int j = 0; j < nW; j++)
{
lpDst[i*nW + j] = 255;
}
}
for (int i = 1; i < nH - 1; i++)
{
for (int j = 1; j < nW - 1; j++)
{
if (lpDst_[i*nW + j] == 0)
{
lpDst[i*nW + j] = 0;
int nw = lpDst_[(i + 1)*nW + j - 1];
int n = lpDst_[(i + 1)*nW + j];
int ne = lpDst_[(i + 1)*nW + j + 1];
int w = lpDst_[i*nW + j - 1];
int e = lpDst_[i*nW + j + 1];
int sw = lpDst_[(i - 1)*nW + j - 1];
int s = lpDst_[(i - 1)*nW + j];
int se = lpDst_[(i - 1)*nW + j + 1];
if (nw + n + ne + e + sw + se + s + w == 0)
{
lpDst[i*nW + j] = 255;
}
}
}
}
return true;
}