OpenCV C++,使用 cv::Mat 获取感兴趣区域 (ROI)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6566295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 20:21:54  来源:igfitidea点击:

OpenCV C++, getting Region Of Interest (ROI) using cv::Mat

c++opencvroi

提问by vprasad

I'm very new to OpenCV (started using it two days ago), I'm trying to cut a hand image from a depth image got from Kinect, I need the hand image for gesture recognition. I have the image as a cv::Mattype. My questions are:

我对 OpenCV 很陌生(两天前开始使用它),我正在尝试从 Kinect 获得的深度图像中剪切手部图像,我需要手部图像进行手势识别。我将图像作为一种cv::Mat类型。我的问题是:

  1. Is there a way to convert cv::Matto cvMatso that I can use cvGetSubRectmethod to get the Region of interest?
  2. Are there any methods in cv::Matthat I can use for getting the part of the image?
  1. 有没有办法转换cv::Mat为,cvMat以便我可以使用cvGetSubRect方法来获取感兴趣的区域?
  2. 有没有什么方法cv::Mat可以用来获取图像的一部分?

I wanted to use IplImagebut I read somewhere that cv::Matis the preferred way now.

我想使用,IplImage但我在某处阅读cv::Mat了现在首选的方式。

回答by Michael Koval

You can use the overloaded function calloperator on the cv::Mat:

您可以在 上使用重载的函数调用运算符cv::Mat

cv::Mat img = ...;
cv::Mat subImg = img(cv::Range(0, 100), cv::Range(0, 100));

Check the OpenCV documentationfor more information and for the overloaded function that takes a cv::Rect. Note that using this form of slicing creates a new matrix header, but does not copy the data.

查看OpenCV 文档以获取更多信息以及使用cv::Rect. 请注意,使用这种形式的切片会创建一个新的矩阵头,但不会复制数据。

回答by Angie Quijano

Maybe an other approach could be:

也许另一种方法可能是:

//Create the rectangle
cv::Rect roi(10, 20, 100, 50);
//Create the cv::Mat with the ROI you need, where "image" is the cv::Mat you want to extract the ROI from
cv::Mat image_roi = image(roi)

I hope this can help.

我希望这会有所帮助。