来自Mat图像的OpenCV子图像 [英] OpenCV sub-image from a Mat image
问题描述
可能重复:
了解openCV 2.4的感兴趣区域
我想从图像(Mat格式)获取子图像(由下面红色框所包围的子图像)。我如何做到这一点?
i want to get a sub-image (the one bounded by the red box below) from an image (Mat format). how do i do this?
这里是我迄今为止的进展:
here's my progress so far:
include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat imgray, thresh;
vector<vector<Point> >contours;
vector<Point> cnt;
vector<Vec4i> hierarchy;
Point leftmost;
Mat im = imread("igoy1.jpg");
cvtColor(im, imgray, COLOR_BGR2GRAY);
threshold(imgray, thresh, 127, 255, 0);
findContours(thresh, contours, hierarchy, RETR_TREE,CHAIN_APPROX_SIMPLE);
}
推荐答案
(在你的情况下,轮廓对应于手)。
然后,计算此轮廓的边界矩形。
最后,从中创建一个新的矩阵头。
You can start picking a contour (in your case, the contour corresponding to the hand). Then, you calculate the bounding rectangle for this contour. Finally you make a new matrix header from it.
int n=0;// Here you will need to define n differently (for instance pick the largest contour instead of the first one)
cv::Rect rect(contours[n]);
cv::Mat miniMat;
miniMat = imgray(rect);
警告:在这种情况下,miniMat是imgray的子区域。这意味着如果你修改前者,你也修改后者。使用 miniMat.copyTo(anotherMat)
可以避免这种情况。
Warning: In this case, miniMat is a subregion of imgray. This means that if you modify the former, you also modify the latter. Use miniMat.copyTo(anotherMat)
to avoid this.
我希望它有帮助,
祝你好运
I hope it helped, Good luck
这篇关于来自Mat图像的OpenCV子图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!