【OpenCV应用笔记】(彩色/灰度)图像像素值读取并保存到txt文件
SkySeraph Feb 23rd 2012 SZTCL
Email: QQ:452728574
-------------------------------------------------------------------------------------------------------------------------------------------------------------
一、啰嗦:
一个搞硬件的朋友要测试FPGA和PC机的CCD通讯,需用到图像数据,找到我这个曾经"笑傲"实验室的“图像处理高手”,委托我写一个小测试程序,将一副指定的图像(彩色/灰度)转换程txt文件保存到PC机上,Ta只要exe文件,代码这玩意不需要...,情何以堪...还是动手写了一个,测试通过,ok,见下....o(∩_∩)o
-------------------------------------------------------------------------------------------------------------------------------------------------------------
二、源码
1 彩色图像
①输入:一副彩色图像rgb.bmp
②输出:D盘下的三个txt文件,分别为r.txt、g.txt、b.txt
③源码:
1 /*===============================================// 2 功能:RGB读取并保存 3 时间:02/23/2012 SkySeraph 4 //===============================================*/ 5 #include "iostream" 6 #include7 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< height;i++) 27 { 28 for(int j=0;j width;j++) 29 { 30 p = cvGet2D(img,i,j); 31 outfile< <<" "< <<" "< <<" "< width<<"*"< height< width<<"*"< height< width<<"*"< height< width;i++) 49 { 50 for(int j=0;j height;j++) 51 { 52 p = cvGet2D(img,i,j);//(j,i) 53 outfile1< <<" "; 54 outfile2< <<" "; 55 outfile3< <<" "; 56 } 57 outfile1<
④exe文件:
2 灰度图像
①输入:一副灰度图像gray.jpg
②输出:gray.txt
③源码:
1 /*===============================================// 2 功能:Gray读取并保存 3 时间:02/23/2012 SkySeraph 4 //===============================================*/ 5 #include "iostream" 6 #include7 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< width;i++) 27 { 28 for(int j=0;j height;j++) 29 { 30 p = cvGet2D(img,i,j);//(j,i) 31 outfile1< <<" "; 32 } 33 outfile1<
④exe文件:
-------------------------------------------------------------------------------------------------------------------------------------------------------------
三、效果
彩色为例,源图像
r.txt(部分):
其它类似,不再啰嗦...
-------------------------------------------------------------------------------------------------------------------------------------------------------------