2

バウンディングボックスの中心点を表すドットをバウンディングボックスに描画しようとしています。中心点を計算しましたが、CMDでのみ出力され、この点が画像に表示されません。

Visual Studio 2010C++でOpenCV2.4.3を使用しています

 for(int i= 0; i < boundRect.size(); i++ )
       {
            //BoundingBox Area
            boundingBoxArea.clear();
            boundingBoxArea.push_back(Point2f(boundRect[i].x, boundRect[i].y));
            boundingBoxArea.push_back(Point2f(boundRect[i].x + boundRect[i].width, boundRect[i].y));
            boundingBoxArea.push_back(Point2f(boundRect[i].x + boundRect[i].width, boundRect[i].y + boundRect[i].height));
            boundingBoxArea.push_back(Point2f(boundRect[i].x, boundRect[i].y + boundRect[i].height));

            double area0 = contourArea(boundingBoxArea);

            cout << "area of bounding box no." << i << " = " << area0 << endl;

            //Bounding Box Centroid
            area0 = (boundRect[i].x + boundRect[i].width)/2, (boundRect[i].y + boundRect[i].height)/2;

            cout<<"Rectangle " <<i<< " Centroid possition is at: " "=" <<area0<<endl;
            cout<<""<<endl;
            cout<<""<<endl;
     }

上記は、私がうまく使用しているコードですが、バウンディングボックスの計算を担当する部分はごくわずかです。

4

3 に答える 3

3

ああ、あなたはすでに面積を計算しました、そして今あなたはそれへの中心(ポイント)を評価しようとしていますか?大野。最後の行を次のように置き換えます。

//Bounding Box Centroid
Point center = Point((boundRect[i].x + boundRect[i].width)/2, (boundRect[i].y + boundRect[i].height)/2);

// print it:
cout<<"Rectangle " <<i<< " Centroid position is at: " << center.x << " " << center.y << endl;

また、boundingBoxAreaが間違っています。代わりに(面積を計算するために)元のboundingRect [i]を使用してください!

于 2013-02-19T22:48:51.803 に答える
1

Momentsを使用すると、コードは次のようになります(java、テストされていません)。

..
MatOfPoint contour = new MatOfPoint();
Rect box = boundRect[i];
//Points order doesn't matter
contour.fromArray(new Point[]{new Point(box.x, box.y), //top-left
                  new Point(box.x + box.width, box.y), // top-right
                  new Point(box.x,  box.y + box.height)}); //bottom-left
                  new Point(box.x + box.width, box.y + box.height), //bottom right
int Cx = (int)Math.round(M.get_m10()/M.get_m00());
int Cy = (int)Math.round(M.get_m01()/M.get_m00());
..
double area0 = Imgproc.contourArea(contour);
..

バックグラウンド

画像モーメントは、オブジェクトの重心、オブジェクトの面積などのいくつかの特徴を計算するのに役立ちます。画像モーメントのウィキペディアページを確認してください。

import cv2
import numpy as np

img = cv2.imread('star.jpg',0)
ret,thresh = cv2.threshold(img,127,255,0)
contours,hierarchy = cv2.findContours(thresh, 1, 2)

cnt = contours[0]
M = cv2.moments(cnt)
print M

重心は、Cx = M10/M00およびCy=M01/M00の関係で与えられます。

Cx = int(M['m10']/M['m00'])
Cy = int(M['m01']/M['m00'])

こちらのOpenCVチュートリアルを参照してください。

于 2015-09-24T13:06:51.063 に答える
0

OKみんな私が自分で自分の問題を解決することができたのは私に誇りを感じさせますhehe:D

xとwidthとy&の両方を分割していたので、方程式自体が間違っていたことを発表しました。これは、xとyによって与えられたオフセットが間違っていたためです。そこで、幅/2と高さ/2だけを分割するようにコードを変更しました

ソリューションの最終的な要素は、cv :: circle();を使用することでした。中心点を描くために使用した関数。

これがいつか誰かを助けるかもしれないことを願っています:D

@berakへのthx

最終結果:

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

于 2013-02-20T00:20:55.840 に答える