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

作者:skyseraph
出处:http://www.cnblogs.com/skyseraph/
Email/GTalk: [email protected] 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 
 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<<"图像宽和高:"<width<<"*"<height<26     for(int i=0;iheight;i++)
27     {
28         for(int j=0;jwidth;j++)
29         {
30             p = cvGet2D(img,i,j);
31             outfile<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<<"图像宽和高:"<width<<"*"<height<42     outfile1<<"图像B值"<43     outfile2<<"图像宽和高:"<width<<"*"<height<44     outfile2<<"图像G值"<45     outfile3<<"图像宽和高:"<width<<"*"<height<46     outfile3<<"图像R值"<47     //cvFlip(img);
48     for(int i=0;iwidth;i++)
49     {
50         for(int j=0;jheight;j++)
51         {
52             p = cvGet2D(img,i,j);//(j,i)
53             outfile1<0]<<" ";
54             outfile2<1]<<" ";
55             outfile3<2]<<" ";
56         }
57         outfile1<58         outfile2<59         outfile3<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 
 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<<"图像宽和高:"<width<<"*"<height<24     outfile1<<"图像像素值"<25     //cvFlip(img);
26     for(int i=0;iwidth;i++)
27     {
28         for(int j=0;jheight;j++)
29         {
30             p = cvGet2D(img,i,j);//(j,i)
31             outfile1<0]<<" ";
32         }
33         outfile1<34     }    //*/
35 
36     return 0;
37 }
复制代码

你可能感兴趣的:(OpenCV应用笔记】(彩色/灰度)图像像素值读取并保存到txt文件)