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
OpenCV C++, getting Region Of Interest (ROI) using cv::Mat
提问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::Mat
type. My questions are:
我对 OpenCV 很陌生(两天前开始使用它),我正在尝试从 Kinect 获得的深度图像中剪切手部图像,我需要手部图像进行手势识别。我将图像作为一种cv::Mat
类型。我的问题是:
- Is there a way to convert
cv::Mat
tocvMat
so that I can usecvGetSubRect
method to get the Region of interest? - Are there any methods in
cv::Mat
that I can use for getting the part of the image?
- 有没有办法转换
cv::Mat
为,cvMat
以便我可以使用cvGetSubRect
方法来获取感兴趣的区域? - 有没有什么方法
cv::Mat
可以用来获取图像的一部分?
I wanted to use IplImage
but I read somewhere that cv::Mat
is 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.
我希望这会有所帮助。