reshape不需要内存拷贝或者重新分配就能改变矩阵的维度
Mat reshape(int cn, int rows=0) const;
cn:表示通道数channels,如果设为0,则表示保持通道数不变,否则则变为设置的通道数
rows;表示矩阵函数,如果设为0,则表示所有函数不变,否则则变为设置的函数
Mat reshape(int cn, int newndims, const int* newsz) const;提供方便的重载函数,和上面的函数只有输入参数不同。
程序代码:
int main(void)
{
Mat Image = imread("F:\\1\\yellow_lane\\0.png", 1);
cout<<"channel:"<<Image.channels()<<endl;
cout<<"rows:"<<Image.rows<<endl;
cout<<"cols:"<<Image.cols<<endl;
cout<<endl;
Mat dst = Image.reshape(1, Image.cols*Image.channels()*Image.rows);
cout<<"channel:"<<dst.channels()<<endl;
cout<<"rows:"<<dst.rows<<endl;
cout<<"cols:"<<dst.cols<<endl;
return 0;
}
需要注意的是,如果程序这样写:
Image.reshape(1, Image.cols*Image.channels()*Image.rows);
Image的行列变换是不会显现出有改变的