c ++ OpenCV如何将过滤器应用到Mat的子矩形? [英] c++ OpenCV How to apply filter to sub rect of Mat?

查看:337
本文介绍了c ++ OpenCV如何将过滤器应用到Mat的子矩形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题类似,但从未回答:图像的OpenCV过滤部分



我使用opencv2和c ++。我有一个Mat,说300x200,我想模糊 矩形中的区域,左上角= 50,50尺寸= 100,50。我一直在通过在opencv.org的示例和文档,但我不能确定如何过滤,或执行其他操作只有一个从一个子矩阵。



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

  {//用于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->像素,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。 Mat,& 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,第323行
在抛出'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.height <= m.rows in function Mat


解决方案<

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

//应用任何算法对'roi'
blur(roi,roi,cv :: Size(5,5));


This question is similar, but never was answered: OpenCV filtering part of an image

I'm using opencv2 and c++. I have a Mat, say 300x200, and I want to blur only the area in the rectangle with top-left = 50,50 size=100,50. I've been wading through the example and docs on opencv.org, but I cannot determine how to filter, or perform other operations on only a sub-rect from a Mat.

Code in question is below, where surf is an SDL_Surface and rect is an SDL_Rect (int x,y,w,h). The line with the creation of Mat src_mat from the surface is fine as it works well elsewhere. This compiles, but gives the following error.

{ // 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));
}

error:

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

解决方案

a subrect is a Mat, too.

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

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

这篇关于c ++ OpenCV如何将过滤器应用到Mat的子矩形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆