【问题标题】:MedianBlur issues with OpenCVOpenCV 的 MedianBlur 问题
【发布时间】:2014-08-29 20:37:13
【问题描述】:

我在 C++ 中使用 OpenCV for Windows Phone 8.1(Windows 运行时),并由 MS Open Tech https://github.com/MSOpenTech/opencv 发布。

此版本基于 OpenCV 3,medianBlur 函数似乎有问题。 当我使用方形图像时,medianBlur 效果很好,但是当我使用矩形图像时,medianBlur 会产生奇怪的效果......

结果如下:http://fff.azurewebsites.net/opencv.png

我使用的代码:

// get the pixels from the WriteableBitmap
byte* pPixels = GetPointerToPixelData(m_bitmap->PixelBuffer);
int height = m_bitmap->PixelHeight;
int width = m_bitmap->PixelWidth;

// create a matrix the size and type of the image
cv::Mat mat(width, height, CV_8UC4);
memcpy(mat.data, pPixels, 4 * height*width);

cv::Mat timg(mat);
cv::medianBlur(mat, timg, 9);
cv::Mat gray0(timg.size(), CV_8U), gray;

// copy processed image back to the WriteableBitmap
memcpy(pPixels, timg.data, 4 * height*width);

// update the WriteableBitmap
m_bitmap->Invalidate();

我没有找到问题出在哪里...这是我的代码中的错误吗?还是 OpenCV 3 的错误?来自 MS Open Tech 的代码?

感谢您的帮助!

【问题讨论】:

    标签: opencv windows-phone-8 winrt-xaml


    【解决方案1】:

    在创建 cv::Mat 时,您正在反转高度和宽度。 Opencv Doc on Mat

    根据文档,您应该这样创建:

    Mat img(height, width, CV_8UC3);
    

    但是,当您使用 cv::Size 时,您首先给出宽度。

    Mat img(Size(width,height),CV_8UC3);
    

    这有点令人困惑,但肯定是有原因的。

    【讨论】:

    • 大声笑,非常感谢!它就像一个魅力 :D 感谢 MS 提供的带有那个小错误的样本 ;)
    • @biquette 原因可能是数学中存在两个约定:矩阵约定索引:行优先。图像约定:x 轴优先。这可能与为什么存在两种使用.at 访问矩阵元素的方法相同的原因,一种是矩阵约定(行优先),另一种是图像约定Point
    • @Micka,感谢您的解释。
    【解决方案2】:

    试试这个代码,修改一些代码。

      // get the pixels from the WriteableBitmap
        byte* pPixels = GetPointerToPixelData(m_bitmap->PixelBuffer);
        int height = m_bitmap->PixelHeight;
        int width = m_bitmap->PixelWidth;
    
        // create a matrix the size and type of the image
        cv::Mat mat(cv::Size(width, height), CV_8UC3);
        memcpy(mat.data, pPixels, sizeof(byte) * height*width);
    
        cv::Mat timg(mat.size(),CV_8UC3);
        cv::medianBlur(mat, timg, 9);
      //  cv::Mat gray0(timg.size(), CV_8U), gray;
    
        // copy processed image back to the WriteableBitmap
        memcpy(pPixels, timg.data,sizeof(byte) * height*width);
    
        // update the WriteableBitmap
        m_bitmap->Invalidate();
    

    【讨论】:

    • 您好,谢谢您的回复,但是不起作用...顶部有奇怪的效果...
    猜你喜欢
    • 1970-01-01
    • 2012-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-26
    • 2012-06-26
    • 2015-10-05
    相关资源
    最近更新 更多