这个问题很相似,但从未得到回答:OpenCV filtering part of an image
我正在使用 opencv2 和 c++。我有一个垫子,比如 300x200,我想仅模糊矩形中左上角 = 50,50 size=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
subrect 也是 Mat。
Mat larger;
Mat roi(larger, Rect(50,50,100,50));
// apply whatever algo on 'roi'
blur( roi,roi, cv::Size(5,5) );
我是一名优秀的程序员,十分优秀!