2012-10-17 1 views
10

가능한 중복 :
Understanding region of interest in openCV 2.4OpenCV의 서브 이미지

내가 이미지에서 서브 이미지 (아래 빨간색 상자에 의해 둘러싸인 하나) 싶어 (매트 포맷). 어떻게해야합니까?

enter image description here

여기 내 진행 지금까지의 :

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); 
} 
+1

이 질문은 이미 질문하고, 답 ([여기] 적어도 http://stackoverflow.com/questions/12705817/understanding-region-of-interest- in-opencv-2-4/12706208 # 12706208) 및 [there] (http://stackoverflow.com/questions/12369697/access-sub-matrix-of-a-multidimensional-mat-in-opencv/12370641#12370641) – remi

답변

24

당신은 윤곽을 따기 시작할 수 있습니다 (귀하의 경우, 손에 해당 윤곽). 그런 다음이 윤곽선의 경계 사각형을 계산합니다. 마지막으로 새 행렬 헤더를 만듭니다.

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)을 사용하십시오. 내가 도움이 희망

, 행운

+1

고마워요! 나는 정확한 출력을 가지고 있지만 다른 윤곽을 포함하는 출력을 얻었습니다. 덜 윤곽이있을 수 있도록 RETR_TREE 대신 RETR_EXTERNAL을 사용했습니다. 올바른 윤곽선을 어떻게 식별합니까? –

+1

@OgNamdik 윤곽선을 순환하여 각각의 경계 사각형 (또는 다른 매개 변수)의 면적이나 면적을 계산할 수 있습니다. 귀하의 경우 가장 큰 면적을 가진 윤곽을 유지할 수있는 것 같습니다. 또한 만족한다면 주저하지 말고 답변을 수락하십시오. : D –

+0

대단히 감사합니다! –