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学习,包含数组与常数相乘,数组相加减,将数组拉成一维向量,图片的读入等内容)的更多相关文章

  1. k-means算法MATLAB和opencv代码

    上一篇博客写了k-means聚类算法和改进的k-means算法.这篇博客就贴出相应的MATLAB和C++代码. 下面是MATLAB代码,实现用k-means进行切割: %%%%%%%%%%%%%%%% ...

  2. 球体的双目视觉定位(matlab,附代码)

    球体的双目视觉定位(matlab,附代码) 标签(空格分隔): 机器视觉 引言 双目视觉定位是我们的一个课程设计,最近刚做完,拿出来与大家分享一下,实验的目的是在拍摄的照片中识别球体,并求出该球体到相 ...

  3. 代码: 两列图片瀑布流(一次后台取数据,图片懒加载。下拉后分批显示图片。图片高度未知,当图片onload后才显示容器)

    代码: 两列图片瀑布流(一次后台取数据,无ajax,图片懒加载.下拉后分批显示图片.图片高度未知,当图片onload后才显示容器) [思路]: 图片瀑布流,网上代码有多种实现方式,也有各类插件.没找到 ...

  4. 使用OC语言编写两个超大数相乘或相加的算法的思路和超大正整数相乘的代码

    正文: 在编程中,无论是OC还是C亦或是C++语言,所声明的整数变量都会在内存中占有固定的存储空间,而这些存储空间都是固定的. 比如我们知道的int.long.short.unsigend int.u ...

  5. (E2E_L2)包含目录、库目录、附加包含目录、附加库目录、附加依赖项之详解【正确的目录添加方法】

    VS项目中的包含目录.库目录.附加包含目录.附加库目录.附加依赖项均在"项目->属性->配置属性"下进行配置 一.具体说明如下: 1.VC++目录:     包含目录: ...

  6. Vs中包含目录、库目录、附加包含目录、附加库目录、附加依赖项之详解

    VS项目中的包含目录.库目录.附加包含目录.附加库目录.附加依赖项均在"项目->属性->配置属性"下进行配置,具体说明如下: VC++目录: 包含目录:寻找#inclu ...

  7. C++ 包含目录、库目录、附加包含目录、附加库目录、附加依赖项之详解(转)

    最近因为接触机器学习,所有涉猎到C++方面的开发.在c++中有几个概念很迷糊. VS项目中的包含目录.库目录.附加包含目录.附加库目录.附加依赖项均在"项目->属性->配置属性& ...

  8. 纯MATLAB版本 SIFT代码

    先贴几个链接: http://blog.csdn.net/abcjennifer/article/details/7639681  Rachel-Zhang的 http://blog.csdn.net ...

  9. JAVA代码根据经纬度范围计算WGS84与谷歌全球墨卡托包含的切片数目与拼接图像像素尺寸

    根据项目需求编写的代码. 适用场景:在网络地图上,比如天地图与谷歌地图,用户用鼠标在地图上拉一个矩形框,希望下载该矩形框内某一层级的瓦片数据,并将所有瓦片拼接成一个完整的,包含地理坐标的tif图像. ...

随机推荐

  1. Javascript - ExtJs - Ext.form.Panel组件

    FormPanel组件(Ext.form.FormPanel) logogram:Ext.form.Panel | xtype:form Ext.form.Panel.配置 frame }//旗下所有 ...

  2. 【NLP CS224N笔记】Lecture 1 - Introduction of NLP

    I. 什么是NLP NLP全称是Natural Language Processing,即自然语言处理,这是一门计算机科学.人工智能以及语言学的交叉学科. NLP涉及的几个层次由下图所示.可以看到输入 ...

  3. 【转】python模块导入细节

    [转]python模块导入细节 python模块导入细节 官方手册:https://docs.python.org/3/tutorial/modules.html 可执行文件和模块 python源代码 ...

  4. Linux中Grep常用的15个例子【转】

    转自:https://www.aliyun.com/jiaocheng/1390860.html?spm=5176.100033.1.9.6a1e41e8Pdjynm 摘要:Grep命令主要用于从文件 ...

  5. 在windows下用vagrant建立lnmp开发环境

    1.安装vagrant,vitrualbox 2.下载homestead的box包,并添加到vagrant 下载地址: https://atlas.hashicorp.com/laravel/boxe ...

  6. 使用cstdiofile在vs2010中无法写入中文的问题

    在VC2010环境下, 以下代码无法实现使用CStdioFile向文本文件中写入中文(用notepad.exe查看不到写入的中文) CStdioFile file; file.Open(…); fil ...

  7. maven项目板块的pom.xml配置

    项目名为helloweb 项目文件结构图1 helloweb>pom.xml内容如下: <project xmlns="http://maven.apache.org/POM/4 ...

  8. python---控制台输出带颜色的文字方法

    控制台的展示效果有限,并不能像前端一样炫酷,只能做一些简单的设置,不过站在可读性的角度来看,已经好很多了. 书写格式: ##格式: 设置颜色开始:\033[显示方式;前景色;背景色m ##说明: 前景 ...

  9. postman 设置api_token,测试数据等操作

    在postman的环境变量中先设置好一个用户的token(事先你的数据库里面要有用户的这个数据),如下图: 然后在api请求的url 的head头部添加对应的token键名,value值用花括号{{t ...

  10. Dubbo原理解析-Dubbo内核实现之SPI简单介绍

    转自:https://blog.csdn.net/quhongwei_zhanqiu/article/details/41577159 Dubbo 采用微内核+插件体系,使得设计优雅,扩展性强.那所谓 ...