gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 22:14:31 24 4
gpt4 key购买 nike

这个问题很相似,但从未得到回答: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) );

关于c++ OpenCV如何将过滤器应用于Mat的子矩形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19667555/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com