我正在尝试为二进制图像实现形态腐蚀操作。我运行后一切都是黑色的。我已将结构元素(int 数组元素)设置为 3x3 正方形。
const int radius = 1;
int element[(radius * 2 + 1)*(radius * 2 + 1)] = { 1,1,1,1,1,1,1,1,1 };
int iBitPerPixel = pDoc->_bmp->bitsperpixel;
int iWidth = pDoc->_bmp->width;
int iHeight = pDoc->_bmp->height;
BYTE *pImg = pDoc->_bmp->point;
int Wp = iWidth;
BYTE copy[319*240*10];
int r = (3 * iWidth) % 6;
int p = (6 - r) % 6;
Wp = 3 * iWidth + p;
for (int i = iHeight - 1; i >= 0; i--)
for (int j = 0; j < iWidth; j++)
{
copy[i*Wp + j * 3] = pImg[i*Wp + j * 3];
pImg[i*Wp + j * 3] = 0;
pImg[i*Wp + j * 3 + 1] = 0;
pImg[i*Wp + j * 3 + 2] = 0;
}
bool fit = true;
for (int i = iHeight - 1; i >= 0; i--)
for (int j = 0; j < iWidth; j++)
{
for (int x = radius; x <= -radius; x--)
for (int y = -radius; y <= radius; y++)
{
if (i + x < 0 || i + x >= iHeight|| j + y < 0) continue;
if (element[(x + radius)*radius + y + radius] == 1 && copy[(i + x)*Wp + (j + y) * 3] == 0)
{
fit = false;
break;
}
}
if (fit)
for (int x = radius; x <= -radius; x--)
for (int y = -radius; y <= radius; y++)
pImg[(i + x)*Wp + (j + y) * 3] = 255;
}