opencv学习笔记(二):图片的读取与写入

opencv学习笔记(二):图片的读取与写入

核心函数1:cv2.imread()

cv2.imread()
@brief: used to read an image 用来读取一张图片
@arguments: need two arguments 需要两个参数
            @first:an image which should be in the working directory or a full path of image should be given 
            第一个参数为文件夹内图片或者其他文件夹的图片的绝对路径
            @second: is a flag which specifies the way image should be read 
            第二个参数是一个标志,用来表示该张图片以何种方式被读取 

上述根据OpenCV_Python_Tutorials进行翻译得到,简言之,opencv的图像读取函数cv2.imread需要有两个参数,第一个用来链接图片,第二个参数用来决定图片的读取形式
第一个参数:有两种表示:若图片为当前工程文件夹内的一张图片,则可表示为’图片名’,如’1.jpg’;若图片为其他路径,则可表示为’D:/opencv_dataset/BSD68/test002.png’
注意:其中路径内的斜杠一定为/或者\\
第二个参数:在这里插入图片描述

总结:cv2.read的语法格式:

cv2.imread(filename,flag)

注意,flag默认为1,表示按照BGR三通道的方式进行读取,并且它的每个像素点都是以BGR三通道的方式进行存储,与flag取cv2.IMREAD_COLOR效果相同;flag也可取0,表示以灰度图形式进行读取,与flag取cv2.IMREAD_GRAYSCALE是效果相同。

以灰度图形式读取工程文件内图片

下面这段代码的功能是以灰度图的形式读取1.jpg,为了清楚地看到图片已被读取,此处加入图像显示cv2.imshow()

import cv2
import numpy as np
Img = cv2.imread('1.jpg',cv2.IMREAD_GRAYSCALE)
cv2.imshow('img board',Img)
cv2.waitKey(0)

运行效果如下:
在这里插入图片描述

另一种写法:

import cv2
import numpy as np
Img = cv2.imread('1.jpg',0)
cv2.imshow('img board',Img)
cv2.waitKey(0)

运行效果如下:
在这里插入图片描述

以原图形式读取工程文件内图片

下面这段代码的功能是以原图形式读取1.jpg

import cv2
import numpy as np
Img = cv2.imread('1.jpg',cv2.IMREAD_UNCHANGED)
cv2.imshow('img board',Img)
cv2.waitKey(0)

运行效果如下:
在这里插入图片描述

读取其他路径下的图片

一定要注意路径的表示方法!路径内的斜杠一定为/或者\\
以下代码的功能是原图形式读取其他路径下的图片

import cv2
import numpy as np
Img = cv2.imread('D:\\opencv_dataset\\CBSD68\\102061.png',cv2.IMREAD_UNCHANGED)
cv2.imshow('img board',Img)
cv2.waitKey(0)

运行效果如下:
在这里插入图片描述

核心函数2:cv2.imwrite()

cv2.imwrite()
Saves an image to a specified file. 

The function imwrite saves the image to the specified file. The image format is chosen based on the filename extension (see cv::imread for the list of extensions). In general, only 8-bit single-channel or 3-channel (with 'BGR' channel order) images can be saved using this function, with these exceptions:16-bit unsigned (CV_16U) images can be saved in the case of PNG, JPEG 2000, and TIFF formats
•32-bit float (CV_32F) images can be saved in TIFF, OpenEXR, and Radiance HDR formats; 3-channel (CV_32FC3) TIFF images will be saved using the LogLuv high dynamic range encoding (4 bytes per pixel)
•PNG images with an alpha channel can be saved using this function. To do this, create 8-bit (or 16-bit) 4-channel image BGRA, where the alpha channel goes last. Fully transparent pixels should have alpha set to 0, fully opaque pixels should have alpha set to 255/65535 (see the code sample below).

If the format, depth or channel order is different, use Mat::convertTo and cv::cvtColor to convert it before saving. Or, use the universal FileStorage I/O functions to save the image to XML or YAML format.

翻译:
将图像保存到指定文件。
函数imwrite将图像保存到指定文件。根据文件扩展名选择图像格式。通常,使用此功能只能保存8位单通道或3通道(具有“BGR”通道顺序)图像,但以下情况除外:
•16位无符号(CV_16U)图像可以保存为PNG、JPEG 2000和TIFF格式
•32位浮点(CV_32F)图像可以保存为TIFF、OpenEXR和Radiance HDR格式;将使用LogLuv高动态范围编码(每像素4字节)保存3通道(CV_32FC3)TIFF图像
•使用此功能可以保存带有alpha通道的PNG图像。为此,创建8位(或16位)4通道图像BGRA,其中alpha通道最后到达。全透明像素的alpha设置为0,全不透明像素的alfa设置为255/65535(见下面的代码示例)。
如果格式、深度或通道顺序不同,请在保存之前使用Mat::convertTo和cv::cvtColor进行转换。或者,使用通用文件存储I/O函数将图像保存为XML或YAML格式。
函数原型:cv.imwrite( filename, img[, params] ) 
Parameters
@filename: Name of the file.  
@img: Image to be saved.  
@params: Format-specific parameters encoded as pairs (paramId_1, paramValue_1, paramId_2, paramValue_2, ... .) see cv::ImwriteFlags 
参数
@文件名:文件名。
@img:要保存的图像。
@params:格式特定参数编码(paramId_1、paramValue_1,paramId-2、paramValue_2,…)

此处仅强调第三个参数,params表示的是特定的编码格式,主要如下:
在这里插入图片描述

以JPEG为例,第三个参数表示图片的质量,取值为0-100,数字越大质量越高,默认情况为95;
对于png ,第三个参数表示的是压缩级别。默认为3
注意:一般而言第三个参数不用填写,第三个参数涉及到图片压缩质量等,此处不做讨论

保存(写出)图片为灰度图片

先以灰度形式读取原图,再保存

import cv2
Img = cv2.imread('1.jpg',cv2.IMREAD_GRAYSCALE)
cv2.imwrite('1gray.jpg',Img)

运行后,打开工程文件即可看到新增1gray.jpg文件
在这里插入图片描述

核心函数3: cv2.imshow()

Displays an image in the specified window. 

The function imshow displays an image in the specified window. If the window was created with the cv::WINDOW_AUTOSIZE flag, the image is shown with its original size, however it is still limited by the screen resolution. Otherwise, the image is scaled to fit the window. The function may scale the image, depending on its depth:
•If the image is 8-bit unsigned, it is displayed as is.
•If the image is 16-bit unsigned or 32-bit integer, the pixels are divided by 256. That is, the value range [0,255*256] is mapped to [0,255].
•If the image is 32-bit or 64-bit floating-point, the pixel values are multiplied by 255. That is, the value range [0,1] is mapped to [0,255].


翻译:在指定窗口中显示图象
该函数功能是在指定窗口中显示图像,如果使用cv2.WINDOW_AUTOSIZE标志进行显示窗口的创建,那么图像就会以其本身的大小进行显示,但是仍然受屏幕分辨率的限制。否则,图像将会被缩放以适应窗口,该函数可根据图像本身的深度缩放图像。
•如果图像为8位无符号,则按原样显示。

•如果图像是16位无符号或32位整数,则像素除以256。即,值范围[0,255*256]映射到[0,255]。

•如果图像是32位或64位浮点,则像素值乘以255。即,值范围[0,1]映射到[0,255]

三个特别注意点

If the window was not created before this function, it is assumed creating a window with cv::WINDOW_AUTOSIZE.

If you need to show an image that is bigger than the screen resolution, you will need to call namedWindow("", WINDOW_NORMAL) before the imshow

This function should be followed by cv::waitKey function which displays the image for specified milliseconds. Otherwise, it won't display the image. For example, waitKey(0) will display the window infinitely until any keypress (it is suitable for image display). waitKey(25) will display a frame for 25 ms, after which display will be automatically closed. (If you put it in a loop to read videos, it will display the video frame-by-frame)

1.如果在调用该函数前并未创建窗口,默认以WINDOW_AUTOSIZE创建
2.如果需要显示大于屏幕分辨率的图像,则需要在imshow之前调用namedWindow(“,WINDOW_NORMAL
3.该函数后面应该紧跟cv::waitKey函数,该函数在指定毫秒内显示图像。否则,它将不显示图像。例如,waitKey(0)将无限显示窗口,直到按下任何键(适用于图像显示)。waitKey(25)将显示一帧25毫秒,之后显示器将自动关闭。(如果您将其放入循环中以读取视频,它将逐帧显示视频)

核心函数4:cv2.waitKey()

Waits for a pressed key. 

The function waitKey waits for a key event infinitely  or for delay milliseconds, when it is positive. 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, 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.
Note
This function is the only method in HighGUI that can fetch and handle events, so it needs to be called periodically for normal event processing unless HighGUI is used within an environment that takes care of event processing.
Note
The function only works if there is at least one HighGUI window created and the window is active. If there are several HighGUI windows, any of them can be active.
Parameters
delay Delay in milliseconds. 0 is the special value that means "forever".  
翻译:等待按下的键。
函数waitKey无限地等待键事件或等待延迟毫秒(当它为正时)。由于操作系统在切换线程之间的时间最短,该函数不会等待延迟毫秒,它将至少等待延迟毫秒(取决于当时计算机上运行的其他内容)。如果在指定时间过去之前没有按键,则返回按键的代码或-1。

此函数是HighGUI中唯一可以获取和处理事件的方法,因此需要定期调用它以进行正常事件处理,除非HighGUI用于处理事件处理的环境中。

该功能仅在至少创建了一个HighGUI窗口且该窗口处于活动状态时才起作用。如果有多个HighGUI窗口,其中任何一个都可以处于活动状态。
参数
延迟以毫秒为单位的延迟。0是表示“永远”的特殊值。
cv2.waitKey(delay)
Parameters
delay: Delay in milliseconds. 0 is the special value that means 
"forever".  
函数原形:cv2.waitKey(delay)
参数:delay的单位为毫秒,取0意味着一直延时。
  • 8
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值