2014-08-29 94 views
0

我正在使用OpenCV for Windows Phone 8.1(Windows运行时)在C++与MS开放技术https://github.com/MSOpenTech/opencv发布。MedianBlur问题与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开放技术的代码?

感谢您的帮助!

回答

0

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

根据文档,你应该建立这样的:

Mat img(height, width, CV_8UC3); 

然而,当您使用CV ::大小,你先放弃宽度。

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

这有点令人困惑,但肯定有一个原因。

+0

哈哈,非常感谢你!它的作用就像一个魅力:D 感谢MS你的样品与这个litle错误;) – Nico 2014-08-29 12:27:35

+0

@biquette的原因可能是,在数学中存在两个约定:矩阵公约索引:行第一。图像约定:首先x轴。这可能与为什么存在两种方式来访问矩阵元素(用矩阵约定中的“.at”之一(行第一)和一个用于图像约定“Point”)是相同的原因。 – Micka 2014-08-29 14:06:50

+0

@Micka,谢谢你的解释。 – biquette 2014-08-29 14:35:09

0

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

// 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(); 
+0

嗨,谢谢你的回复,但它不工作......顶部有奇怪的影响... – Nico 2014-08-29 12:20:46

+0

现在尝试上面的代码。一些改变完成了。 – balajichinna 2014-08-29 12:30:24