1 彩色图像
输入:一副彩色图像rgb.bmp
输出:D盘下的三个txt文件,分别为r.txt、g.txt、b.txt
源码:
/*===============================================//
功能:RGB读取并保存
时间:02/23/2012 SkySeraph
//===============================================*/
#include "iostream"
#include <fstream>
using namespace std;
#include "cv.h"
#include "highgui.h"
#pragma comment(lib,"highgui.lib")
#pragma comment(lib,"cv.lib")
#pragma comment(lib,"cvaux.lib")
#pragma comment(lib,"cxcore.lib")
int main(int argc, char* argv[])
{
/* ①
IplImage*img = cvLoadImage("rgb.bmp",-1);
if(img==NULL)
return 0;
CvScalar p;
ofstream outfile("d:\\rgb.txt");
outfile<<"图像宽和高:"<<img->width<<"*"<<img->height<<endl;
for(int i=0;i<img->height;i++)
{
for(int j=0;j<img->width;j++)
{
p = cvGet2D(img,i,j);
outfile<<p.val[0] <<" "<<p.val[1]<<" "<<p.val[2] <<" "<<endl;
}
}
*/
///* ②
IplImage* img = cvLoadImage("rgb.bmp",-1);
CvScalar p;
ofstream outfile1("d:\\b.txt");
ofstream outfile2("d:\\g.txt");
ofstream outfile3("d:\\r.txt");
outfile1<<"图像宽和高:"<<img->width<<"*"<<img->height<<endl;
outfile1<<"图像B值"<<endl;
outfile2<<"图像宽和高:"<<img->width<<"*"<<img->height<<endl;
outfile2<<"图像G值"<<endl;
outfile3<<"图像宽和高:"<<img->width<<"*"<<img->height<<endl;
outfile3<<"图像R值"<<endl;
//cvFlip(img);
for(int i=0;i<img->width;i++)
{
for(int j=0;j<img->height;j++)
{
p = cvGet2D(img,i,j);//(j,i)
outfile1<<p.val[0]<<" ";
outfile2<<p.val[1]<<" ";
outfile3<<p.val[2]<<" ";
}
outfile1<<endl;
outfile2<<endl;
outfile3<<endl;
} //*/
return 0;
}
2 灰度图像
输入:一副灰度图像gray.jpg
输出:gray.txt
源码:
/*===============================================//
功能:Gray读取并保存
时间:02/23/2012 SkySeraph
//===============================================*/
#include "iostream"
#include <fstream>
using namespace std;
#include "cv.h"
#include "highgui.h"
#pragma comment(lib,"highgui.lib")
#pragma comment(lib,"cv.lib")
#pragma comment(lib,"cvaux.lib")
#pragma comment(lib,"cxcore.lib")
int main(int argc, char* argv[])
{
IplImage* img = cvLoadImage("gray.jpg",0);
CvScalar p;
ofstream outfile1("d:\\gray.txt");
outfile1<<"图像宽和高:"<<img->width<<"*"<<img->height<<endl;
outfile1<<"图像像素值"<<endl;
//cvFlip(img);
for(int i=0;i<img->width;i++)
{
for(int j=0;j<img->height;j++)
{
p = cvGet2D(img,i,j);//(j,i)
outfile1<<p.val[0]<<" ";
}
outfile1<<endl;
} //*/
return 0;
}
转载链接:http://www.cnblogs.com/skyseraph/archive/2012/02/23/2365542.html
3. 读取彩色图像像素HSV值:
//读取HSV图像的每一个像素的H,S,V 分量
//定义文件名
char *image_H;
char *image_S;
char *image_V;
char path1[50] = "d:\\img\\";
char path2[50] = "d:\\img\\";
char path3[50] = "d:\\img\\";
strcat(path1,image_title); //image_title 是之前定义的图像的名称
image_H = strcat(path1,"_H.txt");
strcat(path2,image_title);
image_S = strcat(path2,"_S.txt");
strcat(path3,image_title);
image_V = strcat(path3,"_V.txt");
ofstream outfile1(image_H);
ofstream outfile2(image_S);
ofstream outfile3(image_V);
for(int i=0;i<floathsv->width;i++)
{
for(int j=0;j<floathsv->height;j++)
{
outfile1<<double(cvGet2D(floathsv,i,j).val[0]/360*255)<<" "; //H 原本的取值范围是0~360
outfile2<<double(cvGet2D(floathsv,i,j).val[1]*255)<<" "; //S 原本的取值范围是0~1
outfile3<<double(cvGet2D(floathsv,i,j).val[2]*255)<<" "; //V 原本的取值范围是0~1
}
outfile1<<endl;
outfile2<<endl;
outfile3<<endl;
}