赞
踩
1、项目中遇到需要保存16位深度的png图像,用来加速算法减少耗时并且数据从文本文档转换为png占用更小的flash,一举多得;
IplImage *caimage = cvCreateImage(cvSize(w, h), IPL_DEPTH_16U, 3);
cv::Mat src= cvarrToMat(caimage);
cv::Mat dst;
src.convertTo(dst, CV_16UC3);
std::vector<int> compression_params;
compression_params.push_back(cv::IMWRITE_PNG_COMPRESSION);
compression_params.push_back(0);
compression_params.push_back(cv::IMWRITE_PNG_STRATEGY);
compression_params.push_back(cv::IMWRITE_PNG_STRATEGY_DEFAULT);
char name[128] = { 0 };
string ss = "res";
sprintf(name, "result_images\\%s.png", ss.c_str());
imwrite(name, dst, compression_params);
2、涉及到IplImage与Mat的相互转换也一并在此记录一下
a. Mat -->IplImage
Mat mat_img=imread("samples.bmp");
IplImage* ipl_img;
ipl_img = &IplImage(mat_img);
b. IplImage--->Mat
IplImage* ipl_img = cvLoadImage("samples.bmp");;
Mat mat_img;
mat_img = Mat(ipl_img);//Mat(const IplImage* img, bool copyData=false);//opencv2.0
//mat_img = cvarrToMat(ipl_img);//opencv3.0
3、快速get到Mat某个位置的像素值
Mat src= imread("test.jpg");
int x = src.cols-10;
int y = src.rows-20;
Mat p = mat.col(x).row(y);
uchar* ptr = (uchar*) p.data;
int B = ptr[0];
int G = ptr[1];
int R = ptr[2];
cout<<"res:"<<B<<" "<<G<<" "<<R<<" "<<endl;
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。