先可以读读这篇文章 使用 OpenCV 识别 QRCode,介绍了轮廓的概念,很好 http://ju.outofmemory.cn/entry/255695
import cv2
img = cv2.imread(
'frame.png')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret, binary = cv2.threshold(gray,
127,
255,cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(binary,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
img2 = img
cv2.drawContours(img2,contours,
10,(
0,
0,
255),
3)
from matplotlib
import pyplot
as plt
plt.figure()
plt.imshow(img2)
plt.show()
img1 = img
cv2.drawContours(img1,contours,-
1,(
0,
0,
255),
3)
from matplotlib
import pyplot
as plt
plt.figure()
plt.imshow(img1)
plt.show()
ls
hierarchy.shape
(1, 317, 4)
(1, 317, 4)代表317个轮廓,每个轮廓四个属性
原来contours里面保存的是轮廓里面的点,CHAIN_APPROX_SIMPLE代表只保存角点
len(contours)
317
contours[
0],type(contours)
(array([[[525, 478]]], dtype=int32), list)