【OpenCV:从零到一】01:加载、修改、保存图像

前言
这是我《OpenCV:从零到一》专栏的第一篇博客,想看跟多请戳这。
本文概要
本文主要涉及一下几个函数
imread、imwrite (imgcodecs)
namedWindow、imshow、waitKey(highgui)
cvtColor(imgproc)
后面括号里的是头文件名(下面是官方文档里的主要模块)
在这里插入图片描述
不难发现,命名规律:
im=image
proc=process
这样一来头文件的功能就好理解很多了。事不宜迟赶紧来看代码吧

案例代码
大概内容:图像的加载、修改、保存。

#include<opencv2/opencv.hpp>
#include<opencv2/highgui/highgui_c.h>
#include <iostream>

using namespace cv;

int main() {
	//读取图像
	Mat src = imread("D:\\86186\\Documents\\opencv\\lena.jpg");//创造一个Mat对象并且赋值给他
	if (src.empty()) {//Returns true if the array has no elements.
		printf("could nor load image...\n");
		return -1;
	}
	namedWindow("lena", CV_WINDOW_AUTOSIZE);//创造一个窗口
	imshow("lena", src);//把图片展示得到窗口中
	//修改图像
	namedWindow("result", CV_WINDOW_AUTOSIZE);//再创造一个窗口
	Mat result;//创造一个空的图片对象
	cvtColor(src, result, CV_BGR2HLS);//修改颜色模型
	imshow("result", result);//Displays an image in the specified window.
	//保存图像
	imwrite("D:\\86186\\Documents\\opencv\\test_lena1.jpg", result);//Saves an image to a specified file.
	waitKey(6000);//Waits for a pressed key.
	return 0;
}

解析及注意事项

  • 学习过数字图像处理的话我们知道所有图片都会被表示为一个矩阵,而这个矩阵对应在OpenCV中则是Mat对象
  • Mat对象顾名思义Matrix 矩阵、母体(著名的黑客帝国英文原名就是matrix,有没有因为这部电影入行的?反正我不是哈哈啊)
  • Mat有超级多的成员函数,当然大多说成员函数单凭名字大概就知道是什么功能了
  • imread、imwrite是一对函数,一个从电脑读取,一个写入电脑。imshow和他们的形式很像,但是他来自另一个头文件,而且他前面必须要有一个namedWindow创造窗口,后面也必须要跟一个waitKey关闭窗口,这三个可以说是输出三板斧了,那么前面两个姑且可以命名为读写双子星吧(幼稚鬼
  • imread第一个参数是图片在计算机中的名称(url),第二个参数可以改变加载类型(如彩色图片灰色来显示),没有特别需要就不用加了。
  • imread读取的图像类型取决于图像内容而不是拓展名
  • cvtColor用于改变图像颜色空间(比如灰色图像变为红色图像),cvt=convert。第一个参数是原Mat对象,第二个参数是结果Mat对象,第三个参数是颜色空间参数。
  • namedWindow第一个参数是窗口名,不能与其他窗口重复,否则语句无效。第二个参数是窗口类型,CV_WINDOW_AUTOSIZE类型的意思是自动大小,用户不能改变。
  • imshow第一个参数是窗口名,输出到指定的窗口,imshow后面没有waitkey的话不会展示图片(第一次写程序的时候我就尝试使用system(“pause”)来停止程序,后面发现图像没有显示,当时把他归结为玄学…)第二个参数是mat对象
  • waitKey用来监听键盘事件,参数大于0的时候则在delay ms内监听键盘,若无输入则返回-1,若参数值小于等于0则无限等待下去,通常用0。

全注释代码

#include<opencv2/opencv.hpp>
#include<opencv2/highgui/highgui_c.h>
#include <iostream>

using namespace cv;

int main() {
	//mat对象是一个 n-dimensional dense array class 拥有众多成员函数
	Mat src = imread("D:\\86186\\Documents\\opencv\\lena.jpg");//创造一个Mat对象并且赋值给他
	/*
	imread功能是加载图像文件成为一个Mat对象(Loads an image from a file.)
	第一个参数表示图像文件名称
	第二个参数,表示加载的图像是什么类型:
		IMREAD_UNCHANGED (<0) 表示加载原图,不做任何改变
		IMREAD_GRAYSCALE(0)表示把原图作为灰度图像加载进来
		IMREAD_COLOR(> 0) 表示把原图作为RGB图像加载进来
	The function imread loads an image from the specified file and returns it.
	If the image cannot be read (because of missing file, improper permissions, unsupported or invalid format),the function returns an empty matrix ( Mat::data==NULL ).
	如果图像因为 找不到文件\不正确权限\非法格式 则返回一个空矩阵
	The function determines the type of an image by the content, not by the file extension.
	该图像类型取决于内容而不是拓展名
	*/

	if (src.empty()) {//Returns true if the array has no elements.
		printf("could nor load image...\n");
		return -1;
	}
	namedWindow("lena", CV_WINDOW_AUTOSIZE);//创造一个窗口
	//If a window with the same name already exists, the function does nothing.如果窗口名重复了则该语句不会生效
	/*
	You can call cv::destroyWindow or cv::destroyAllWindows to close the window and de-allocate any associated memory usage.
	For a simple program, you do not really have to call these functions
	because all the resources and windows of the application are closed automatically by the operating system upon exit.
	*/
	imshow("lena", src);//把图片展示得到窗口中
	//This function should be followed by cv::waitKey function which displays the image for specified milliseconds.Otherwise, it won't display the image.
	//该函数后面必须要跟一个waitkey函数,否则不会显示图片
	namedWindow("result", CV_WINDOW_AUTOSIZE);//再创造一个窗口
	Mat result;//创造一个空的图片对象
	cvtColor(src, result, CV_BGR2HLS);//修改颜色空间Converts an image from one color space to another.
	//颜色空间命名还是一些规范的,这里展示不展开但可以看出些东西如BGR(RGB在opencv存储顺序) HLS颜色模型.....
	imshow("result", result);//Displays an image in the specified window.
	imwrite("D:\\86186\\Documents\\opencv\\test_lena1.jpg", result);//Saves an image to a specified file.
	//和imread是一对的,使用方法和注意事项差不多

	waitKey(6000);//Waits for a pressed key.
	/*
	waitKey(delay);//delay为int型
	waitKey官方原文:
	The function waitKey waits for a key event infinitely (when delay≤0 ) or for delay milliseconds, when it is positive.
	该函数用来监听键盘事件,如果delay大于0则等待delay ms,否则无限等待下去(通常用0)
	Since the OS has a minimum time between switching threads, the function will not wait exactly delay ms, it will wait at least delay ms,
	因为OS(操作系统)切换线程需要时间,所以函数不是等待准确的delay ms,而是至少等待delay ms
	depending on what else is running on your computer at that time.
	这取决于你当时运行的电脑
	It returns the code of the pressed key or -1 if no key was pressed before the specified time had elapsed.
	函数返回输入键的ASCII码或者-1(如果你没有输入任何键在指定时间过去之前)
	*/
	return 0;
}

翻译笔记
n-dimensional dense array class 多维密集数组类
specified file 指定的文件
improper permissions 不正确限权
file extension 文件后缀名
infinitely 无限的
switching threads 切换线程
before the specified time had elapsed 在指定的时间过去之前

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值