Mat和vector的相互转化
转https://blog.csdn.net/guyuealian/article/details/80253066
/***************** Mat转vector **********************/
template
vector<_Tp> convertMat2Vector(const Mat &mat)
{
return (vector<_Tp>)(mat.reshape(1, 1));//通道数不变,按行转为一行
}
/****************** vector转Mat *********************/
template
cv::Mat convertVector2Mat(vector<_Tp> v, int channels, int rows)
{
cv::Mat mat = cv::Mat(v);//将vector变成单列的mat
cv::Mat dest = mat.reshape(channels, rows).clone();//PS:必须clone()一份,否则返回出错
return dest;
}
//velc.reshape(0, numelementsy);
vector 创建和初始化全为0
vector a(b.size(),0);
vector> a(b.size(), vector(c.size(), 0));
Mat:
Mat velcomp = Mat::zeros(numelementsy, numelementsx, CV_32F);
1、 repmat =repeat
Matlab:
x = repmat((min:step:max), num, 1)
Opencv:
Mat x;
vector xtemp,;
for (int i = min; i < max+1;i=i+ step) {
xtemp.push_back(i );
x= Mat(xtemp).t();
}
x= repeat(x, num, 1);
2、fspecial(gaussian)=getGaussianKernel ,, imfilter=filter2D
Matlab:
h=fspecial('gaussian',3,1);
x=imfilter(x,h,'replicate');
Opencv:
Mat kernel = getGaussianKernel(3, 1) * getGaussianKernel(3, 1).t();
filter2D(x, x, -1, kernel, Point(-1, -1), 0.0, IPL_BORDER_REPLICATE);
3、interp2 =remap(三次样条插值),这个不太对
Matlab:
m=interp2(xold,yold,m,x,y,'*spline');
用指定的算法method 计算二维插值:
’linear’:双线性插值算法(缺省算法);
’nearest’:最临近插值;
’spline’:三次样条插值;
’cubic’:双三次插值。
opencv:
remap(m, m, x, y,INTER_CUBIC);
INTER_NEAREST 最近邻插值
INTER_LINEAR 双线性插值
INTER_CUBIC 双三次插值
https://stackoverflow.com/questions/46692060/matlab-interp2-function-behaviour-differently-compared-with-opencv-remap
opencv需要减1,xold必须是0,1,2,3....,需要映射
4、padarray 填充
Matlab:
x= padarray(x, [1,1], 'replicate');
'replicate':边界,K:固定值
Opencv:
//用K填充图像的边界RowPad上下扩充,ColPad左右扩充
Mat PadArray(Mat Image, int RowPad, int ColPad,int K)
{
int n = Image.rows;
int m = Image.cols;
Mat temp1 = Mat::zeros(n, m + ColPad * 2, Image.type());
Mat temp2 = Mat::zeros(n + RowPad * 2, m + ColPad * 2, Image.type());
Mat c1 = Mat(RowPad, m + ColPad * 2, CV_32F,K);
Mat c2 = Mat(n, ColPad, CV_32F,K);
Image.copyTo(temp1.colRange(ColPad, m + ColPad));
c2.copyTo(temp1.colRange(0, ColPad));
c2.copyTo(temp1.colRange(m + ColPad, m + ColPad * 2));
c1.copyTo(temp2.rowRange(0, RowPad));
temp1.copyTo(temp2.rowRange(RowPad, n + RowPad));
c1.copyTo(temp2.rowRange(n + RowPad, n + RowPad * 2));
return temp2;
}
5、interp1 分段线性插值
Matlab:
firstlinex_intp=interp1(xold,y,xnew,'linear','extrap');
Opencv:
void interp1(vector x, vector y, vector &x2, vector&y2) {
int n = x.size();
int k = x2.size();
for (int j = 0; j < k; j++) {
for (int i = 1; i < n - 1; i++) {
if (x[i] <= x2[j] && x2[j] < x[i + 1]) {
double s = x2[j] - x[i];
double delta = (y[i + 1] - y[i]) / (x[i + 1] - x[i]);
y2.push_back(y[i] + s * delta);
break;
}
//对溢出边界做处理
else if(x2[j]<=x[0]){
double s = x2[j] -x[0];
double delta = (y[1] - y[0]) / (x[1] - x[0]);
y2.push_back(y[0] + s * delta);
break;
}
//对溢出边界做处理
else if (x2[j] >= x[n - 2]) {
double s = x2[j] - x[n-2];
double delta = (y[n-1] - y[n-2]) / (x[n-1] - x[n-2]);
y2.push_back(y[n-2] + s * delta);
break;
}
}
}
}
6、vector,赋值等差序列
std::vector X1(100);
std::iota(X1.begin(), X1.end(), 1);
先这样吧。。。