这个函数原本应该使用边缘检测并应用Sobel滤波器。但是它似乎没有。

huangapple go评论75阅读模式
英文:

So, this function was supposed to use edge detection and apply sobel filter. But well It doesn't

问题

我试图弄清楚我做错了什么。我的意图在标题中 这个函数原本应该使用边缘检测并应用Sobel滤波器。但是它似乎没有。

这是我尝试的代码,显然它不起作用。它只打印一个纯色,边缘也尚未包括。我无法弄清楚这段代码的哪个部分是错误的。它只是一个用C编写的函数。

void edges(int height, int width, RGBTRIPLE image[height][width])
{
    RGBTRIPLE edit[height][width];

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            edit[i][j] = image[i][j];
        }
    }

    for (int q = 1; q < height - 1; q++)
    {
        for (int w = 1; w < width - 1; w++)
        {
            // 这里是对图像进行边缘检测的代码
            // 你可以继续调试和改进这个函数
        }
    }

    return;
}

请注意,这只是一个边缘检测函数的框架,其中还没有实际的边缘检测逻辑。你需要在相应的部分添加代码以完成边缘检测。

英文:

I'm trying to figure out what I did wrong. My intent is in title 这个函数原本应该使用边缘检测并应用Sobel滤波器。但是它似乎没有。

This is the code I tried, and it clearly isn't working. It just print's 1 solid colour, also edges are not included yet. What part is wrong I can't figure it out what part of this code is wrong. It's just a function and it's in C.

void edges(int height, int width, RGBTRIPLE image[height][width])
{
    RGBTRIPLE edit[height][width];

    for (int i = 0; i &lt; height; i++)
    {
        for (int j = 0; j &lt; width; j++)
        {
            edit[i][j] = image[i][j];
        }
    }


    for (int q = 1; q &lt; height - 1; q++)
    {

        for (int w = 1; w &lt; width - 1; w++)
        {

            int redEdit[9];
            int greenEdit[9];
            int blueEdit[9];


            for (int top = 0; top &lt; 3; top++)
            {
                redEdit[top] += edit[q - 1][w - 1 + top].rgbtRed;
                greenEdit[top] += edit[q - 1][w - 1 + top].rgbtGreen;
                blueEdit[top] += edit[q - 1][w - 1 + top].rgbtBlue;
            }

            int midctr = 0;

            for (int mid = 3; mid &lt; 6; mid++)
            {
                redEdit[mid] += edit[q][w - 1 + midctr].rgbtRed;
                greenEdit[mid] += edit[q][w - 1 + midctr].rgbtGreen;
                blueEdit[mid] += edit[q][w - 1 + midctr].rgbtBlue;

                midctr ++;
            }

            int topctr = 0;

            for (int top = 6; top &lt; 9; top++)
            {
                redEdit[top] += edit[q + 1][w - 1 + topctr].rgbtRed;
                greenEdit[top] += edit[q + 1][w - 1 + topctr].rgbtGreen;
                blueEdit[top] += edit[q + 1][w - 1 + topctr].rgbtBlue;

                topctr ++;
            }

            int matrixgx[] = {-1, 0, 1, -2, 0, 2, -1, 0, 1};
            int matrixgy[] = {- 1, -2, -1, 0, 0, 0, 1, 2, 1};

            int redEditgx[9];
            int greenEditgx[9];
            int blueEditgx[9];

            int redEditgy[9];
            int greenEditgy[9];
            int blueEditgy[9];

            for (int mtrx = 0; mtrx &lt; 9; mtrx++)
            {
                redEditgx[mtrx] = redEdit[mtrx] * matrixgx[mtrx];
                greenEditgx[mtrx] = greenEdit[mtrx] * matrixgx[mtrx];
                blueEditgx[mtrx] = blueEdit[mtrx] * matrixgx[mtrx];

                redEditgy[mtrx] = redEdit[mtrx] * matrixgy[mtrx];
                greenEditgy[mtrx] = greenEdit[mtrx] * matrixgy[mtrx];
                blueEditgy[mtrx] = blueEdit[mtrx] * matrixgy[mtrx];
            }

            // now sum up the changes of gx and gt

            int redSumgx = 0;
            int greenSumgx = 0;
            int blueSumgx = 0;

            int redSumgy = 0;
            int greenSumgy = 0;
            int blueSumgy = 0;

            for (int sum = 0; sum &lt; 9; sum++)
            {
                redSumgx += redEditgx[sum];
                greenSumgx += greenEditgx[sum];
                blueSumgx +=  blueEditgx[sum];

                redSumgy += redEditgy[sum];
                greenSumgy += greenEditgy[sum];
                blueSumgy += blueEditgy[sum];
            }

            int finalRed = round(sqrt(pow(redSumgx, 2) + pow(redSumgy, 2)));
            int finalGreen = round(sqrt(pow(greenSumgx, 2) + pow(greenSumgy, 2)));
            int finalBlue = round(sqrt(pow(blueSumgx, 2) + pow(blueSumgy, 2)));

            if (finalRed &gt; 255)
            {
                finalRed = 255;
            }
            if (finalGreen &gt; 255)
            {
                finalGreen = 255;
            }
            if (finalBlue &gt; 255)
            {
                finalBlue = 255;
            }

            image[q][w].rgbtRed = finalRed;
            image[q][w].rgbtGreen = finalGreen;
            image[q][w].rgbtBlue = finalBlue;
        }

    }

    return;
}

I know this code is cancer please don't judge

答案1

得分: 1

这些数组

int redEdit[9];
int greenEdit[9];
int blueEdit[9];

尚未初始化,包含任意值。因此,当您执行诸如

redEdit[top] += edit[q - 1][w - 1 + top].rgbtRed;

这样的语句时,求和是没有意义的。您需要

int redEdit[9] = { 0 };
int greenEdit[9] = { 0 };
int blueEdit[9] = { 0 };

只有静态变量会被隐式初始化为0。局部(自动)变量不会被初始化。

英文:

These arrays

int redEdit[9];
int greenEdit[9];
int blueEdit[9];

have not been initialised and hold arbitrary values. So when you execute statements such as

redEdit[top] += edit[q - 1][w - 1 + top].rgbtRed;

the sum is meaningless. You need

int redEdit[9] = { 0 };
int greenEdit[9] = { 0 };
int blueEdit[9] = { 0 };

Only static variables are implicitly initialised to 0. Local (auto) variables are not.

huangapple
  • 本文由 发表于 2023年2月10日 02:58:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75403234.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定