matlab轉c++代碼實現(主要包含C++ std::vector,std::pair學習,包含數組與常數相乘,數組相加減,將數組拉成一維向量等內容)

來源:互聯網
上載者:User

標籤:out   data   points   map   ima   str   include   har   pac   

MATLAB部分:xmap = repmat( linspace( -regionW/2, regionW/2, regionW), regionH, 1 );%linspace [x1,x2,N] 等差數列 ymap = repmat( linspace( -regionH/2, regionH/2, regionH)‘, 1, regionW);  %轉置%compute the angle of the vector p1-->p2vecp1p2 = labelData(2,:) - labelData(1,:);angle = -atan2(vecp1p2(2), vecp1p2(1)); %角度計算 四象限反正切widthOfTheRealRegion = norm(vecp1p2) + offset; %  求p1p2點距離,開根號 +offsetmidPoint = mean(labelData,1); % 取中點xmapScaled = xmap * widthOfTheRealRegion / regionW; %對座標進行scaleymapScaled = ymap * 24 / regionH;%add for Alexnet  %ymapScaled = ymap * 24 / regionH;%add for Alexnet bywxq  xmapInImage = cos(angle) * xmapScaled + sin(angle) * ymapScaled + midPoint(1);  % 順時針旋轉ymapInImage = -sin(angle) * xmapScaled + cos(angle) * ymapScaled + midPoint(2);%++++++++++++++++++++++++++++++++++++++

  

[h, w] = size(xmap);length = h * w;xmap = xmap(:);ymap = ymap(:);fxmap = floor( xmap ); %向下取整fymap = floor( ymap );cxmap = ceil( xmap ); %向上取整cymap = ceil( ymap );%插值係數deltacxx = cxmap - xmap;deltaxfx = xmap - fxmap;deltacyy = cymap - ymap;deltayfy = ymap - fymap;roi = zeros(length, 1);

  

C++實現://author: fourmi-2018-09-04#include<iostream>#include<math.h>#include<vector>#include<cmath>#define pi 3.1415926#define POINT   std::pair<int,int>#define VECT    std::vector<std::vector<float> >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]<<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<<ymap[j][i]<<"   ";     }     std::cout<<std::endl<<std::endl;     std::cout<<xmap.size()<<"  "<<xmap[0].size()<<std::endl;   }   */   return 0;}

  

matlab轉c++代碼實現(主要包含C++ std::vector,std::pair學習,包含數組與常數相乘,數組相加減,將數組拉成一維向量等內容)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.