功能:
图片的创建、打开、转存,水平垂直翻转,复制
代码:
/*------------------------------------------------------------------------------------------*\
This file contains material supporting chapter 1 of the cookbook:
Computer Vision Programming using the OpenCV Library.
by Robert Laganiere, Packt Publishing, 2011.
This program is free software; permission is hereby granted to use, copy, modify,
and distribute this source code, or portions thereof, for any purpose, without fee,
subject to the restriction that the copyright notice may not be removed
or altered from any source or altered source distribution.
The software is released on an as-is basis and without any warranties of any kind.
In particular, the software is not guaranteed to be fault-tolerant or free from failure.
The author disclaims all warranties with regard to this software, any use,
and any consequent failure, is purely the responsibility of the user.
Copyright (C) 2010-2011 Robert Laganiere, www.laganiere.name
\*------------------------------------------------------------------------------------------*/
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
// function that creates and returns an image
cv::Mat function() {
// create image 并初始化图片大小,格式,类型 CV_8U 表示像素1-byte pixel images
cv::Mat ima(240,320,CV_8U,cv::Scalar(100));
// return it
return ima;
}
int main() {
// create an image of size 0 by 0
cv::Mat image;
// print image size ,the result is size: 0,0
std::cout << "size: " << image.size().height << " , "
<< image.size().width << std::endl;
// open image depends on<highgui.hpp>
image= cv::imread("img.jpg");
// check if image has been successfully read , It is simply set to 0 when no image has been read
if (!image.data) {
// no image has been created?
return 0;
}
// print image size that likes size (after reading):319,480
std::cout << "size (after reading): " << image.size().height << " , "
<< image.size().width << std::endl;
// display image
cv::namedWindow("Original Image"); // define the window
cv::imshow("Original Image", image); // show the image
// create another image
cv::Mat result;
// flip the image 水平翻转
cv::flip(image,result,1); // positive for horizontal
// 0 for vertical,
// negative for both
// display result
cv::namedWindow("Output Image");
cv::imshow("Output Image", result);
// wait for key pressed
cv::waitKey(0); //关闭图示窗口后程序继续运行
// write image on file 保存翻转后的图片
cv::imwrite("output.bmp", result);
// create two new images
cv::Mat image2, image3;
image2= result; // the two images refer to the same data
result.copyTo(image3); // a new copy is created
// flip vertically this time
cv::flip(result,result,0); //上下翻转
// display result
cv::namedWindow("image 2");
cv::imshow("image 2", image2);
cv::namedWindow("image 3");
cv::imshow("image 3", image3);
// get a gray-level image
cv::Mat gray= function();
// display result
cv::namedWindow("Gray Image");
cv::imshow("Gray Image", gray);
// wait for key pressed
cv::waitKey(0);
return 1;
}