Introduction to the use of Opencv in Python

penCV is a C++ library for dealing with computer vision problems in real time, covering many modules in the field of computer vision. 
OpenCV has two Python interfaces, the old version of the cv module uses OpenCV's built-in data types, and the new version of the cv2 module uses NumPy arrays. For the new version of the module, it can be imported in the following ways:

import cv2
  • 1

The old version of the module is imported in the following way:

import cv2.cv
  • 1
1.1 Configure opencv in Python

Detailed steps to install OpenCV in Python under Windows 
Native Win7, Python3.5,  OpenCV2.4.9
copy D:\OpenCV\opencv\build\python\2.7\x86the cv2.pyd file to the D:\Python\Python35-32\Lib\site-packagesfolder. 
An error occurred: 
write picture description here

Although there is a blog saying: currently opencv only supports python2.6 and python2.7, don't give up, I finally found the following blog:  win7 64-bit python3.4&opencv3.0 configuration and installation tutorial

  1. Find the specified version and download the corresponding opencv: Opencv download URL
  2. Install with pippip install *.whl 
    write picture description here
  3. Test, the installation was successful! 
    write picture description here
1.2 Read and write images

Here's an example that simply loads an image, prints the dimensions, and saves the image:

#!/usr/bin/env python3        
# -*- coding: utf-8 -*-  
import cv2

# 载入图像
im = cv2.imread('./0.png')

# 打印图像尺寸
h,w = im.shape[:2]
print(h,w)

# 保存PNG格式图像为JPEG格式
cv2.imwrite('./0.jpg',im)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
1.3 Color space conversion

In OpenCV, images are not stored in regular RGB color channels, they are in BGR order. When reading an image, the default is BGR, but there are many conversions that can be used. The color space conversion function can be done with cvtColor().

#!/usr/bin/env python3        
# -*- coding: utf-8 -*-  
import cv2

# 载入图像
im = cv2.imread('./2.png')
print(im.shape)

# create a grayscale version
gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
print(gray.shape)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

The result is: 
write picture description here

1.4 Displaying images
#!/usr/bin/env python3        
# -*- coding: utf-8 -*-  
import cv2
# from matplotlib import pyplot as plt
from pylab import *

# 添加中文字体支持
from matplotlib.font_manager import FontProperties
font = FontProperties(fname=r"c:\windows\fonts\SimSun.ttc",size = 14)

# 载入图像
im = cv2.imread('Middlebury_01_clean_color.png')

# 颜色空间转换
gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)

# 显示原始图像
fig = plt.figure()
subplot(121)
plt.gray()
imshow(im)
title(u'彩色图', fontproperties= font)
axis('off')
# 显示灰度化图像
plt.subplot(122)
plt.gray()
imshow(gray)
title(u'灰度图', fontproperties= font)
axis('off')

show()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

Display result: 
write picture description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325999485&siteId=291194637