1.首先opencv imread一张图片到内存,参考:imread

2.读取每个像素点的BGR 3通道的值,并逐行写入到文件。

#include <iostream>
#include <fstream>
#include <opencv2/highgui/highgui.hpp>

using namespace std;
using namespace cv;

int main(int argc, char **argv) {
  Mat src = imread(\"/home/abb/Downloads/red_left_00045.jpg\",IMREAD_COLOR);
  if(src.empty()){
    cout << \"Image is empty\"<<endl;
    return -1;
  }
  cout << \"image width * hight = \" << src.cols << \" * \" << src.rows << endl;
  fstream file(\"/home/abb/2.txt\",ios::out);
  for(int nrows = 0; nrows < src.rows; nrows++){
    for(int ncols = 0; ncols < src.cols; ncols++ ){
      Vec3b BGR_v = src.at<Vec3b>(nrows,ncols);
      file << \"(\"<< (int)BGR_v.val[0] <<\",\"<<(int)BGR_v.val[1]<<\",\"<<(int)BGR_v.val[2] <<\") \";
      if(ncols + 1 ==  src.cols){
        file <<endl;
      }
    }
 }
 return 0;
}

 

收藏 打印