OpenCV实战(一)


前言

学习于毛星云大佬的文章,愿他在那边实现他的理想!

https://qianmo.blog.csdn.net/article/details/20537737


一、OpenCV读取图片并显示

所需头文件

#include<iostream>
#include<highgui.hpp>//高级图形用户界面模块,比如图形交互界面,图像/视频文件的IO等。
#include<core.hpp>//核心模块,定义了多种数据结构(比如矩阵等)包括了重要的Mat模块。核心模块,定义了多种数据结构(比如矩阵等)包括了重要的Mat模块。

using namespace std;
using namespace cv;//给cv命名,后续可以直接使用 替代 cv::

对于简单的程序以上代码足矣。

载入图片并加以显示

载入图片有两种方式:
方法一:载入图片路径

	string path = "D:\\1.png";
	Mat girl = imread(path);

方法二:

	Mat girl = imread("D:\\1.png"32); //载入图像到Mat,图像变为原来的1/4,并显示为灰色
	namedWindow("【1】素材图"); //创建一个名为 "【1】素材图"的窗口 
	imshow("【1】素材图", girl);//显示名为 "【1】素材图"的窗口 

注意填写图片路径时要**“\”双斜杠**
Mat的解释:这些是形成矩阵的各种构造函数。如自动定位中所述,通常默认构造函数就足够了,适当的矩阵将由OpenCV函数分配。构造的矩阵可以进一步分配给另一个矩阵或矩阵表达式,或者可以与Mat::create一起分配。在前一种情况下,旧内容被取消引用。

imread函数定义

CV_EXPORTS_W Mat imread( const String& filename, int flags = IMREAD_COLOR );

flags不同数值对应不同方式显示

ImreadModes {
       IMREAD_UNCHANGED            = -1, //!< If set, return the loaded image as is (with alpha channel, otherwise it gets cropped). Ignore EXIF orientation.
       IMREAD_GRAYSCALE            = 0,  //!< If set, always convert image to the single channel grayscale image (codec internal conversion).
       IMREAD_COLOR                = 1,  //!< If set, always convert image to the 3 channel BGR color image.
       IMREAD_ANYDEPTH             = 2,  //!< If set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit.
       IMREAD_ANYCOLOR             = 4,  //!< If set, the image is read in any possible color format.
       IMREAD_LOAD_GDAL            = 8,  //!< If set, use the gdal driver for loading the image.
       IMREAD_REDUCED_GRAYSCALE_2  = 16, //!< If set, always convert image to the single channel grayscale image and the image size reduced 1/2.
       IMREAD_REDUCED_COLOR_2      = 17, //!< If set, always convert image to the 3 channel BGR color image and the image size reduced 1/2.
       IMREAD_REDUCED_GRAYSCALE_4  = 32, //!< If set, always convert image to the single channel grayscale image and the image size reduced 1/4.
       IMREAD_REDUCED_COLOR_4      = 33, //!< If set, always convert image to the 3 channel BGR color image and the image size reduced 1/4.
       IMREAD_REDUCED_GRAYSCALE_8  = 64, //!< If set, always convert image to the single channel grayscale image and the image size reduced 1/8.
       IMREAD_REDUCED_COLOR_8      = 65, //!< If set, always convert image to the 3 channel BGR color image and the image size reduced 1/8.
       IMREAD_IGNORE_ORIENTATION   = 128 //!< If set, do not rotate the image according to EXIF's orientation flag.
     };

IMREAD_UNCHANGED=-1,/!<如果设置,则按原样返回加载的图像(使用alpha通道,否则会被裁剪)。忽略EXIF方向。
IMREAD_GRAYSCALE=0,/!<如果设置,则始终将图像转换为单通道灰度图像(编解码器内部转换)。
IMREAD_COLOR=1,/!<如果设置,则始终将图像转换为3通道BGR彩色图像。
IMREAD_ANYDEPTH=2,/!<如果设置,则在输入具有相应深度时返回16位/32位图像,否则将其转换为8位。
IMREAD_ANYCOLOR=4,/!<如果设置,则以任何可能的颜色格式读取图像。
IMREAD_LOAD_GDAL=8,/!<如果设置,则使用gdal驱动程序加载图像。
IMREAD_REDUCED_GRAYSCALE_2=16,/!<如果设置,则始终将图像转换为单通道灰度图像,图像大小减小1/2。
IMREAD_REDUCED_COLOR_2=17,/!<如果设置,则始终将图像转换为3通道BGR彩色图像,并且图像大小减小1/2。
IMREAD_REDUCED_GRAYSCALE_4=32,/!<如果设置,则始终将图像转换为单通道灰度图像,图像大小减小1/4。
IMREAD_REDUCED_COLOR_4=33,/!<如果设置,则始终将图像转换为3通道BGR彩色图像,并且图像大小减小1/4。
IMREAD_REDUCED_GRAYSCALE_8=64,/!<如果设置,则始终将图像转换为单通道灰度图像,图像大小减小1/8。
IMREAD_REDUCED_COLOR_8=65,/!<如果设置,则始终将图像转换为3通道BGR彩色图像,并且图像大小减小1/8。
IMREAD_IGNORE_ORIENTATION=128/!<如果设置,则不要根据EXIF的方向标志旋转图像。

二、实现图像重叠

实例代码

	//载入图片
	Mat image = imread("D:\\1.png", 1);
	Mat logo = imread("D:\\Opencv项目\\素材\\cat.png");

	//载入后先显示
	namedWindow("【2】原画图");
	imshow("【2】原画图", image);

	namedWindow("【3】logo图");
	imshow("【3】logo图", logo);

	//定义一个Mat类型,用于存放图像的ROI
	Mat imageRoi;
	//方法一,利用Rect设置ROI位置
	imageRoi = image(Rect(0, 0, logo.cols, logo.rows));
	//方法二
	//imageROI=image(Range(350,350+logo.rows),Range(800,800+logo.cols));

	//将logo加到原图上
	addWeighted(imageRoi, 0.7, logo, 0.3, -1, imageRoi);//0.3是logo的虚实程度

	//显示结果
	namedWindow("【4】原画+logo图");
	imshow("【4】原画+logo图", image);

写OPENCV的代码时,也要注意Rect后面初始位置的两个参数,如果x+logo.cols比image.cols大,就有可能产生未经处理的异常。

输出图像并保存

//输出一张jpg图片到工程目录下
	imwrite("D:\\2.png", image);

总结

本文主要写了如何读取图片,针对图片载入显示以及图像混合进行了简单讲解。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值