Getting Started with Images
-
Warning
This tutorial can contain obsolete information.
警告:该教程可能包含已淘汰的信息。
Goal
In this tutorial you will learn how to:
- Read an image from file (using cv::imread)
- Display an image in an OpenCV window (using cv::imshow)
- Write an image to a file (using cv::imwrite)
在这个教程你将学习:
- 从文件读一张图片(用cv::imread )
- 在OpenCV窗口展示一张图片(用 cv::imshow)
- 把一张图片写入文件(用 cv::imwrite)
Source Code
code at glance代码一览表
import cv2 as cv
import sys
img = cv.imread(cv.sample.findFile("starry_night.jpg"))
if img is None:
sys.exit("Could not read the image.")
cv.imshow("Display window",img)
k=cv.waitKey(0)
if k ==ord("s"):
cv.imwrite("starry_night.png",img)
Explanation
As a first step, the OpenCV python library is imported. The proper way to do this is to additionally assign it the name cv, which is used in the following to reference the library.
第一步,导入库。适宜的方式是将cv2命名为cv,如下所做:
import cv2 as cv
import sys
Now, let’s analyze the main code. As a first step, we read the image “starry_night.jpg” from the OpenCV samples. In order to do so, a call to the cv::imread function loads the image using the file path specified by the first argument. The second argument is optional and specifies the format in which we want the image. This may be:
现在,我们开始分析主要代码。第一步,我们从OpenCV的例子中读取图片 “starry_night.jpg”。要想达成这个,用 cv::imread第一个参数是文件路径第二个参数可选,明确想要的图片格式。可能有:
- IMREAD_COLOR loads the image in the BGR 8-bit format. This is the default that is used here.
- IMREAD_COLOR加载BGR 8-bit格式的图片。这是在这里使用默认的。
- IMREAD_UNCHANGED loads the image as is (including the alpha channel if present)
- IMREAD_UNCHANGED 加载原图(包含alpha通道)
- IMREAD_GRAYSCALE loads the image as an intensity one
- IMREAD_GRAYSCALE 以灰度模式加载图片
After reading in the image data will be stored in a cv::Mat object.
读取的图片数据将存在 cv::Mat 中。
img = cv.imread(cv.samples.findFile(“starry_night.jpg”))
-
Note
OpenCV offers support for the image formats Windows bitmap (bmp), portable image formats (pbm, pgm, ppm) and Sun raster (sr, ras). With help of plugins (you need to specify to use them if you build yourself the library, nevertheless in the packages we ship present by default) you may also load image formats like JPEG (jpeg, jpg, jpe), JPEG 2000 (jp2 - codenamed in the CMake as Jasper), TIFF files (tiff, tif) and portable network graphics (png). Furthermore, OpenEXR is also a possibility.
OpenCV支持bmp,pbm,pgm,ppm,sr,ras图片格式。在插件的帮助下,也可以加载JPEG,JPEG2000,TIFF,PNG。另外,OpenEXR也有可能。
Afterwards, a check is executed, if the image was loaded correctly.
然后,一个检查执行,如果图片加载正确。
if img is None:
sys.exit(“Could not read the image.”)
Then, the image is shown using a call to the cv::imshow function. The first argument is the title of the window and the second argument is the cv::Mat object that will be shown.
然后,用imshow功能来展示图片。第一个参数是窗口的名字,第二个是展示的物体。
Because we want our window to be displayed until the user presses a key (otherwise the program would end far too quickly), we use the cv::waitKey function whose only parameter is just how long should it wait for a user input (measured in milliseconds). Zero means to wait forever. The return value is the key that was pressed.
因为我们想要我们的窗口一直展示,直到用户按下一个键(否则程序将结束得太快),我们用 cv::waitKey 功能,唯一的参数是好长时间等待用户输入(以毫秒来度量)。0意味着永远等待。返回值是按下的健。
cv.imshow(“Display window”, img)
k = cv.waitKey(0)
In the end, the image is written to a file if the pressed key was the “s”-key. For this the cv::imwrite function is called that has the file path and the cv::Mat object as an argument.
最后,如果按键是s,图片会写进文件。就cv::imwrite功能而言,第一个参数是文件路径,第二个参数是物体。
if k == ord(“1”):
cv.imwrite(“starry_night.png”, img)