一起学opencv2(一)

功能:

图片的创建、打开、转存,水平垂直翻转,复制

代码:

/*------------------------------------------------------------------------------------------*\
   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;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值