俺就给你写个sobel的,你把sobel模板换成robert模板就OK了。
本来sobel找阈值还有个小算法,不过一般不要求的,俺就用黄金分割点乘以255替代了。
sobel卷积代码如下:
void CSobelDlg::CreateSobolImage(void)
{
static const int sizeOfSobelMask = 9;
static int sobelMaskHor[sizeOfSobelMask] =
{
-1, -2, -1,
0, 0, 0,
1, 2, 1
};
static int SobelMaskVer[sizeOfSobelMask] =
{
1, 0, -1,
2, 0, -2,
1, 0, 1
};
int numOfBytes = m_bmpInfo.bmWidthBytes * m_bmpInfo.bmHeight;
unsigned char* pbuf1 = new unsigned char[numOfBytes];
unsigned char* pbuf2 = new unsigned char[numOfBytes];
m_bmpOrg.GetBitmapBits(numOfBytes, pbuf1);
unsigned char averageColor = 0;
for(int row = 0; row < m_bmpInfo.bmHeight; ++ row)
{
for(int col = 0; col < m_bmpInfo.bmWidth; ++col)
{
averageColor = ( pbuf1[row * m_bmpInfo.bmWidthBytes + col * m_bmpInfo.bmBitsPixel / 8 + 0] +
pbuf1[row * m_bmpInfo.bmWidthBytes + col * m_bmpInfo.bmBitsPixel / 8 + 1] +
pbuf1[row * m_bmpInfo.bmWidthBytes + col * m_bmpInfo.bmBitsPixel / 8 + 2] ) / 3;
pbuf1[row * m_bmpInfo.bmWidthBytes + col * m_bmpInfo.bmBitsPixel / 8 + 0] = averageColor;
}
}
unsigned char ts = 0, tv = 0, tmp = 0, dst = 0, idx = 0;
for(int row = 1; row < m_bmpInfo.bmHeight - 1; ++ row)
{
for(int col = 1; col < m_bmpInfo.bmWidth - 1; ++col)
{
idx = ts = tv = 0;
for(int r = row - 1; r <= row + 1; ++r)
{
for(int c = col - 1; c <= col + 1; ++c)
{
tmp = pbuf1[r * m_bmpInfo.bmWidthBytes + c * m_bmpInfo.bmBitsPixel / 8];
ts += (sobelMaskHor[idx] * tmp );
tv += (SobelMaskVer[idx] * tmp );
++idx;
}
}
dst = (unsigned char)sqrt( (float)(ts * ts + tv * tv) );
if(dst > (unsigned char)(0.6180339887 * 255.0) ) {
pbuf2[row * m_bmpInfo.bmWidthBytes + col * m_bmpInfo.bmBitsPixel / 8 + 0] = 0;
pbuf2[row * m_bmpInfo.bmWidthBytes + col * m_bmpInfo.bmBitsPixel / 8 + 1] = 255;
pbuf2[row * m_bmpInfo.bmWidthBytes + col * m_bmpInfo.bmBitsPixel / 8 + 2] = 0;
pbuf2[row * m_bmpInfo.bmWidthBytes + col * m_bmpInfo.bmBitsPixel / 8 + 3] = 255;
}
else {
pbuf2[row * m_bmpInfo.bmWidthBytes + col * m_bmpInfo.bmBitsPixel / 8 + 0] = 0;
pbuf2[row * m_bmpInfo.bmWidthBytes + col * m_bmpInfo.bmBitsPixel / 8 + 1] = 0;
pbuf2[row * m_bmpInfo.bmWidthBytes + col * m_bmpInfo.bmBitsPixel / 8 + 2] = 255;
pbuf2[row * m_bmpInfo.bmWidthBytes + col * m_bmpInfo.bmBitsPixel / 8 + 3] = 255;
}
}
}
m_bmpSobol.CreateBitmap(m_bmpInfo.bmWidth, m_bmpInfo.bmHeight, 1, 32, pbuf2);
delete []pbuf1;
delete []pbuf2;
}
再给你一张本程序运行的效果图。