繁体   English   中英

在OpenCv2中将像素坐标转换为世界坐标

[英]Convert pixel coordinate into world coordinate in OpenCv2

我的程序用于对象跟踪。 我可以跟踪对象,并通过moments方法为对象x,y提供坐标。

在此处输入图片说明

在此处输入图片说明

我想在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(apples,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文件和头文件。

水果

#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;

};

水果

#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;
}

你能给我你的绝妙主意吗?

看看opencv中的findHomography函数。 它有助于找到从一个平面到另一平面的转换,但是只能找到2D坐标。
此链接提供了将坐标从图像平面转换为对象平面的类似示例。 给定4个已知点,相机像素到平面世界点

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM