Python OpenCV introductory tutorial is waiting for you

Although python is very powerful and has its own image processing library PIL, compared to OpenCV, OpenCV is more powerful and can do more and more complex applications, such as face recognition. Like many open source software, OpenCV also provides a complete python interface, which is very easy to call. It contains more than 2500 algorithms and functions. Almost any mature algorithm imaginable can be implemented by calling OpenCV functions, which is very practical.

The full name of OpenCV is: Open Source Computer Vision Library. OpenCV is a cross-platform computer vision library based on the BSD license (open source) that can run on Linux, Windows and Mac OS operating systems. It is lightweight and efficient—consisting of a series of C functions and a small number of C++ classes, it also provides interfaces to languages ​​such as Python, Ruby, and MATLAB, and implements many common algorithms in image processing and computer vision.

1. Image reading: cv2.imread()

Use the function cv2.imread() to read in the image. The image should be in the working path of the program, or provide the full path to the function. The second parameter is to tell the function how to read the image.

• cv2.IMREAD_COLOR: read in a color image. The transparency of the image will be ignored, 

This is the default parameter. 

• cv2.IMREAD_GRAYSCALE: read the image in grayscale mode

#!/bin/python
# -*- coding: utf-8 -*-
import cv2
#import numpy as np
# 彩色图模式加载一副彩图
img = cv2.imread('pythontab.jpg',cv2.IMREAD_COLOR)

作者:哔哔one
链接:https://www.imooc.com/article/50877
来源:慕课网

Note: If you call opencv, even if the path of the image is wrong, OpenCV will not report an error or a warning. So how do we judge that the image we loaded is correct? It's very simple, just need to judge that the result of print img is None, it means the loading is wrong, otherwise the loading is correct.

2. Display the image cv2.imshow()

Use the function cv2.imshow() to display the image. The window will automatically adjust to the image size. The first parameter is the name of the window, and the second is our image. You can create as many windows as you like, but you must give them different names.

cv2.imshow('image',img) #注意参数顺序
cv2.waitKey(0) 
"""
cv2.waitKey() 是一个键盘绑定函数。需要指出的是它的时间尺度是毫
秒级。函数等待特定的几毫秒,看是否有键盘输入。特定的几毫秒之内,如果
按下任意键,这个函数会返回按键的ASCII 码值,程序将会继续运行。如果没
有键盘输入,返回值为-1,如果我们设置这个函数的参数为0,那它将会无限
期的等待键盘输入。它也可以被用来检测特定键是否被按下,例如按键a 是否
被按下,这个后面我们会接着讨论。
"""
cv2.destroyAllWindows()
"""
cv2.destroyAllWindows() 可以轻易删除任何我们建立的窗口。如果
你想删除特定的窗口可以使用cv2.destroyWindow(),在括号内输入你想删
除的窗口名。
"""
3. Save the image cv2.imwrite()
cv2.imwrite('lena.png',img) #很简单就可以生成一张图片

The above is the main content that the editor will share in this article, I hope it can be helpful to you.

end:

I am a python development engineer, and I have compiled a set of the latest python system learning tutorials, including basic python scripts to web development, crawlers, data analysis, data visualization, machine learning, and interview books. Those who want these materials can pay attention to the editor, add Q skirt 851211580 to pick up Python learning materials and learning videos, and online guidance from the Great God!

Guess you like

Origin blog.csdn.net/pyjishu/article/details/105435054