matlab转c++代码实现(主要包含C++ std::vector,std::pair学习,包含数组与常数相乘,数组相加减,将数组拉成一维向量,图片的读入等内容)
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);
%+++
c++实现:
//fourmi-2018-09-06
/******************************include the head files********************************************/
#include<iostream>
#include<math.h>
#include<vector>
#include<cmath>
#include"/home/gaoshengjun/opencv-2.4.13/include/opencv/cv.h"
#include "/home/gaoshengjun/opencv-2.4.13/include/opencv/highgui.h"
#include <stdio.h>
#include <unistd.h>
#include <dirent.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <string.h>
/***********************************************************************************************/ /*******************************Macro definition************************************************/ //define PI , coordinate type, matrix type #define PI 3.1415926
#define POINT std::pair<int,int>
#define MATRIX std::vector<std::vector<float> >
#define VECT std::vector<float>
/***********************************************************************************************/ using namespace cv;//using opencv /*****************************calculate the angle***********************************************/
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;
};
/***********************************************************************************************/ /****************************calculate the distance********************************************/
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;
}
/***********************************************************************************************/ /********produce xmap(size:regionH X 1) according linspace(-regionW/2,regionW/2,regionW)********/
MATRIX produce_xmap_Matrix(float regionW,float regionH)
{
MATRIX 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); }
}
return array;
}
/***********************************************************************************************/ /******produce ymap(size:1 X regionW) according linspace(-regionH/2,regionH/2,regionH)**********/
MATRIX produce_ymap_Matrix(float regionW,float regionH)
{
MATRIX 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;
}
/***********************************************************************************************/ /******************************make SCALED MATRIX***********************************************/
MATRIX matrix_multi_const_number(MATRIX array,float scale)
{
MATRIX 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; } }
return result;
}
/***********************************************************************************************/ /*****************************MATRIX-I + MATRIX-II**********************************************/
MATRIX matrix_add_matrix(MATRIX array0,MATRIX array1)
{
MATRIX 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;
}
/***********************************************************************************************/ /************************MATRIX-I + const-number***********************************************/
MATRIX matrix_ADD_const_number(MATRIX array0,float num)
{
MATRIX 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;
}
/***********************************************************************************************/ /******************roudn each value in the array***********************************************/
VECT round(VECT array,char kind)
{
VECT 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;
}
/***********************************************************************************************/ /*****************************VECTER-I-VECTOR-II***********************************************/
VECT vector_sub_vector(VECT array0,VECT array1)
{
VECT result(array0.size());
int n=0;
for(int i=0;i<array0.size();i++)
{
result[i]=array0[i]-array1[i]; } return result;
}
/***********************************************************************************************/ /****************************MATRIX to VECTOR**************************************************/
VECT change_format(MATRIX array)
{
VECT 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;
}
/***********************************************************************************************/ /************************VECTOR-I.*VECTOR-II****************************************************/
VECT interpolation_by_dot_multi(VECT deltacxx,VECT deltaxfx,VECT deltacyy,VECT deltayfy,VECT imfxfy,
VECT imfxcy,VECT imcxfy, VECT imcxcy)
{
VECT roi(deltacxx.size()); for(int i=0;i<deltacxx.size();i++)
{ roi[i]=(imfxfy[i]*deltacxx[i]*deltacyy[i]+ imfxcy[i]* deltacxx[i]* deltayfy[i]+
imcxfy[i]* deltaxfx[i]* deltacyy[i] + imcxcy[i]* deltaxfx[i]* deltayfy[i]);
}
return roi; }
/***********************************************************************************************/ /******************************produce ZERO MATRIX**********************************************/
MATRIX zeros(int length,float num)
{
MATRIX 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;
}
/***********************************************************************************************/ /*****************************produce ONES MATRIX**********************************************/
MATRIX ones(int length,float num)
{
MATRIX 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]=1;
}
}
return result;
} /***********************************************************************************************/ /********************make sure each position of the two points*********************************/ POINT * compare_2_points_left_right(POINT &pt1,POINT &pt2)
{
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; };
/***********************************************************************************************/ /**************************calculate the mid of two points*************************************/
POINT cal_two_points_mid(POINT &pt1,POINT &pt2)
{
POINT midPoint;
midPoint.first=(pt1.first+pt2.first)/2;
midPoint.second=(pt1.second+pt2.second)/2;
return midPoint;
}
/***********************************************************************************************/ /**********************************MATRIX-I + MATRIX-II*****************************************/
MATRIX VECT_TO_MATRIX(VECT input,int h,int w)
{
MATRIX result(h);
int i,j;
int n=0;
for(i=0;i<h;i++)
result[i].resize(w);
for(i=0;i<w;i++)
{
for(j=0;j<h;j++)
{
result[j][i]=input[n++];
}
}
return result;
}
/***********************************************************************************************/ /*********************************if anyone is non-zero return true*****************************/
bool any_compare(VECT vec)
{
for(int i=0;i<vec.size();i++)
{
if (vec[i]!=0)
{
return true;
}
else
{
return false;
}
}
}
/***********************************************************************************************/ /********************VECT compare with const number*********************************************/
VECT vec_compare_const_number(VECT vec,int num,char type)
{
VECT result(vec.size());
for(int i=0;i<vec.size();i++)
{
if (type=='s')
{
if (vec[i]<num)
{
result[i]=1;
}
else result[i]=0;
}
else
{
if (vec[i]>num)
{
result[i]=1;
}
else result[i]=0;
}
}
return result;
}
/*******************************extract_Image_pixel in a special way****************************/
VECT extract_Image_pixel(Mat img,VECT map1,VECT map2,VECT map3,int channel_choose)
{
VECT result(map1.size());
for(int i=0;i<map1.size();i++)
{
int k0,k1,k2;
k0=map1[i];
k1=map2[i];
k2=map3[i];
Vec3b pix = img.at<Vec3b>(k0-1,k1-1);
result[i]=pix[channel_choose];
}
return result;
}
/***********************************************************************************************/ /***********************************extract_ROI*************************************************/
MATRIX extract_ROI( Mat img, MATRIX xmapInImage, MATRIX ymapInImage)
{
MATRIX roi_new;
int h,w,length;
int channel_choose=2;
VECT xmapInImage0,ymapInImage0,fxmap,fymap,cxmap
,cymap,deltacxx,deltaxfx,deltacyy,deltayfy,roi,zmap,imfxfy,imfxcy,imcxfy,imcxcy; 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=vector_sub_vector(cxmap,xmapInImage0);
deltaxfx=vector_sub_vector(xmapInImage0,fxmap);
deltacyy=vector_sub_vector(cymap,ymapInImage0);
deltayfy=vector_sub_vector(ymapInImage0,fymap); roi=change_format(zeros(length,1));
zmap=change_format(ones(length,1));
imfxfy=change_format(zeros(length,1));
imfxcy=change_format(zeros(length,1));
imcxfy=change_format(zeros(length,1));
imcxcy=change_format(zeros(length,1)); imfxfy=extract_Image_pixel(img,fymap,fxmap,zmap,channel_choose);
imfxcy=extract_Image_pixel(img,cymap,fxmap,zmap,channel_choose);
imcxfy=extract_Image_pixel(img,fymap,cxmap,zmap,channel_choose);
imcxcy=extract_Image_pixel(img,cymap,cxmap,zmap,channel_choose); roi=interpolation_by_dot_multi(deltacxx,deltaxfx,deltacyy,deltayfy,imfxfy, imfxcy,imcxfy, imcxcy); roi_new=VECT_TO_MATRIX(roi,h,w); return roi_new;
}
/***********************************************************************************************/ /***********************************extractEntranceLineRegion***********************************/
MATRIX extractEntranceLineRegion(POINT &pt1,POINT &pt2,Mat img)
{
const float regionW=96;
const float regionH=24;
const int offset=24;
int ImageHeight=img.rows;
int ImageWidth=img.cols;
double angle;
bool outofboundary=false;
MATRIX xmap,ymap,xmapScaled,ymapScaled,xmapInImage,ymapInImage,roi;
POINT new_pt1,new_pt2,midPoint;
POINT * arr;
float widthOfTheRealRegion ;
xmap=produce_xmap_Matrix(regionW,regionH);
ymap=produce_ymap_Matrix(regionW,regionH); arr=compare_2_points_left_right(pt1,pt2);
new_pt1=arr[0];
new_pt2=arr[1]; angle=cal_angle(new_pt1,new_pt2); widthOfTheRealRegion=cal_distance(new_pt1,new_pt2,offset); midPoint=cal_two_points_mid(new_pt1,new_pt2); xmapScaled=matrix_multi_const_number(xmap,widthOfTheRealRegion/regionW);
ymapScaled=matrix_multi_const_number(ymap,regionH/regionH); xmapInImage=matrix_ADD_const_number(matrix_add_matrix(matrix_multi_const_number(xmapScaled,cos(angle)),matrix_multi_const_number(ymapScaled,sin(angle))),midPoint.first);
ymapInImage=matrix_ADD_const_number(matrix_add_matrix(matrix_multi_const_number(xmapScaled,-sin(angle)),matrix_multi_const_number(ymapScaled,cos(angle))),midPoint.second);
/*make sure the pixel is bigger than 1 and the size of the point is under the readed image */
if (any_compare(vec_compare_const_number(change_format(xmapInImage),1,'s'))or (any_compare(vec_compare_const_number(change_format(xmapInImage),ImageWidth,'b'))) or
(any_compare(vec_compare_const_number(change_format(ymapInImage),1,'s')))or(any_compare(vec_compare_const_number(change_format(ymapInImage),ImageHeight,'b'))))
{
outofboundary = true;
}
if (!outofboundary)
{
roi = extract_ROI(img,xmapInImage,ymapInImage);
}
else
roi=zeros(regionH,regionW); return roi;
}
/***********************************************************************************************/ /*******************************FUNCTION MAIN()*************************************************/
int main()
{
MATRIX roi;
POINT point1(171,213);
POINT point2(171,145);
Mat img=imread("./000338.jpg", CV_LOAD_IMAGE_UNCHANGED); roi=extractEntranceLineRegion(point1,point2,img);
for(int i=0;i<roi.size();i++)
{
for(int j=0;j<roi[0].size();j++)
{
std::cout<<roi[i][j]<<" ";
}
std::cout<<std::endl<<std::endl;
}
return 0;
} /*******************************END***********************************************************/
matlab转c++代码实现(主要包含C++ std::vector,std::pair学习,包含数组与常数相乘,数组相加减,将数组拉成一维向量,图片的读入等内容)的更多相关文章
- k-means算法MATLAB和opencv代码
上一篇博客写了k-means聚类算法和改进的k-means算法.这篇博客就贴出相应的MATLAB和C++代码. 下面是MATLAB代码,实现用k-means进行切割: %%%%%%%%%%%%%%%% ...
- 球体的双目视觉定位(matlab,附代码)
球体的双目视觉定位(matlab,附代码) 标签(空格分隔): 机器视觉 引言 双目视觉定位是我们的一个课程设计,最近刚做完,拿出来与大家分享一下,实验的目的是在拍摄的照片中识别球体,并求出该球体到相 ...
- 代码: 两列图片瀑布流(一次后台取数据,图片懒加载。下拉后分批显示图片。图片高度未知,当图片onload后才显示容器)
代码: 两列图片瀑布流(一次后台取数据,无ajax,图片懒加载.下拉后分批显示图片.图片高度未知,当图片onload后才显示容器) [思路]: 图片瀑布流,网上代码有多种实现方式,也有各类插件.没找到 ...
- 使用OC语言编写两个超大数相乘或相加的算法的思路和超大正整数相乘的代码
正文: 在编程中,无论是OC还是C亦或是C++语言,所声明的整数变量都会在内存中占有固定的存储空间,而这些存储空间都是固定的. 比如我们知道的int.long.short.unsigend int.u ...
- (E2E_L2)包含目录、库目录、附加包含目录、附加库目录、附加依赖项之详解【正确的目录添加方法】
VS项目中的包含目录.库目录.附加包含目录.附加库目录.附加依赖项均在"项目->属性->配置属性"下进行配置 一.具体说明如下: 1.VC++目录: 包含目录: ...
- Vs中包含目录、库目录、附加包含目录、附加库目录、附加依赖项之详解
VS项目中的包含目录.库目录.附加包含目录.附加库目录.附加依赖项均在"项目->属性->配置属性"下进行配置,具体说明如下: VC++目录: 包含目录:寻找#inclu ...
- C++ 包含目录、库目录、附加包含目录、附加库目录、附加依赖项之详解(转)
最近因为接触机器学习,所有涉猎到C++方面的开发.在c++中有几个概念很迷糊. VS项目中的包含目录.库目录.附加包含目录.附加库目录.附加依赖项均在"项目->属性->配置属性& ...
- 纯MATLAB版本 SIFT代码
先贴几个链接: http://blog.csdn.net/abcjennifer/article/details/7639681 Rachel-Zhang的 http://blog.csdn.net ...
- JAVA代码根据经纬度范围计算WGS84与谷歌全球墨卡托包含的切片数目与拼接图像像素尺寸
根据项目需求编写的代码. 适用场景:在网络地图上,比如天地图与谷歌地图,用户用鼠标在地图上拉一个矩形框,希望下载该矩形框内某一层级的瓦片数据,并将所有瓦片拼接成一个完整的,包含地理坐标的tif图像. ...
随机推荐
- 20165337岳源 第四次实验 Android开发
1.实验要求: 参考<Java和Android开发学习指南(第二版)(EPUBIT,Java for Android 2nd)>第二十四章 参考http://www.cnblogs.com ...
- 2017-2018-2 20165221 实验三《敏捷开发与XP实践》实验报告
实验报告封面 姓名:谭笑 学号:20165221 班级:1652班 实验名称: JAVA--敏捷开发与XP实践 指导老师:娄嘉鹏 试验时间:15:35--17:15 实验性质:选修 实验组次:21 实 ...
- Log4j日志根据配置输出到多个自定义文件
最近工作中遇到所有日志需要记录到一个文件,而错误的sql执行记录到另一个文件中,查询了一些资料搞定,记录下来.顺便吐槽下公司限制印象笔记的使用. ##log4j.rootLogger=INFO, CO ...
- 在Python中用Selenium执行JavaScript
Selenium自己不带浏览器, 需要与第三方浏览器结合在一起使用.例如在Firefox上运行Selenium. PhantomJS是一个"无头"浏览器. 它会把网站加载到内存并执 ...
- [转] Torch中实现mini-batch RNN
工作中需要把一个SGD的LSTM改造成mini-batch的LSTM, 两篇比较有用的博文,转载mark https://zhuanlan.zhihu.com/p/34418001 http://ww ...
- [sklearn] 官方例程-Imputing missing values before building an estimator 随机填充缺失值
官方链接:http://scikit-learn.org/dev/auto_examples/plot_missing_values.html#sphx-glr-auto-examples-plot- ...
- Vue对变量的监控
Vue对变量的监控 watch: { a(val, oldVal) {//普通的watch监听 if (val == "1") { $('#myModal').modal(); } ...
- Iterables vs. Iterators vs. Generators
Reprinted from: Iterables vs. Iterators vs. Generators Occasionally I've run into situations of conf ...
- Crash工具实战-变量解析【转】
转自:http://blog.chinaunix.net/uid-14528823-id-4358785.html Crash工具实战-变量解析 Crash工具用于解析Vmcore文件,Vmcore文 ...
- MII与RMII接口的区别【转】
转自:https://blog.csdn.net/fun_tion/article/details/70270632 1.概述 MII即“媒体独立接口”,也叫“独立于介质的接口”.它是IEEE-802 ...