2017-05-31 3 views
0

だから、私は問題を抱えていますイメージを処理し、元のイメージに境界線を作成します。そのためには、元の画像に完全にフィットするように、その枠を拡大する必要があります。私が持っているコードは元の画像の処理と描画のバウンディングボックスでうまくいっていますが、スケーリングを行う方法は?C++ OpenCVの再スケールバウンディングのRect

Mat &image = *(Mat *) matAddrRgba; 
//over here I should resize image and do the processing with the resized one, and in the end, scale everything back so I can draw the bounding box to the original 
    Rect bounding_rect; 

    Mat thr(image.rows, image.cols, CV_8UC1); 
    cvtColor(image, thr, CV_BGR2GRAY); //Convert to gray 
    threshold(thr, thr, 150, 255, THRESH_BINARY + THRESH_OTSU); //Threshold the gray 

    vector<vector<Point> > contours; // Vector for storing contour 
    vector<Vec4i> hierarchy; 
    RotatedRect rect; 
    findContours(thr, contours, hierarchy, CV_RETR_CCOMP, 
       CV_CHAIN_APPROX_SIMPLE); // Find the contours in the image 
    sort(contours.begin(), contours.end(), 
     compareContourAreas);   //Store the index of largest contour 
    bounding_rect = boundingRect(contours[0]); 
    rect = minAreaRect(contours[0]); 
    // matrices we'll use 
    Mat rot_mat, rotated; 
    // get angle and size from the bounding box 
    float angle = rect.angle; 
    Size rect_size = rect.size; 

    if (rect.angle < -45.) { 
     angle += 90.0; 
     swap(rect_size.width, rect_size.height); 
    } 

    rot_mat = getRotationMatrix2D(rect.center, angle, 1); 

    warpAffine(image, rotated, rot_mat, image.size(), INTER_CUBIC); 

    image = rotated; 

    cvtColor(image, thr, CV_BGR2GRAY); //Convert to gray 
    threshold(thr, thr, 150, 255, THRESH_BINARY + THRESH_OTSU); //Threshold the gray 

    findContours(thr, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE); 
    sort(contours.begin(), contours.end(), compareContourAreas); 
    bounding_rect = boundingRect(contours[0]); 

    image = Mat(image, bounding_rect); 
+0

「スケーリングを行う方法」 - オブジェクトからのスケールの登りや除去を指していない限り、*乗算*はトリックを行う傾向があります。新しい幅と高さに 'scale-1'のオフセットを追加すると、少し上手くフィットするようになります。正方形の紙を1枚取ってそれを描きなさい - これは把握するために多くの仕事を取るべきではありません。 –

+0

@DanMašek私は私の計算の積である回転したrectを得る。次に、イメージを回転させて、デバイスと完全に一致させます。その後、私は回転rect(それから境界rectを作成する)で作物を行います。その境界矩形を拡大/拡大/拡大する方法は? –

+0

たとえば、イメージの幅と高さを2倍にすると、同じスケーリング係数が座標に適用されます。したがって、境界ボックスのx座標とy座標は2倍になり、幅と高さは2倍になります。 –

答えて

2

[OK]を、ので、私はこれで始まります:ここでは、コードスニペットはある

Mat &img = (Mat) matAddrRgba; 
    Mat image; 
    double thrA = 5; 
    resize(img, image, Size((int) (img.size().width/thrA), (int) (img.size().height/thrA))); 

    Rect bounding_rect; 

    Mat thr(image.rows, image.cols, CV_8UC1); 
    cvtColor(image, thr, CV_BGR2GRAY); //Convert to gray 
    threshold(thr, thr, 150, 255, THRESH_BINARY + THRESH_OTSU); //Threshold the gray 

    vector<vector<Point> > contours; // Vector for storing contour 
    vector<Vec4i> hierarchy; 
    RotatedRect rect; 
    findContours(thr, contours, hierarchy, CV_RETR_CCOMP, 
       CV_CHAIN_APPROX_SIMPLE); // Find the contours in the image 
    sort(contours.begin(), contours.end(), 
     compareContourAreas);   //Store the index of largest contour 
    bounding_rect = boundingRect(contours[0]); 

    bounding_rect.width = (int) (bounding_rect.width * thrA); 
    bounding_rect.height = (int) (bounding_rect.height * thrA); 
    bounding_rect.x = (int) (bounding_rect.x * thrA); 
    bounding_rect.y = (int) (bounding_rect.y * thrA); 

    rectangle(img, bounding_rect, Scalar(0, 172, 236, 255), 3); 

あなたが見ることができるように、あなたは規模を持っている必要があり、その規模は幅、高さで乗算する必要があります、境界矩形のxとy。あなたはこれから残りを把握することができます。

関連する問題