码迷,mamicode.com
首页 > 编程语言 > 详细

matlab转c++代码实现(主要包含C++ std::vector,std::pair学习,包含数组与常数相乘,数组相加减,将数组拉成一维向量等内容)

时间:2018-09-04 19:12:29      阅读:479      评论:0      收藏:0      [点我收藏+]

标签: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-->p2
vecp1p2 = labelData(2,:) - labelData(1,:);
angle = -atan2(vecp1p2(2), vecp1p2(1)); %角度计算 四象限反正切
widthOfTheRealRegion = norm(vecp1p2) + offset; %  求p1p2点距离,开根号 +offset
midPoint = mean(labelData,1); % 取中点

xmapScaled = xmap * widthOfTheRealRegion / regionW; %对坐标进行scale
ymapScaled = 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学习,包含数组与常数相乘,数组相加减,将数组拉成一维向量等内容)

标签:out   data   points   map   ima   str   include   har   pac   

原文地址:https://www.cnblogs.com/fourmi/p/9585978.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!