【发布时间】:2017-12-16 06:58:55
【问题描述】:
鉴于我访问图片像素的方法,我正在尝试将 sobel 过滤算法应用于给定图片(在本例中为灰度)。由于我以不使用库的方式访问它们,因此我无法弄清楚如何应用这种方法的算法。代码的第一部分只是访问像素数据:
第 1 部分:
CKingimageDoc* pDoc = GetDocument(); // get picture
int iBitPerPixel = pDoc->_bmp->bitsperpixel; // used to see if grayscale(8 bits) or RGB (24 bits)
int iWidth = pDoc->_bmp->width;
int iHeight = pDoc->_bmp->height;
BYTE *pImg = pDoc->_bmp->point; // pointer used to point at pixels in the image
const int area = iWidth * iHeight;
int Wp = iWidth;
int intensity;
if (iBitPerPixel == 8) ////Grayscale 8 bits image
{
int r = iWidth % 4; // pixels leftover from width (remainder has to be factor of 8 or 24)
int p = (4-r) % 4; // has to be a factor of number of bits in pixel, num leftover to take care of
Wp = iWidth + p;
第二部分(sobel滤波器算法的实际应用):
float kernelx[3][3] = { { -1, 0, 1 },
{ -2, 0, 2 },
{ -1, 0, 1 } };
float kernely[3][3] = { { -1, -2, -1 },
{ 0, 0, 0 },
{ 1, 2, 1 } };
double magX = 0.0; // this is your magnitude
for (int a = 0; a < 3; a++) {
for (int b = 0; b < 3; b++) {
magX += pImg[i*Wp + j] * kernelx[a][b]; // where i get confused
}
}
}
非常感谢任何和所有帮助。
【问题讨论】:
-
所以你的问题是......遍历图像?
-
@Charles 是的,本质上我想遍历图像并将 sobel 过滤器应用于算法描述的每个像素。
标签: c++ algorithm image-processing sobel