2013-10-29 27 views
1

这个问题是类似的,但从来没有被回答:OpenCV filtering part of an imageC++ OpenCV如何将过滤器应用到Mat的子节点?

我使用opencv2和c + +。我有一个垫,比如说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 

回答

3

一个subrect是一个垫子,太。

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

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

我刚刚得到了一个漂亮的段错误,所以我发布代码,因为我显然误解了你的建议。 – justinzane

+0

回答你的编辑:你的Rect看起来非常破碎:Rect(rect.y,rect.y + rect.h,rect.x,rect.x + rect.w); //在这里尝试简单的'rect' – berak

+0

cv :: Rect是(x,y,w,h),所以没有y + h,x + w,并且你得到y和x反向 – berak