2012-05-02 31 views
0

我想将x,y,z坐标转换为极坐标。我正在( - )在y coordiantes。有人能解释我为什么得到它。这将是很大的帮助。坐标转换,概念

我从软件读取这些值(xyz,az_elev_r),无法更改。我只是不确定角度的顺序(az & elevation)。使用我的代码,我得到了-y而不是y。这意味着有180 rotation.My代码:

xyz=[-0.564 3.689 -0.735; 
     2.011 5.067 -1.031; 
    -1.181 3.943 -1.825; % Reference values 
    ]; 

%% az_elev_r - > XYZ

az_elev_r=[ 261.30 -11.24 3.80; 
       291.65 -10.692 5.548; 
       253.34 -23.897 4.50]; % Also Reference (degree) 



az_elev_r(:,1:2)=deg2rad(az_elev_r(:,1:2)); 

r=az_elev_r(:,3); 
az=az_elev_r(:,1); 
elev=az_elev_r(:,2); 


x=r.*cos(az).*cos(elev) 
y=r.*sin(az).*cos(elev) 
z=r.*sin(elev) 

回答

0

az_elev_r矩阵与您的xyz参考一致。

>> [az, el, r] = cart2sph(xyz(:,1), xyz(:,2), xyz(:,3)); 
>> rad2deg(az) 
ans = 
      98.6924675475501 
      68.3527736950233 
      106.673911589314 

您的答案与sph2cart函数返回的值一致。 (例如使用原来的输入开始,在dec2rad更换前。

>> [x, y, z] = sph2cart(deg2rad(az_elev_r(:,1)), deg2rad(az_elev_r(:,2)), az_elev_r(:,3)) 
x = 
     -0.563766229670505 
      2.01131973806906 
     -1.17951822049783 
y = 
     -3.68422880893852 
     -5.06709019311118 
     -3.94153436658676 
z = 
     -0.740692730942158 
     -1.02931719412937 
     -1.82292172199717 

顺便说一句,你的代码将更具可读性,如果你只是使用sph2cart功能和工作弧度,除非你试图了解转换为它们自己。

+0

我读从软件这些值(XYZ,az_elev_r),并不能更改。 – Shahgee

0

OpenCV有转换为极坐标和后面的代码,这种转换对于通过关联或者以其他方式创建对象的以对象为中心的“旋转无关”表示来说很有用。以显示每个极坐标以及它们的联合图像。下面的图像应该是self_explanatory。极坐标绘图以角度为横轴,半径为纵轴,以使4个峰值对应于输入图像的4个角。代码(带有OpenCV的C++)已附带。 Polar coordinate visualization

//================================ 
// Name  : PolarCoord.cpp 
// Author  : V.Ivanchenko [email protected] 
// Version  : 
// Copyright : Your copyright notice 
// Description : Hello World in C++, Ansi-style 
//====================================== 

#include <iostream> 
#include "opencv.hpp" 
using namespace std; 
using namespace cv; 

#define VALID(x, y, w, h) ((x)>=0 && (y)>=0 && (x)<(w) && (y)<(h)) // validates index 

/* 
* 1. Original binary image HxW CV_8U 
*    | 
*    | 
*    V 
* 2. Two coordinate Mats HxW CV_32F 
*    | 
*    | 
*    V 
* 3. Visualization CV_8U 
*  a. gray HxW for a single coordinate image 
*  b. binary Rx360 for two coordinate images 
*/ 

// convert a binary 2D image into two Mats with float coordiantes 
void imageToCoord(const Mat& img, Mat& X, Mat& Y, bool centered = true) { 
    if (img.empty()) 
     return; 

    int h = img.rows; 
    int w = img.cols; 
    X.create(h, w, CV_32F); 
    Y.create(h, w, CV_32F); 
    float Cx = w/2.0f; 
    float Cy = h/2.0f; 

    for (int i=0; i<h; ++i){ 
     const uchar* img_row = img.ptr<uchar>(i); 
     float* x_row = X.ptr<float>(i); 
     float* y_row = Y.ptr<float>(i); 

     for (int j=0; j<w; ++j) { 
      if (img_row[j]>0) { 
       float x = j; 
       float y = i; 
       if (centered) { 
        x-=Cx; 
        y-=Cy; 
       } 
       x_row[j] = x; 
       y_row[j] = y; 
      } 
     } // j 
    } // i 
} //imageToCoord() 

// convert a single float ploar coord Mat to a gray image 
void polarToImg(const Mat& PolarCoord, Mat& img) { 
    if (PolarCoord.empty()) 
     return; 

    int h = PolarCoord.rows; 
    int w = PolarCoord.cols; 
    img.create(h, w, CV_8U); 
    float maxVal = std::numeric_limits<float>::min(); 

    // find maxVal 
    for (int i=0; i<h; ++i){ 
     const float* x_row = PolarCoord.ptr<float>(i); 
     for (int j=0; j<w; ++j) { 
      if (maxVal < x_row[j]) 
       maxVal = x_row[j]; 
     } // j 
    } // i 

    // create an image 
    if (maxVal>0) { 
     float k = 255.0/maxVal; 
     for (int i=0; i<h; ++i){ 
      uchar* img_row = img.ptr<uchar>(i); 
      const float* x_row = PolarCoord.ptr<float>(i); 
      for (int j=0; j<w; ++j) { 
       img_row[j] = saturate_cast<uchar>(k*x_row[j]); 
      }// j 
     } // i 
    } // if 
} // plarToImg() 

// convert two polar coord Mats to a binary image 
void polarToImg(const Mat& radius, const Mat& angle, Mat& img) { 
    if (angle.empty() || radius.empty()) 
     return; 

    int h = angle.rows; 
    int w = angle.cols; 
    assert(radius.cols==w && radius.rows==h); 
    const int imgH = sqrt(h*h+w*w)+0.5f; // radius 
    const int imgW = 360;     // angle, deg 
    img.create(imgH, imgW, CV_8U); 

    // create an image 
    for (int i=0; i<h; ++i){ 
     const float* ang_row = angle.ptr<float>(i); 
     const float* r_row = radius.ptr<float>(i); 

     for (int j=0; j<w; ++j) { 
      int x = ang_row[j] + 0.5f; 
      int y = r_row[j] + 0.5f; 

      if (x>0) { 
       cout<<x<<endl; 
      } 
      if (VALID(x, y, imgW, imgH)) 
       img.at<uchar>(y, x) = 255; 
      else { 
       cout<<"Invalid x, y: "<<x<<", "<<y<<endl; 
      } 
     }// j 
    } // i 
} // plarToImg() 

int main() { 
    cout << "Cartesian to polar" << endl; // prints "Syntax training in openCV" 
    const int W=400, H=400; 
    Mat Minput(H, W, CV_8U); 
    Minput(Rect(W/4, H/4, W/2, H/2)) = 255; 
    Mat X, Y, Angle, Radius, Mr, Mang, Mpolar; 

    // processing 
    imageToCoord(Minput, X, Y);    // extract coordinates 
    cartToPolar(X, Y, Radius, Angle, true);// convert coordiantes 

    // visualize 
    polarToImg(Radius, Mr); 
    polarToImg(Angle, Mang); 
    polarToImg(Radius, Angle, Mpolar); 

    // debug 
    //cout<<Mpolar<<endl; 

    namedWindow("input", 0); 
    namedWindow("angle", 0); 
    namedWindow("radius", 0); 
    namedWindow("Polar", 0); 

    const int winw=200, winh=200; 
    resizeWindow("input", winw, winh); 
    resizeWindow("angle", winw, winh); 
    resizeWindow("radius", winw, winh); 
    resizeWindow("Polar", 360, (int)sqrt(H*H + W*W)); 

    moveWindow("input", 0, 0); 
    moveWindow("angle", winw, 0); 
    moveWindow("radius", 2*winw, 0); 
    moveWindow("Polar", 3*winw, 0); 

    imshow("input", Minput); 
    imshow("angle", Mang); 
    imshow("radius", Mr); 
    imshow("Polar", Mpolar); 
    waitKey(-1); 

    return 0; 
}