openCV读取图片文件RGB输出txt源码展示可视化

头文件如下:
#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;
}

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

海宝7号

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值