Matlab to C + + code implementation (mainly includes C + + STD::VECTOR,STD::p air learning, including array and constant multiplication, array addition minus, pull the array into one-dimensional vector, etc.)

Source: Internet
Author: User
Tags abs cos pow sin

Matlab section: xMAP = Repmat (Linspace (-REGIONW/2, REGIONW/2, regionw), Regionh, 1);%linspace [x1,x2,n] arithmetic progression ymap = Repmat (li Nspace (-REGIONH/2, REGIONH/2, Regionh) ', 1, regionw);  % transpose%compute the angle of the vector p1-->p2vecp1p2 = labeldata (2,:)-labeldata (1,:); angle =-atan2 (VECP1P2 (2), VECP1P2 (1)); The% angle calculates the four-quadrant inverse tangent widthoftherealregion = norm (vecp1p2) + offset; %  seek p1p2 point distance, open radical +offsetmidpoint = mean (labeldata,1);% take midpoint xmapscaled = xMAP * WIDTHOFTHEREALREGION/REGIONW;% SCA for coordinates leymapscaled = Ymap * 24/regionh;%add for Alexnet  %ymapscaled = ymap * 24/regionh;%add for Alexnet bywxq  Xmapi nImage = cos (angle) * xmapscaled + sin (angle) * ymapscaled + midpoint (1);  % Clockwise Rotation ymapinimage =-sin (angle) * xmapscaled + cos (angle) * ymapscaled + midpoint (2);%++++++++++++++++++++++++++++++++++ ++++

  

[H, W] = size (xmap), length = h * w;xmap = xMAP (:); ymap = Ymap (:); fxmap = floor (xmap);% Rounding Fymap = floor (ymap); cxmap = Ceil (xMAP); % Rounding Cymap = Ceil (ymap);% interpolation factor deltacxx = Cxmap-xmap;deltaxfx = Xmap-fxmap;deltacyy = Cymap-ymap;deltayfy = Ymap-f Ymap;roi = Zeros (length, 1);

  

C + + implementation://author:fourmi-2018-09-04#include<iostream> #include <math.h> #include <vector> #include <cmath> #define PI 3.1415926#define point std::p air<int,int> #define VECT STD::VECTOR&LT;STD::VECTOR&LT;FL oat> >const float regionw=96;const float regionh=24;const int offset=24;float cal_angle (Point &pt1,point &        PT2) {double angle;    if ((Pt2.first-pt1.first) ==0) {ANGLE=PI/2;                  } else {angle=std::abs (Atan (Pt2.second-pt1.second)/(Pt2.first-pt1.first)); } return angle;};   Float cal_distance (point &pt1,point &pt2,const int offset) {float distance;   Distance=sqrt (Pow ((Pt2.second-pt1.second), 2) +pow ((Pt2.first-pt1.first), 2)) +offset;   std::cout<< "1111:" <<distance<<std::endl; return distance;}   VECT Make_xmap (float regionw,float regionh) {VECT array (REGIONH);   int i,j;   For (I=0;i<array.size (); i++) array[i].resize (REGIONW);  For (I=0;i<array.size (); i++) {     For (J=0;j<array[0].size (); j + +) {array[i][j]=-regionw/2+j* (REGIONW)/(REGIONW-1);   }//std::cout<<array.size () <<std::endl; } return array;   VECT Make_ymap (float regionw,float regionh) {VECT array (REGIONH);   int i,j;   For (I=0;i<array.size (); i++) array[i].resize (REGIONW); For (I=0;i<array[0].size (), i++) {for (J=0;j<array.size (); j + +) {array[j][i]=-regionh/2+j* (regionh        )/(REGIONH-1);      std::cout<<j<< "" <<i<< "";   }//std::cout<<std::endl; } return array;    VECT scaled (VECT array,float scale) {VECT result (array.size ());    int i,j;    For (I=0;i<array.size (); i++) Result[i].resize (Array[0].size ());        For (I=0;i<array.size (), i++) {for (J=0;j<array[0].size (); j + +) {Result[i][j]=array[i][j]*scale;      std::cout<<result[i][j]<< "";    }//std::cout<<std::endl; } return result; VECT ADD (VECT array0,vect array1) {VECT result (array0.size ());    int i,j;    For (I=0;i<array0.size (); i++) Result[i].resize (Array0[0].size ()); For (I=0;i<array0.size (), i++) {for (J=0;j<array0[0].size (); j + +) {result[i][j]=array0[i][j]+       ARRAY1[I][J]; }} return result;    VECT add_const (VECT array0,float num) {VECT result (array0.size ());    int i,j;    For (I=0;i<array0.size (); i++) Result[i].resize (Array0[0].size ()); For (I=0;i<array0.size (), i++) {for (J=0;j<array0[0].size (); j + +) {result[i][j]=array0[i][j]+       Num }} return result; Std::vector<float> round (std::vector<float> Array,char kind) {std::vector<float> result (    Array.size ());    int n=0;         for (int i=0;i<array.size (); i++) {if (kind== ' F ') {Result[i]=floor (array[i]);         } else {Result[i]=ceil (array[i]); }} return result;}std::vector<float> sub_vector (std::vector<float> array0,std::vector<float> array1) {std::vector    <float> result (Array0.size ());    int n=0;             for (int i=0;i<array0.size (); i++) {result[i]=array0[i]-array1[i]; } return result;    Std::vector<float> Change_format (VECT array) {std::vector<float> result (Array.size () *array[0].size ());    int n=0;          for (int i=0;i<array[0].size (), i++) {for (int j=0;j<array.size (); j + +) {Result[n]=array[j][i];      n++; }} return result;    VECT zeros (int length,float num) {VECT result (length);    int i,j;    for (i=0;i<length;i++) result[i].resize (num);       for (i=0;i<length;i++) {for (j=0;j<num;j++) {result[i][j]=0; }} return result;    Point * compare_2_points (point &pt1,point &pt2,double angle,int offset) {int x_left,x_right,y_down,y_up;    Point New_pt1,new_pt2; Static Point Arr[2];    /* Pt1.first=std::abs (Pt1.first);    Pt2.first=std::abs (Pt2.first);    Pt1.second=-std::abs (Pt1.second);   Pt2.second=-std::abs (Pt2.second);       */if (Pt2.first<pt1.first) {x_left=pt2.first;    X_right=pt1.first;       } else {X_left=pt1.first;    X_right=pt2.first;    } std::cout<< "X_left:" <<x_left<< "X_right:" <<x_right<<std::endl;       if (Pt2.second<pt1.second) {y_down=pt2.second;     Y_up=pt1.second;       } else {Y_down=pt1.second;    Y_up=pt2.second;    } std::cout<< "Y_down:" <<y_down<< "y_up:" <<y_up<<std::endl;    New_pt1.first=x_left;    NEW_PT1.SECOND=Y_UP;    New_pt2.first=x_right;    New_pt2.second=y_down;    ARR[0]=NEW_PT1;    Arr[1]=new_pt2;    return arr; };   Point Make_mid (Point &pt1,point &pt2) {point midpoint;   midpoint.first= (Pt1.first+pt2.first)/2;   Midpoint.second= (Pt1.second+pt2.second)/2; return midpoint;} Point * make_New_points (Point &pt1,point &pt2,double angle,int offset) {double x_offset,y_offset;    Point New_pt1,new_pt2;    Static Point arr[2];    x_offset= (offset)/2.0*cos (angle);       y_offset= (offset)/2.0*sin (angle);    New_pt1.first=pt1.first-x_offset;    New_pt1.second=pt1.second+y_offset;    New_pt2.first=pt2.first+x_offset;    New_pt2.second=pt2.second-y_offset;    ARR[0]=NEW_PT1;    Arr[1]=new_pt2;    return arr;   };int Main () {double angle;     VECT Xmap,ymap,xmapscaled,ymapscaled,xmapinimage,ymapinimage;   Point Point1 (171,213);   Point Point2 (171,145);   Point New_pt1,new_pt2,midpoint;   Point * ARR;   float widthoftherealregion;   int h,w,length; Std::vector<float> Xmapinimage0,ymapinimage0,fxmap,fymap,cxmap,cymap,deltacxx,deltaxfx,deltacyy,deltayfy,   Roi   Xmap=make_xmap (REGIONW,REGIONH);   Ymap=make_ymap (REGIONW,REGIONH);   Arr=compare_2_points (point1,point2,0,0);   NEW_PT1=ARR[0];   NEW_PT2=ARR[1];   Angle=cal_angle (NEW_PT1,NEW_PT2); Std::cout<<angle<<std::endl;   Widthoftherealregion=cal_distance (New_pt1,new_pt2,offset);   Midpoint=make_mid (NEW_PT1,NEW_PT2);    std::cout<<midpoint.first<< "" <<midPoint.second<<std::endl;   Xmapscaled=scaled (XMAP,WIDTHOFTHEREALREGION/REGIONW);      Ymapscaled=scaled (YMAP,24/REGIONH);   Xmapinimage=add_const (ADD (Scaled (xmapscaled,cos (angle)), scaled (ymapscaled,sin (angle))), Midpoint.first);   Ymapinimage=add_const (ADD (Scaled (xmapscaled,-sin (angle)), scaled (Ymapscaled,cos (angle))), Midpoint.second);   H=xmapinimage.size ();   W=xmapinimage[0].size ();   Length=h*w;   Xmapinimage0=change_format (Xmapinimage);   Ymapinimage0=change_format (Ymapinimage);   Fxmap=round (XmapInImage0, ' f ');   Fymap=round (YmapInImage0, ' f ');   Cxmap=round (XMAPINIMAGE0, ' C ');   Cymap=round (YMAPINIMAGE0, ' C ');   Deltacxx=sub_vector (CXMAP,XMAPINIMAGE0);   Deltaxfx=sub_vector (XMAPINIMAGE0,FXMAP);   Deltacyy=sub_vector (CYMAP,YMAPINIMAGE0);   Deltayfy=sub_vector (YMAPINIMAGE0,FYMAP); Roi=change_format (ZEros (length,1)); for (int i=0;i<ymapscaled.size () *ymapscaled[0].size (); i++) {std::cout<<i<< "" <<roi[i]<&l            T;std::endl;   }//std::cout<<xmapinimage.size () << "" <<xmapinimage[0].size () <<std::endl;   Std::cout<<ymapinimage.size () << "<<ymapinimage[0].size" () <<std::endl;   /* Arr=make_new_points (New_pt1,new_pt2,angle,offset);   NEW_PT1=ARR[0];   NEW_PT2=ARR[1];   std::cout<< "1111111111111:" <<new_pt1.first<< "<<new_pt1.second<<std::endl;   std::cout<< "2222222222222:" <<new_pt2.first<< "<<new_pt2.second<<std::endl; for (int i=0;i<ymap[0].size (), i++) {for (int j=0;j<ymap.size (); j + +) {STD::COUT&LT;&LT;YMAP[J][I]&L     t;< "";     } std::cout<<std::endl<<std::endl;   Std::cout<<xmap.size () << "<<xmap[0].size" () <<std::endl; } */return 0;}

  

Matlab to C + + code implementation (mainly includes C + + STD::VECTOR,STD::p air learning, including array and constant multiplication, array addition minus, pull the array into one-dimensional vector, etc.)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.