sobel过滤器算法(C ++)(无库)

时间:2017-07-12 03:05:40

标签: c++ algorithm image-processing sobel

考虑到我访问图片像素的方法,我试图将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;

第2部分(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
        }
    }
}

非常感谢任何和所有帮助。

2 个答案:

答案 0 :(得分:0)

您必须使用来自中心像素邻域的适当像素与内核条目相乘:

//row, col - coordinates of central pixel for calculation
for (int row = 1; row < height - 1; row++) {
    for (int col = 1; col < width - 1; col++) {
        double magX = 0.0; // this is your magnitude

        for (int a = 0; a < 3; a++) {
            for (int b = 0; b < 3; b++) {
                magX += pImg[(row - 1 + a) * Wp + col - 1 + b] * kernelx[a][b];   
            }
        }
        resultImg[row * Wp + col] = magX;  
   }
}

我省略了边框像素

答案 1 :(得分:0)

CKingimageDoc* pDoc = GetDocument(); // get picture

int iBitPerPixel = pDoc->_bmp->bitsperpixel; // used to see if grayscale(8b) or RGB(24b)
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;
BYTE *pImg2 = new BYTE[area];

if (iBitPerPixel == 8) // Grayscale 8bit image
{
    int pixel_x;
    int pixel_y;

    float sobel_x[3][3] =
    { { -1, 0, 1 },
      { -2, 0, 2 },
      { -1, 0, 1 } };

    float sobel_y[3][3] =
    { { -1, -2, -1 },
      { 0,  0,  0 },
      { 1,  2,  1 } };

    for (int x=1; x < iWidth-1; x++)
    {
        for (int y=1; y < iHeight-1; y++)
        {

            pixel_x = (sobel_x[0][0] * pImg[iWidth * (y-1) + (x-1)])
                    + (sobel_x[0][1] * pImg[iWidth * (y-1) +  x   ])
                    + (sobel_x[0][2] * pImg[iWidth * (y-1) + (x+1)])
                    + (sobel_x[1][0] * pImg[iWidth *  y    + (x-1)])
                    + (sobel_x[1][1] * pImg[iWidth *  y    +  x   ])
                    + (sobel_x[1][2] * pImg[iWidth *  y    + (x+1)])
                    + (sobel_x[2][0] * pImg[iWidth * (y+1) + (x-1)])
                    + (sobel_x[2][1] * pImg[iWidth * (y+1) +  x   ])
                    + (sobel_x[2][2] * pImg[iWidth * (y+1) + (x+1)]);

            pixel_y = (sobel_y[0][0] * pImg[iWidth * (y-1) + (x-1)])
                    + (sobel_y[0][1] * pImg[iWidth * (y-1) +  x   ])
                    + (sobel_y[0][2] * pImg[iWidth * (y-1) + (x+1)])
                    + (sobel_y[1][0] * pImg[iWidth *  y    + (x-1)])
                    + (sobel_y[1][1] * pImg[iWidth *  y    +  x   ])
                    + (sobel_y[1][2] * pImg[iWidth *  y    + (x+1)])
                    + (sobel_y[2][0] * pImg[iWidth * (y+1) + (x-1)])
                    + (sobel_y[2][1] * pImg[iWidth * (y+1) +  x   ])
                    + (sobel_y[2][2] * pImg[iWidth * (y+1) + (x+1)]);

            int val = (int)sqrt((pixel_x * pixel_x) + (pixel_y * pixel_y));

            if(val < 0) val = 0;
            if(val > 255) val = 255;

            pImg2[iHeight * y + x] = val;
        }
    }
}