2016-08-09 40 views
0

私のプログラムはオブジェクトトラッキング用です。 私はオブジェクトを追跡し、オブジェクトx、yにモーメント法による座標を与えることができます。OpenCv2でピクセル座標をワールド座標に変換する

enter image description here

enter image description here

私は、ピクセルが世界OpenCV2の座標に座標変換します。私はすでにカメラの校正によって回転行列(3 * 3)と平行移動ベクトル(3 * 1)を得ています。私はカメラの焦点距離を知っています。

ここでは、次のように定義しました。

CvMat *rotation = (CvMat*)cvLord("Rotation.xml") 
CvMat *translation = (CvMat*)cvLord("Translation.xml") 

これは私のプログラムの一部です。

void trackFilteredObject(Mat threshold,Mat HSV, Mat &Birds_image){ 

    vector <Fruit> apples; 

    Mat temp; 
    threshold.copyTo(temp); 

    // these two vectors needed for output of findContours 
    vector< vector<Point> > contours; 
    vector<Vec4i> hierarchy; 

    // find contours of filtered image using OpenCv findCountours function 
    findContours(temp,contours,hierarchy,CV_RETR_CCOMP,CV_CHAIN_APPROX_SIMPLE); 

    // use moments method to find our filtered object. 
    double refArea = 0; 
    bool objectFound = false; 
    if (hierarchy.size() > 0) { 
    int numObjects = hierarchy.size(); 

    // if number of objects greater than MAX_NUM_OBJECTS, we have a noisy filter. 
    if(numObjects<MAX_NUM_OBJECTS){ 
     for (int index = 0; index >= 0; index = hierarchy[index][0]) { 
      Moments moment = moments((cv::Mat)contours[index]); 
      double area = moment.m00; 

      if(area>MIN_OBJECT_AREA){ 
       Fruit apple; 

       // moments method 
       apple.setXPos(moment.m10/area); 
       apple.setYPos(moment.m01/area); 
       apples.push_back(apple); 

       objectFound = true; 

      }else objectFound = false; 
     } 
     if(objectFound ==true){ 
      // draw object location on screen 
      drawObject(apples,Birds_image); 
     } 
    }else putText(Birds_image,"TOO MUCH NOISE! ADJUST FILTER",Point(0,50),1,2,Scalar(0,0,255),2); 
    } 
} 

drawObject(リンゴ、Birds_image)はこれです。

void drawObject(vector<Fruit> theFruits,Mat &frame){ 
    for(int i =0; i<theFruits.size(); i++){ 
    cv::circle(frame,cv::Point(theFruits.at(i).getXPos(),theFruits.at(i).getYPos()),10,cv::Scalar(0,0,255)); 
    cv::putText(frame,intToString(theFruits.at(i).getXPos())+ " , " + intToString(theFruits.at(i).getYPos()),cv::Point(theFruits.at(i).getXPos(),theFruits.at(i).getYPos()+20),1,1,Scalar(0,255,0)); 
    } 
} 

これらのsouseファイルとヘッダーファイルを使用します。

Fruit.h

#pragma once 
#include <string> 
using namespace std; 

class Fruit 
{ 
public: 
Fruit(void); 
~Fruit(void); 

int getXPos(); 
void setXPos(int x); 


int getYPos(); 
void setYPos(int y); 

private: 

int xPos, yPos; 
string type; 

}; 

Fruit.cpp

#include "Fruit.h" 


Fruit::Fruit(void) 
{ 
} 


Fruit::~Fruit(void) 
{ 
} 

int Fruit::getXPos(){ 
return Fruit::xPos; 
} 

void Fruit::setXPos(int x){ 
Fruit::xPos = x; 
xPos = x; 
} 

int Fruit::getYPos(){ 
return Fruit::yPos; 
} 

void Fruit::setYPos(int y){ 
Fruit::yPos = y; 
yPos = y; 
} 

あなたは私にあなたの素晴らしいアイデアを与えることができます。

+0

カメラキャリブレーションプロセスによって推定された回転行列と並進ベクトルは、対応する画像のチェスボードフレームとカメラフレーム間の変換を表します。単一の画像と任意のオブジェクトを使用すると、3D座標を復元することはできません。実際のオブジェクトサイズを知っていれば、大まかに3D座標を見積もることができます。ありがとう。 – Catree

答えて

0

opencvのfindHomography関数を見てください。これは、ある平面から他の平面への変換を見つけるのに役立ちますが、2D座標のみが見つかります。
このリンクは、座標をイメージ平面からオブジェクト平面に変換する同様の例を提供します。 (Camera pixels to planar world points given 4 known points

+0

私はこの問題を解決しました。 –

関連する問題