Opencv读取图片像素值并保存为txt文件
Posted 西北逍遥
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Opencv读取图片像素值并保存为txt文件相关的知识,希望对你有一定的参考价值。
#include <opencv2/opencv.hpp>
#include<vector>
#include <fstream>
using namespace std;
using namespace cv;
int main(int argc, char* argv[])
{
const char* imagename = "2.jpg";
//从文件中读入图像
Mat img = imread(imagename);
ofstream outfile("rgb.txt");
//如果读入图像失败
if (img.empty())
{
fprintf(stderr, "Can not load image %s\n", imagename);
return -1;
}
int i, j;
int cPointR, cPointG, cPointB, cPoint;//currentPoint;
for (i = 1; i < img.rows; i++)
{
for (j = 1; j<img.cols; j++)
{
cPointB = img.at<Vec3b>(i, j)[0];
cPointG = img.at<Vec3b>(i, j)[1];
cPointR = img.at<Vec3b>(i, j)[2];
cout << "R:"<<cPointR<<" G:"<<cPointG <<" B:"<<cPointB<< endl;
outfile << i<<"," << j << "," <<cPointR << "," << cPointG << "," << cPointB << " ";
if (cPointB>100 & cPointR<100 & cPointG<100)
{
img.at<Vec3b>(i, j)[0] = 0; //单通道是uchar,没有[0][1][2]
img.at<Vec3b>(i, j)[1] = 0;
img.at<Vec3b>(i, j)[2] = 0;
}
}
outfile << endl;
}
//显示图像
imshow("image", img);
//此函数等待按键,按键盘任意键就返回
waitKey();
return 0;
}
以上是关于Opencv读取图片像素值并保存为txt文件的主要内容,如果未能解决你的问题,请参考以下文章
将 .txt 文件读取到地图中,然后将其再次保存为 .txt?