头文件如下:
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include “opencv2/core.hpp”
#include “opencv2/imgproc.hpp”
#include “opencv2/video.hpp”
#include “opencv2/videoio.hpp”
#include “opencv2/highgui.hpp”
#include “opencv2/imgproc/imgproc_c.h”
#include
#include
#include
可能用不了那么多,但是多多益善。
主程序如下:
using namespace std;
using namespace cv;
int main(int argc, char** argv)
{
ofstream ofile003;
string txt003 = "E:\\xunleixx11\\pictures\\bgr.txt";
ofile003.open(txt003);
string filename001 = "E:\\xunleixx11\\pictures\\cio07.jpg";
Mat img = imread(filename001, -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);
Mat_<Vec3b>::iterator it = img.begin<Vec3b>();
Mat_<Vec3b>::iterator itend = img.end<Vec3b>();
while (it != itend)
{
ofile003 << (int)(*it)[0] << " " << (int)(*it)[1] << " " << (int)(*it)[2] << endl;
it++;
}
ofile003.close();
system("pause");
return 0;
}
效果如图:
改进版:对号入座之后的程序。
全套代码整理如下:
//2021.5.25-Mr--haibao007
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/video.hpp"
#include "opencv2/videoio.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include <iostream>
#include <fstream>
#include<iomanip>
using namespace std;
using namespace cv;
int main(int argc, char** argv)
{
ofstream ofile003;
string txt003 = "E:\\xunleixx11\\pictures\\bgr.txt";
ofile003.open(txt003);
ofile003 << "图像的每一像素的RGB值:" << endl;
ofile003 << "R\tG\tB" << endl;
string filename001 = "E:\\xunleixx11\\pictures\\cio07.jpg";
Mat img = imread(filename001, -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);
Mat_<Vec3b>::iterator it = img.begin<Vec3b>();
Mat_<Vec3b>::iterator itend = img.end<Vec3b>();
while (it != itend)
{
int BB = (int)(*it)[0];
int GG = (int)(*it)[1];
int RR = (int)(*it)[2];
ofile003 << RR<< "\t" << GG<< "\t" << BB<<"\t"<< endl;
it++;
}
ofile003.close();
system("pause");
return 0;
}