OpenCV Sobel过滤器导致几乎完全黑色的图像



我的sobel_y(和sobel_x,但我认为他们也有同样的问题)过滤器存在一些问题,因为它一直给我一个基本上只有黑白的图像。我必须为一个类重写这个函数,所以不,我不能使用内置的函数,并且让它工作,减去一些小的调整,因为输出图像看起来有点奇怪,仍然是黑白的,尽管它应该被转换回来。我想好了如何解决这个问题,在这个过程中,我搞砸了一些东西,把它弄坏了,即使只输出黑白图像,似乎也无法让它恢复工作。我一直看到一张黑色的照片,上面到处都是白线。我曾尝试将Mat灰度类型(第三个参数)更改为所有不同的值,正如我的教授在课堂上提到的那样,我们使用的是32位浮点图像,但这也没有帮助。

尽管这个问题发生在运行Studentfilter2D之后,但我认为这是图像灰度的问题,尽管每当我调试时,它似乎都能正常工作。这也是因为我必须编写另外两个使用Studentfilter2D的过滤函数,它们都给了我预期的结果。我的sobel_y函数如下所示:

// Convert the image in bgr to grayscale OK to use the OpenCV function.  
// Find the coefficients used by the OpenCV function, and give a link where you found it.
// Note: This student function expects the matrix gray to be preallocated with the same width and
// height, but with 1 channel.
void BGR2Gray(Mat& bgr, Mat& gray)
{
    // Y = .299 * R + .587 * G + .114 * B, from http://docs.opencv.org/modules/imgproc/doc/miscellaneous_transformations.html#cvtcolor
    // Some extra assistance, for the third parameter for the InputArray, from http://docs.opencv.org/trunk/modules/core/doc/basic_structures.html#inputarray
    // Not sure about the fourth parameter, but was just trying it to see if that may be the issue as well
    cvtColor(bgr, gray, CV_BGR2GRAY, 1);
    return;
}
// Convolve image with kernel - this routine will be called from the other
// subroutines!  (gaussian, sobel_x and sobel_y)
// image is single channel.  Do not use the OpenCV filter2D!!
// Implementation can be with the .at or similar to the
// basic method found in the Chapter 2 of the OpenCV tutorial in CANVAS,  
// or online at the OpenCV documentation here: 
// http://docs.opencv.org/doc/tutorials/core/mat-mask-operations/mat-mask operations.html 
// In our code the image and the kernel are both floats (so the sample code   will need to change)
void Studentfilter2D (Mat& image, Mat& kernel)
{
    int kCenterX = kernel.cols / 2;
    int kCenterY = kernel.rows / 2;
    // Algorithm help from http://www.songho.ca/dsp/convolution/convolution.html
    for (int iRows = 0; iRows < image.rows; iRows++)
    {
        for (int iCols = 0; iCols < image.cols; iCols++)
        {
            float result = 0.0;
            for (int kRows = 0; kRows < kernel.rows; kRows++)
            {
                // Flip the rows for the convolution
                int kRowsFlipped = kernel.rows - 1 - kRows;
                for (int kCols = 0; kCols < kernel.cols; kCols++)
                {
                    // Flip the columns for the convolution
                    int kColsFlipped = kernel.cols - 1 - kCols;
                    // Indices of shifting around the convolution
                    int iRowsIndex = iRows + kRows - kCenterY;
                    int iColsIndex = iCols + kCols - kCenterX;
                    // Check bounds using the indices
                    if (iRowsIndex >= 0 && iRowsIndex < image.rows && iColsIndex >= 0 && iColsIndex < image.cols)
                    {
                        result += image.at<float>(iRowsIndex, iColsIndex) * kernel.at<float>(kRowsFlipped, kColsFlipped);
                    }
                }
            }
            image.at<float>(iRows, iCols) = result;
        }
    }
    return;
}
void sobel_y (Mat& image, int)
{
    // Note, the filter parameter int is unused.
    Mat mask = (Mat_<float>(3, 3) << 1, 2, 1,
        0, 0, 0,
        -1, -2, -1) / 3;
    //Mat grayscale(image.rows, image.cols, CV_32FC1);
    BGR2Gray(image, image);
    Studentfilter2D(image, mask);
    // Here is the documentation on normalize http://docs.opencv.org/modules/core/doc/operations_on_arrays.html#normalize
    normalize(image, image, 0, 1, NORM_MINMAX);
    cvtColor(image, image, CV_GRAY2BGR);
    return;
}

就像我说的,我以前也做过这个工作,只是想找一些新鲜的眼光来看待它,看看我可能错过了什么。在过去的4天里,我一直在看同样的代码,我想我只是错过了一些东西。如果有人想知道,我也尝试过更改过滤器的掩码值,但没有成功。

有两件事值得一提。

第一个原因是你没有正确处理矩阵/图像的类型。sobel_y中的Studentfilter2D的输入是类型为CV_8UC1的8位灰度图像,这意味着数据是unsigned char的阵列。然而,您的Studentfilter2D函数正在对该输入图像进行索引,就好像它是float类型一样。这意味着选择了错误的像素来处理

如果以上内容不能立即解决您的问题,您应该考虑最终衍生图像的范围。由于它是一个导数,它将不再在[0255]的范围内。相反,它甚至可能包含负数。当您尝试将其可视化时,除非您首先规范化您的图像,否则会遇到问题。如果你在文档中四处查看,OpenCV中有一些内置函数可以做到这一点。

最新更新