Matlab 坐标、概念的变换

Matlab 坐标、概念的变换,matlab,Matlab,我想把x,y,z坐标转换成极坐标。我在y坐标中得到(-)。有人能解释一下我为什么得到它吗。这将是很大的帮助 我从软件中读取这些值(xyz,az_elev_r),无法更改。我只是不确定角度的顺序(az&elev)。使用我的代码,我得到-y而不是y。这意味着有180个旋转。我的代码是: xyz=[-0.564 3.689 -0.735; 2.011 5.067 -1.031; -1.181 3.943 -1.825; % Reference values ]; %%a

我想把x,y,z坐标转换成极坐标。我在y坐标中得到(-)。有人能解释一下我为什么得到它吗。这将是很大的帮助

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

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
矩阵与您的
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
函数,并以弧度工作,您的代码将更具可读性,除非您试图理解转换本身的原因。

OpenCV有转换为极坐标和返回极坐标的代码。此转换对于通过相关或其他方式查找对象旋转非常有用rwise创建以对象为中心的“旋转无关”对象表示。可视化每个极坐标及其关节图像非常有用。下面的图像应该是自解释的。极坐标图的角度为水平轴,半径为垂直轴,因此4个峰值对应于输入图像的4个角e、 代码(带有OpenCV的C++)附在后面。

//================================
//名称:PolarCoord.cpp
//作者:V.Ivanchenkocudassimo@gmail.com
//版本:
//版权:您的版权声明
/ C++:ANSI风格的Hello World
//======================================
#包括
#包括“opencv.hpp”
使用名称空间std;
使用名称空间cv;
#定义有效的(x,y,w,h)((x)>=0&&(y)>=0&&(x)我正在从软件读取这些值(xyz,az_elev_r),无法更改。
>> [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
//================================
// Name        : PolarCoord.cpp
// Author      : V.Ivanchenko cudassimo@gmail.com
// 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;
}