Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ sobel过滤算法(C+;+;)(无库)_C++_Algorithm_Image Processing_Sobel - Fatal编程技术网

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

C++ sobel过滤算法(C+;+;)(无库),c++,algorithm,image-processing,sobel,C++,Algorithm,Image Processing,Sobel,鉴于我访问图片像素的方法,我尝试将sobel滤波算法应用于给定图片(本例中为灰度)。由于我是以一种不使用库的方式访问它们的,所以我很难弄清楚如何应用这种方法给出的算法。代码的第一部分只是访问像素数据: 第1部分: CKingimageDoc* pDoc = GetDocument(); // get picture int iBitPerPixel = pDoc->_bmp->bitsperpixel; // used to see if grayscale(8 b

鉴于我访问图片像素的方法,我尝试将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 } };
双磁感应=0.0;//这是你的震级
对于(int a=0;a<3;a++){
对于(int b=0;b<3;b++){
magX+=pImg[i*Wp+j]*kernelx[a][b];//我哪里弄糊涂了
}
}
}

非常感谢所有帮助。

您必须使用中心像素附近的适当像素与内核条目相乘:

//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;  
   }
}
//行、列-用于计算的中心像素坐标
对于(int row=1;row
我省略了边框像素

CKingimageDoc*pDoc=GetDocument();//了解情况
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;
        }
    }
}
int-iBitPerPixel=pDoc->_-bmp->bitsperpixel;//用于查看灰度(8b)还是RGB(24b) intiWidth=pDoc->\u bmp->宽度; intiheight=pDoc->\u bmp->高度; 字节*pImg=pDoc->\u bmp->点;//用于指向图像中像素的指针 const int area=iWidth*iHeight; 字节*pImg2=新字节[区域]; if(iBitPerPixel==8)//灰度8位图像 { int像素_x; int像素_y; 浮点数sobel_x[3][3]= { { -1, 0, 1 }, { -2, 0, 2 }, { -1, 0, 1 } }; 浮动索贝尔_y[3][3]= { { -1, -2, -1 }, { 0, 0, 0 }, { 1, 2, 1 } }; 对于(intx=1;x255)val=255; pImg2[iHeight*y+x]=val; } } }
所以你的问题是。。。迭代图像?@Charles是的,本质上我想迭代图像,并按照算法描述对每个像素应用sobel滤波器。-2 at for循环应为-1,与
iHeight相乘
应与
iWidth相乘,在计算像素_y时,在最后一个标记之前缺少+1。@geza notes现已修复。对于边界像素,如果大于255,则设置为255;如果小于0,则设置为0?边界像素-第一列和最后一列,第一行和最后一行。0/255-饱和问题-应适用于任何地方
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;
        }
    }
}