简体   繁体   English

在C ++中执行Sobel筛选器功能时我做错了什么

[英]What am I doing wrong when executing the sobel filter function in c++

Here is my sobel filter function performed on a grayscale image. 这是我在灰度图像上执行的sobel滤镜功能。 Apparently I'm not doing my calculations correct because I keep getting an all black image. 显然我的计算不正确,因为我一直得到全黑图像。 I have already turned in the project but it is bothering me that the results aren't right. 我已经提交了该项目,但是结果不正确令我感到困扰。

int sobelH[3][3] = { -1, 0, 1, 
                    -2, 0, 2, 
                    -1, 0, 1 },

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

//variable declaration
int mag;
int pix_x, pix_y = 0;
int img_x, img_y;

for (img_x = 0; img_x < img->x; img_x++)
{
    for (img_y = 0; img_y < img->y; img_y++)
    {
            pix_x = 0;
            pix_y = 0;

            //calculating the X and Y convolutions
            for (int i = -1; i <= 1; i++)
            {
                for (int j = -1; j <= 1; j++)
                {
                    pix_x += (img->data[img_y * img->x + img_x].red + img->data[img_y * img->x + img_x].green + img->data[img_y * img->x + img_x].blue) * sobelH[1 + i][1 + j];
                    pix_y += (img->data[img_y * img->x + img_x].red + img->data[img_y * img->x + img_x].green + img->data[img_y * img->x + img_x].blue) * sobelV[1 + i][1 + j];
                }
            }

        //Gradient magnitude
        mag = sqrt((pix_x * pix_x) + (pix_y * pix_y));

        if (mag > RGB_COMPONENT_COLOR)
            mag = 255;
        if (mag < 0)
            mag = 0;

        //Setting the new pixel value
        img->data[img_y * img->x + img_x].red = mag;
        img->data[img_y * img->x + img_x].green = mag;
        img->data[img_y * img->x + img_x].blue = mag;
    }
}

Although your code could use some improvement, the main reason is that you compute the convolution at constant img_y and img_x . 尽管您的代码可以使用一些改进,但是主要原因是您以常数img_yimg_x计算卷积。 What you need to do is: 您需要做的是:

pix_x += (img->data[img_y * img->x + img_x + i].red + img->data[img_y * img->x + img_x + i].green + img->data[img_y * img->x + img_x + i].blue) * sobelH[1 + i][1 + j];

Indeed, the Sobel convolution is symmetric, so if you compute the convolution with a constant image, it will result in only black. 实际上,Sobel卷积是对称的,因此,如果使用恒定图像计算卷积,则只会产生黑色。

Note that in the above example I do not take into account the border of the image. 请注意,在上面的示例中,我没有考虑图像的边框。 You should make sure to not access pixels that are outside your pixel array. 您应该确保不要访问像素阵列之外的像素。

Another mistake is that you're writing in the input image. 另一个错误是您在输入图像中书写。 You write at location (x,y), then compute the filter result for location (x+1,y) using the modified value at (x,y), which is the wrong value to use. 您在位置(x,y)处进行写入,然后使用(x,y)处的修改后的值来计算位置(x + 1,y)的过滤结果,这是使用错误的值。

You need to write your result to a new image. 您需要将结果写入新图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM