c ++ OpenCV如何将过滤器应用于Mat的子矩形?

时间:2013-10-29 19:35:05

标签: c++ opencv

这个问题很相似,但从未得到回答:OpenCV filtering part of an image

我正在使用opencv2和c ++。我有一个Mat,比如300x200,我想模糊矩形中的区域,左上角= 50,50大小= 100,50。我一直在浏览opencv.org上的示例和文档,但我无法确定如何过滤,或仅在Mat的子矩形上执行其他操作。

有问题的代码如下,其中surf是SDL_Surface,rect是SDL_Rect(int x,y,w,h)。从表面创建Mat src_mat的线很好,因为它在其他地方运行良好。这会编译,但会出现以下错误。

{ // Extra scoping used for the surface_lock.
    using namespace cv;
    surface_lock surf_lock(surf);

    //int rows, int cols, int type, void* data, size_t step=AUTO_STEP
    Mat src_mat = Mat(surf->h, surf->w, CV_8UC4, src->pixels, Mat::AUTO_STEP);
    Mat cropmat(src_mat, Rect(rect.y, rect.y + rect.h, rect.x, rect.x + rect.w));

    blur(crop_mat, crop_mat, Size((depth + 1), (depth + 1)), Point(-1,-1));
}

错误:

OpenCV Error: Assertion failed (0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows) in Mat, file /build/opencv/src/opencv-2.4.6.1/modules/core/src/matrix.cpp, line 323
terminate called after throwing an instance of 'cv::Exception'
  what():  /build/opencv/src/opencv-2.4.6.1/modules/core/src/matrix.cpp:323: error: (-215) 0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows in function Mat

1 个答案:

答案 0 :(得分:3)

subrect也是Mat。

Mat larger; 
Mat roi(larger, Rect(50,50,100,50));

// apply whatever algo on 'roi'
blur( roi,roi, cv::Size(5,5) );