用opencv提取rgb,并存储到txt文件

#include<opencv2/opencv.hpp>
#include<iostream>
#include <fstream>
#include<stdio.h>
#include <cv.h>//"opencv/cv.h"//
#include "opencv/highgui.h"
using namespace std;
using namespace cv;
int main(int argc,char** argv)
{  // FILE *pf;
//    pf=fopen("bgr.txt","wb");
        Mat img=imread(argv[1]);
        vector<Mat> channels;  
        split(img,channels);  
        Mat B = channels.at(0);          //从vector中读数据用vector::at()  
        Mat G = channels.at(1);  
        Mat R = channels.at(2);
        cout<<"RGB="<<endl<<img<<endl;  
        cout<<"B="<<endl<<B<<endl;  
        cout<<"G="<<endl<<G<<endl;  
        cout<<"R="<<endl<<R<<endl;
        
         ofstream outfile("rgb.txt");
        Mat_<Vec3b>::iterator it = img.begin<Vec3b>();  
        Mat_<Vec3b>::iterator itend = img.end<Vec3b>();      
    while (it != itend)  
    {  
               outfile<<(int)(*it)[0]<<" "<<(int)(*it)[1]<<" "<<(int)(*it)[2]<<endl;  
        //do something with (*it)[0] (*it)[1] (*it)[2]  
                  cout<<(int)(*it)[0]<<" "<<(int)(*it)[1]<<" "<<(int)(*it)[2]<<endl;
                 it++;  
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/beizhengren/article/details/78846163