OpenCV读取RGB图像像素值,并保存到txt中

首先是在某论坛上一位老师写的一个程序,这个不是用OpenCV写的,也很棒,放在这里,免得日后忘记。

这是用了微软提供的GDI写的,其实跟OpenCV也很像。如果没有安装OpenCV的话,是很好的选择。

#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
#include <gdiplus.h><span style="white-space:pre">	</span>//微软提供的图形设备接口图形模块
#pragma comment(lib, "gdiplus.lib")


using namespace std;
using namespace Gdiplus;


int main() {
<span style="white-space:pre">	</span>GdiplusStartupInput gdiplusstartupinput;
<span style="white-space:pre">	</span>ULONG_PTR gdiplustoken;
<span style="white-space:pre">	</span>GdiplusStartup(&gdiplustoken, &gdiplusstartupinput, NULL);
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>//修改需要处理的图像名称
<span style="white-space:pre">	</span>wstring infilename(L"20051020_57967_0100_PP.tif");
<span style="white-space:pre">	</span>//输出RGB像素值到color.txt文档中
<span style="white-space:pre">	</span>string outfilename("color.txt");


<span style="white-space:pre">	</span>Bitmap* bmp = new Bitmap(infilename.c_str());
<span style="white-space:pre">	</span>UINT height = bmp->GetHeight();
<span style="white-space:pre">	</span>UINT width = bmp->GetWidth();
<span style="white-space:pre">	</span>cout << "width " << width << ", height " << height << endl;


<span style="white-space:pre">	</span>Color color;
<span style="white-space:pre">	</span>ofstream fout(outfilename.c_str());


<span style="white-space:pre">	</span>for (UINT y = 0; y < height; y++)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>for (UINT x = 0; x < width; x++) {
<span style="white-space:pre">			</span>bmp->GetPixel(x, y, &color);
<span style="white-space:pre">			</span>//按R、G、B形式存储
<span style="white-space:pre">			</span>fout << (int)color.GetRed() << ","
<span style="white-space:pre">				</span><< (int)color.GetGreen() << ","
<span style="white-space:pre">				</span><< (int)color.GetBlue()<<"   ";
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>fout << endl; 
<span style="white-space:pre">		</span>//每行像素值之间加了一个空行
<span style="white-space:pre">		</span>fout << endl;
<span style="white-space:pre">	</span>}


<span style="white-space:pre">	</span>fout.close();


<span style="white-space:pre">	</span>delete bmp;
<span style="white-space:pre">	</span>GdiplusShutdown(gdiplustoken);
<span style="white-space:pre">	</span>return 0;
}
下面是OpenCV的形式,主要注意两点,一是OpenCV保存顺序为BGR,注意。第二是cvGet2D的使用,cvGet2D(img,idx0,idx1);其中idx0代表纵坐标,和图像中的height相对应;idx1代表横坐标,和图像中的width相对应。

#include "iostream"
#include <fstream>
#include <opencv\cv.h>
#include <opencv2\highgui\highgui.hpp>
using namespace std;

int main(int argc, char* argv[])
{
	IplImage* img = cvLoadImage("Image_1.bmp", -1);
	if (img == NULL)
		return 0;
	CvScalar p;
	ofstream outfile("rgb.txt");
	cout << "图像的宽和高分别是:" << img->width << "和" << img->height << endl;
	for (int i = 0; i < img->height; i++)
	{

		for (int j = 0; j < img->width; i++)
		{
			p = cvGet2D(img, i, j);
			//注意OpenCV中保存形式为BGR,为方便阅读,在txt中保存为RGB形式
			outfile << p.val[1] << "," << p.val[0] << "," << p.val[2] << " ";
		}
		outfile << endl;
		//outfile << endl;
	}
	return 0;
}



相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页