c++ - Prewitt 滤波器、边缘检测

标签 c++ image edge-detection

我有这段实现 Prewitt 边缘检测的代码。我需要做的是只用一个缓冲区来实现它,也就是说,我不会创建图像的拷贝,而是编辑原始图像。所以如果我想改变值 78 的像素,我不能把新值,例如100,直到所有周围像素的读取值为 78。Color values of the pixels .我一整天都在尝试弄清楚但做不到,如果有人能给我写一些伪代码,我将不胜感激

void filter_serial_prewitt(int *inBuffer, int *outBuffer, int width, int height){
   for (int i = 1; i < width - 1; i ++) {
     for (int j = 1; j < height - 1; j ++) {
        int Fx = 0;
        int Fy = 0;
        int F = 0;
        for (int m = -1; m <= 1; m++) {
            for (int n = -1; n <= 1; n++) {
                Fx += inBuffer[(j + n) * width + (i + m)] * n;
                Fy += inBuffer[(j + n) * width + (i + m)] * m;
            }
        }
            F = abs(Fx) + abs(Fy);

            if (F < THRESHOLD){
                outBuffer[j * width + i] = 255;
            } else{
                outBuffer[j * width + i] = 0;
            }
       }
   }
}

最佳答案

关于 Prewitt 运算符的一件事是它是可分离的。 See the Wikipedia article了解详情。

要计算单个输出行,您需要执行以下操作(伪代码):

int* buffer = malloc (sizeof(int) * width);
for (int i = 0; i < width; i++)
{
    // Do the vertical pass of the convolution of the first 3 rows into
    // the buffer.
    buffer [ i ] = vertical_convolve(inBuffer [ i ], vertical_kernel);
}

// Next, do the horizontal convolution of the first row. We need to 
// keep the previous value in a temp buffer while we work
int temp0 = horizontal_convolve(buffer [ 0 ], horizontal_kernel);
for (int i = 1; i < width; i++)
{
    int temp1 = horizontal_convolve(buffer[ i ], horizontal_kernel);
    inBuffer [ i - 1 ] = temp0;
    temp0 = temp1;
}

这需要一个 1 像素高和图像宽度的缓冲区。

要处理整个图像,您需要保留上述缓冲区中的 2 个,并且在计算第三行的像素后,您可以将图像第一行的第一个像素替换为图像的第一行的第一个像素第一个缓冲区。然后您可以将新计算的值放入缓冲区。

因此在这种情况下,您不会保留整个第二张图像,而是需要保留大约 2 个 1 像素高且与图像一样宽的缓冲区。

关于c++ - Prewitt 滤波器、边缘检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41308906/

相关文章:

c++ - MFC:嵌入式子对话框未显示在父对话框中

c++ - 通过继承使用初始化列表

javascript - 图像映射禁用图像上的 <A> 标签

c# - iOS 使图像适合屏幕

c++ - HBITMAP 可以包含 alpha channel 信息吗?

c++ - 为什么 c++11 上的 std::list 更大?

javascript - 如何复制 Google Fastflip 的 UI 功能?

python - 基于边缘像素图的图像分割

java - OpenCV - 在视频和图像上查找黑板边缘

python - 使用Opencv检测梯形和菱形时的公差问题