2016-05-12 10 views
3

C++を使用してそれぞれの切り抜きを別々の画像に抽出するにはどうすればよいですか? (マイOpenCVのバージョン2.4.10)OPENCVを使用して、画像の切り取った部分を別々の画像に抽出します

enter image description here

Iは、ライセンスプレートの所望の大きさの幅/高さの比と一致するように輪郭を濾過してきました。 (3番目の画像 - 矩形3)

今私は "i"候補と同じサイズの別々の画像に "i"個の候補を抽出する必要があるので、文字を分割してOCRアルゴリズムを使用することができます。

この画像から所望の出力は次のようになります

2つの画像(理想的には、画像に示されるように、いくつかの余分な添加幅/高さで抽出された)をそれぞれ含むクロップバージョン 見つかった境界ボックスの 。

enter image description here

私は別々の画像を必要とするか、私は単にセグメント文字に順にだけトリミングされた部品を含む画像全体(と表示される画像のように黒い背景)で動作することができればそれは、私には疑問です。

私はここに私のコードの一部を提供しています:答えを提供するための三木へ

findContours(crop, contours, hierarchy, CV_RETR_TREE, 
      CV_CHAIN_APPROX_SIMPLE, Point(0, 0)); 

    vector<Point2f> ContArea(contours.size()); 

    for (int i = 0; i < contours.size(); i++) { 
     approxPolyDP(Mat(contours[i]), contours_poly[i], 3, true); 
     boundRect[i] = boundingRect(Mat(contours_poly[i])); 

    } 

    // Draw polygonal contour + filled bonding rects 

    Mat drawing4 = Mat::zeros(src_gray.size(), CV_8UC3); 

    for (int i = 0; i < contours.size(); i++) { 
     Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), 
       rng.uniform(0, 255)); 

     rectangle(drawing4, boundRect[i].tl(), boundRect[i].br(), color, 
       CV_FILLED, 1, 0); 

    } 

    imshow("Rectangles4", drawing4); 

    float ratio; 
    Mat drawing3 = Mat::zeros(crop.size(), CV_8UC3); 

    // Draw bonding rects 

     for (int i = 0; i < contours.size(); i++) { 
     Scalar color = Scalar(0, 255, 0); 

     double a = contourArea(contours[i]); 

     ratio = (float) boundRect[i].width/(float) boundRect[i].height; 

     //check for min, max size of area and its ratios 

     if ((a > 200 && a < 2600) && ((ratio >= 1.3) && (ratio <= 10))) { 
      printf("a: %f ratios: %f", a, ratio); 

     //drawContours(drawing3, contours_poly, (int) i, color, 1, 8, 
        vector<Vec4i>(), 0, Point()); 
     rectangle(drawing3, boundRect[i].tl(), boundRect[i].br(), color, 
         CV_FILLED, 1, 0); 
     } 
    } 

    imshow("Rectangles3", drawing3); 
+0

: 'マットcropped_image_i(original_image(BBOX))'、またはディープコピーについて: 'マットcropped_image_i = original_image(BBOX).clone( ) ' – Miki

+0

これは素敵に動作するようです!どうもありがとう! 唯一の問題は、次のトリミングが最後のトリミングと重なっているように見えることです。 私はこれをこのように見せているのですか? imshow( "crops"、crops [j]); imshowで画像が表示されるだけでオーバーラップします 回答を追加します。 :) – Whatever

答えて

1

感謝を!

トリミングコード

float ratio; 
    int j=0; 
    Mat crops[10]; 
    Mat drawing3 = Mat::zeros(crop.size(), CV_8UC3); 

    for (int i = 0; i < contours.size(); i++) 
    { 
     Scalar color = Scalar(0,255,0); 

     double a = contourArea(contours[i]); 
      ratio = (float)boundRect[i].width/(float)boundRect[i].height; 
        if ((a > 200 && a < 2600)&&((ratio>=1.3)&&(ratio<=10))){ 
      printf(" a: %f ratios: %f image%d",a,ratio,i); 

      drawContours(drawing3, contours_poly, (int)i, color, 1, 8, vector<Vec4i>(), 0, Point()); 
      rectangle(drawing3, boundRect[i].tl(), boundRect[i].br(), color, 
          CV_FILLED, 1, 0); 

      Mat cropx(src_resized(boundRect[i])); 
      crops[j]=cropx; 

      imshow("crops",crops[0]); 
      if (j==1) imshow("crops1",crops[1]); 
      j++; 
      waitKey(0); 

     } 
    } 
     imshow("Rectangles3", drawing3); 

Basicalyコード

//outside the loop 
    int j=0; 
      Mat crops[10]; 


//inside the loop  
    Mat cropx(src_resized(boundRect[i])); 
        crops[j]=cropx; 
     j++; 

結果(まだ大きい面積にリサイズしない)ここに示されているのはわずか数行以上の編集された部分に示されています。 Crops

拡大するには、単に(またはこれに類する属性を追加する必要があります)

boundRect[i].x=boundRect[i].x -boundRect[i].width/4; 
boundRect[i].y=boundRect[i].y -boundRect[i].height/4; 
boundRect[i].width=boundRect[i].width +boundRect[i].width/2; 
boundRect[i].height=boundRect[i].height +boundRect[i].height/2; 

拡大の結果をここで見ることができます。各バウンディングボックスに対して enter image description here

関連する問題