遇到opencv没有CSV文件读取
自己写了个灰度16bit CSV文件的读取
#include "opencv.hpp"
#include <fstream>
#include <iostream>
using namespace std;
using namespace cv;
//one channel
Mat read_csv(char *filepath, Size img_size, int img_type)
{
Mat image;
image.create(img_size,img_type);
string pixel;
ifstream file(filepath, ifstream::in);
if (!file) cout << "CSV read fail" << endl;
int nl= image.rows; // number of lines
int nc= image.cols ; // number of columns
if (image.isContinuous())
{
// then no padded pixels
nc= nc*nl;
nl= 1; // it is now a 1D array
}
for (int j=0; j<nl; j++)
{
ushort* data= image.ptr<ushort>(j);
for (int i=0; i<nc; i++)
{
getline(file,pixel,',');
*data++= atoi(pixel.c_str());
}
}
// cout << image << endl;
return image;
}
//example code
Mat img = read_csv("F:/Depth.csv",Size(320,240),CV_16U);
img.convertTo(img,CV_8U,0.1);
// cout << img << endl;
imshow("Depth",img);
waitKey(0);