1.首先给出opencv文档中,对 rowRange和colRange的定义:cv::Mat Class Reference
下面是解释和翻译:
函数原型:Mat cv::Mat::colRange(int startcol, int endcol) const
功能:为指定的列跨度创建一个矩阵头。
该方法为矩阵的指定列跨度创建一个矩阵头。 类似于Mat :: row和Mat :: col,这是一个O(1)操作
参数:(分别表示我们想取的Mat的第几列到第几列)
startcol:一个包含0的起始索引的列跨度。
endcol:一个除0以外的结束索引的列跨度。
函数原型:Mat cv::Mat::colRange(const Range& r) const
功能:这是为了方便起见,它是一个重载的成员函数。 它与上述函数的区别仅在于它接受的参数。
参数:
r:包含开始和结束索引的范围结构。
rowRange与colRange类似,不再解释
2.代码示例:
#include <opencv2/photo/photo.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <vector>
#include <string>
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
using namespace cv;
void loadExposureSeq(String, vector<Mat>&);//加载图像
int main()
{
String path = "D:\\project\\StructuredLight_Project\\StructuredLight\\cam\\cam_01\\";
vector<Mat> images;
loadExposureSeq(path, images);
return 0;
}
void loadExposureSeq(String path, vector<Mat>& images)
{
ifstream list_file((path + "list.txt").c_str());//文件读操作,存储设备读取到内存中
string name;//txt文件中保存的图片名字
while (list_file >> name ) {
Mat img = imread(path + name);
images.push_back(img);
imshow("img", img);
waitKey(0);
}
Mat res(576, 5 * 768, CV_8UC3);
for (int i = 0; i < 5; i++)
{
res.colRange(i * 768, (i + 1) * 768) = images[i] +0;//从mat中提取特定列
}
imwrite("a.png", res);
waitKey(0);
list_file.close();//关闭文件流
}
这个函数的作用是:加载了5张图片,存放到一个Mat中,效果如下图所示: