【OpenCV应用笔记】(彩色/灰度)图像像素值读取并保存到txt文件

【OpenCV应用笔记】(彩色/灰度)图像像素值读取并保存到txt文件

   SkySeraph Feb 23rd 2012  SZTCL

Email:zgzhaobo@gmail.com    QQ:452728574

-------------------------------------------------------------------------------------------------------------------------------------------------------------

一、啰嗦:

  一个搞硬件的朋友要测试FPGA和PC机的CCD通讯,需用到图像数据,找到我这个曾经"笑傲"实验室的“图像处理高手”,委托我写一个小测试程序,将一副指定的图像(彩色/灰度)转换程txt文件保存到PC机上,Ta只要exe文件,代码这玩意不需要...,情何以堪...还是动手写了一个,测试通过,ok,见下....o(∩_∩)o 

-------------------------------------------------------------------------------------------------------------------------------------------------------------

二、源码

1 彩色图像

①输入:一副彩色图像rgb.bmp

②输出:D盘下的三个txt文件,分别为r.txt、g.txt、b.txt

③源码:

View Code
 1 /*===============================================//
2 功能:RGB读取并保存
3 时间:02/23/2012 SkySeraph
4 //===============================================*/
5 #include "iostream"
6 #include <fstream>
7 using namespace std;
8
9 #include "cv.h"
10 #include "highgui.h"
11
12 #pragma comment(lib,"highgui.lib")
13 #pragma comment(lib,"cv.lib")
14 #pragma comment(lib,"cvaux.lib")
15 #pragma comment(lib,"cxcore.lib")
16
17 int main(int argc, char* argv[])
18 {
19 /*
20 IplImage*img = cvLoadImage("rgb.bmp",-1);
21 if(img==NULL)
22 return 0;
23 CvScalar p;
24 ofstream outfile("d:\\rgb.txt");
25 outfile<<"图像宽和高:"<<img->width<<"*"<<img->height<<endl;
26 for(int i=0;i<img->height;i++)
27 {
28 for(int j=0;j<img->width;j++)
29 {
30 p = cvGet2D(img,i,j);
31 outfile<<p.val[0] <<" "<<p.val[1]<<" "<<p.val[2] <<" "<<endl;
32 }
33 }
34 */
35 ///* ②
36 IplImage* img = cvLoadImage("rgb.bmp",-1);
37 CvScalar p;
38 ofstream outfile1("d:\\b.txt");
39 ofstream outfile2("d:\\g.txt");
40 ofstream outfile3("d:\\r.txt");
41 outfile1<<"图像宽和高:"<<img->width<<"*"<<img->height<<endl;
42 outfile1<<"图像B值"<<endl;
43 outfile2<<"图像宽和高:"<<img->width<<"*"<<img->height<<endl;
44 outfile2<<"图像G值"<<endl;
45 outfile3<<"图像宽和高:"<<img->width<<"*"<<img->height<<endl;
46 outfile3<<"图像R值"<<endl;
47 //cvFlip(img);
48 for(int i=0;i<img->width;i++)
49 {
50 for(int j=0;j<img->height;j++)
51 {
52 p = cvGet2D(img,i,j);//(j,i)
53 outfile1<<p.val[0]<<" ";
54 outfile2<<p.val[1]<<" ";
55 outfile3<<p.val[2]<<" ";
56 }
57 outfile1<<endl;
58 outfile2<<endl;
59 outfile3<<endl;
60 } //*/
61
62 return 0;
63 }

④exe文件:rgb.zip

2 灰度图像

①输入:一副灰度图像gray.jpg

②输出:gray.txt

③源码:

View Code
 1 /*===============================================//
2 功能:Gray读取并保存
3 时间:02/23/2012 SkySeraph
4 //===============================================*/
5 #include "iostream"
6 #include <fstream>
7 using namespace std;
8
9 #include "cv.h"
10 #include "highgui.h"
11
12 #pragma comment(lib,"highgui.lib")
13 #pragma comment(lib,"cv.lib")
14 #pragma comment(lib,"cvaux.lib")
15 #pragma comment(lib,"cxcore.lib")
16
17
18 int main(int argc, char* argv[])
19 {
20 IplImage* img = cvLoadImage("gray.jpg",0);
21 CvScalar p;
22 ofstream outfile1("d:\\gray.txt");
23 outfile1<<"图像宽和高:"<<img->width<<"*"<<img->height<<endl;
24 outfile1<<"图像像素值"<<endl;
25 //cvFlip(img);
26 for(int i=0;i<img->width;i++)
27 {
28 for(int j=0;j<img->height;j++)
29 {
30 p = cvGet2D(img,i,j);//(j,i)
31 outfile1<<p.val[0]<<" ";
32 }
33 outfile1<<endl;
34 } //*/
35
36 return 0;
37 }

④exe文件:gray.zip

-------------------------------------------------------------------------------------------------------------------------------------------------------------

三、效果

彩色为例,源图像

r.txt(部分):gray.zip



其它类似,不再啰嗦...

-------------------------------------------------------------------------------------------------------------------------------------------------------------

posted @ 2012-02-23 21:59 SkySeraph 阅读(...) 评论(...) 编辑 收藏