首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 平面设计 > 图形图像 >

opencv写XML后在读取发生图片变亮的有关问题

2012-03-26 
opencv写XML后在读取发生图片变亮的问题#include stdafx.h#include cv.h#include highgui.hint _tma

opencv写XML后在读取发生图片变亮的问题
#include "stdafx.h"
#include "cv.h"
#include "highgui.h"


int _tmain(int argc, _TCHAR* argv[])
{
IplImage *img = cvLoadImage("p2.bmp");

//write
CvFileStorage* fs = cvOpenFileStorage("testXML.xml",0,CV_STORAGE_WRITE);
cvStartWriteStruct(fs,"test",CV_NODE_MAP,NULL,cvAttrList(0,0));
cvWriteInt(fs,"t1",100);
cvWriteInt(fs,"t2",200);
cvEndWriteStruct(fs);
//img->widthStep = img->width;
CvMat* mat = cvCreateMat(img->height,img->width,CV_8SC3);
cvConvert(img,mat);
cvWrite(fs,"mat",mat,cvAttrList(0,0));
//cvEndWriteStruct(fs);
cvReleaseFileStorage(&fs);

//read
CvFileStorage* fs1 = cvOpenFileStorage("testXML.xml",0,CV_STORAGE_READ);
CvFileNode* fn = cvGetFileNodeByName(fs1,0,"test");
CvFileNode* nmat = cvGetFileNodeByName(fs1,0,"mat");
CvMat *mat1 = cvCreateMat(img->height,img->width,CV_8SC3);
//mat1 = (CvMat*)cvRead(fs1,nmat);
mat1 = (CvMat*)cvReadByName(fs1,NULL,"mat",NULL);

IplImage* img1 = cvCreateImage(cvSize(mat1->cols,mat1->rows),8,3);
cvGetImage(mat1,img1);
cvReleaseFileStorage(&fs1);
cvNamedWindow("test");
cvShowImage("test",img1);
cvWaitKey();
//cvReleaseFileStorage(&fs1);
cvReleaseMat(&mat);
cvReleaseImage(&img);
return 0;
}

代码如上
显示的结果比原图亮了不少
考虑可能是因为写入和读出的时候ASCII码转换的原因
不知道哪位前辈有好的解决办法

[解决办法]
把cvConvert(img,mat);改成mat = cvGetMat(img, mat);试一下

热点排行