13

重複の可能性:
openCV2.4の関心領域を理解する

画像(マット形式)からサブ画像(下の赤いボックスで囲まれたもの)を取得したい。どうすればこれを行うことができますか?

ここに画像の説明を入力してください

これまでの私の進捗状況は次のとおりです。

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);
}
4

1 に答える 1

29

輪郭(あなたの場合、手に対応する輪郭)の選択を開始できます。次に、この輪郭の外接する長方形を計算します。最後に、それから新しいマトリックスヘッダーを作成します。

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)これを回避するために使用します。

お役に立てば幸いです

于 2012-10-17T10:07:13.420 に答える