1 /* 2 *environment:opencv2.4.3+Qt5.0.1(mingw47_32) 3 *author:ChrisZZ 4 *来源:OpenCV2 Computer Vision Application programming Cookbook 5 *功能:实现从原有图像复制出新的图像 6 *说明: 7 * img1:原有图像名称 8 * img2:复制出来的图像名称 9 */ 10 11 #include <cv.h> 12 #include <highgui.h> 13 14 using namespace std; 15 using namespace cv; 16 17 int main(){ 18 19 string imgname="c:/testdir/barcode.bmp"; 20 Mat img1 = imread(imgname); 21 22 //看是否能成功载入 23 if (!img1.data) 24 printf("Can't find the picture!:(n"); //不能载入 25 else{ 26 printf("Can find the image!:)n"); //成功载入 27 Mat img2 = Mat(img1); 28 29 namedWindow( "Image", 1 );//创建窗口 30 imshow("Image", img2 );//显示图像 31 32 namedWindow( "Image2", 1 );//创建窗口 33 imshow("Image2", img2 );//显示图像 34 35 waitKey(0); //等待按键 36 37 string newImgName = "C:/testdir/barcode_new.jpg"; 38 imwrite(newImgName, img2); 39 } 40 return 0; 41 }
opencv中文官网上不是已经有了图像的读取复制保存的代码了么?但那是C语言的对应的是opencv1.0的版本的吧。。我想指针这东西还是容易犯错的还需要释放资源什么的C++就不用了 这里使用了imwrite()实现了保存,替代了原有的cvSaveImage() 不过参数什么的我还没有去仔细查看过。。
也可以自己写一个函数传递一个图像 这是shallow copy浅复制
1 /* 2 *environment:opencv2.4.3+Qt5.0.1(mingw47_32) 3 *author:ChrisZZ 4 *来源:OpenCV2 Computer Vision Application programming Cookbook 5 *功能:自定义函数可以返回Mat类型的值作为图片 6 */ 7 8 #include <cv.h> 9 #include <highgui.h> 10 11 using namespace std; 12 using namespace cv; 13 14 Mat myFun(){ 15 Mat ima = imread("C:/testdir/barcode.bmp"); 16 return ima; 17 } 18 19 int main(){ 20 Mat gray = myFun(); 21 string windowName="ChrisZZ's barcode"; 22 imshow("test img", gray); 23 waitKey(0); 24 }