视频学习:2020最新-3h精通Opencv
1、读取视频
import cv2
cap = cv2.VideoCapture("Resources/3.mp4")
while True:
succes, img = cap.read()
cv2.imshow("Video", img)
if cv2.waitKey(1) & 0xFF == ord(
'q'): # 总体效果:按下q键后break。cv2.waitkey(1)在有按键按下的时候返回按键的ASCII值,否则返回-1,因为有些系统cv2.waitkey(1)的返回值不只8位
break
2、摄像头的使用
import cv2
cap = cv2.VideoCapture(0) # 设置摄像头ID
cap.set(3, 640) # 设置宽度
cap.set(4, 480) # 设置高度
cap.set(10, 100) # 设置亮度
while True:
succes, img = cap.read()
cv2.imshow("Video", img)
if cv2.waitKey(1) & 0xFF == ord(
'q'): # 总体效果:按下q键后break。cv2.waitKey(1)在有按键按下的时候返回按键的ASCII值,否则返回-1,因为有些系统cv2.waitkey(1)的返回值不只8位
break
3、模糊图像、图像扩张、Canny边缘检测器、图像腐蚀
import cv2
import numpy as np
img = cv2.imread("Resources/lena.png")
kernel = np.ones((5, 5), np.uint8) # 内核
imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 设置为灰度
imgBlur = cv2.GaussianBlur(imgGray, (7, 7), 0) # 模糊图像,内核必须为奇数X奇数
imgCanny = cv2.Canny(img, 100, 100) # 这种边缘检测器成为Canny边缘检测器 100-->>200增加边缘厚度
imgDialation = cv2.dilate(imgCanny, kernel, iterations=1) # 图像扩张(膨胀),必须定义矩阵的大小和值的矩阵
imgEroded = cv2.erode(imgDialation, kernel, iterations=1) # 图像腐蚀
cv2.imshow("Gray Image", imgGray)
cv2.imshow("Blur Image", imgBlur)
cv2.imshow("Canny Image", imgCanny)
cv2.imshow("Dialation Image", imgDialation)
cv2.imshow("Eroded Image", imgEroded)
cv2.waitKey(0)
4、改变大小和剪辑
import cv2
img = cv2.imread("Resources/lena.png")
print(img.shape) # 检查图像形状以便IMG点形
imgResize = cv2.resize(img, (300, 200)) # 先定义宽度,再定义高度
print(imgResize.shape)
imgCropped = img[0:200, 200:500] # 裁剪图像
cv2.imshow("Image", img)
cv2.imshow("Image Resize", imgResize)
cv2.imshow("Image Cropped", imgCropped)
cv2.waitKey(0)
如何在图像上绘制形状
import cv2
import numpy as np
img = np.zeros((512, 512, 3), np.uint8)
# print(img)
# img[:]=255,0,0
cv2.line(img, (0, 0), (img.shape[1], img.shape[0]), (0, 255, 0), 3) # 线(宽度,高度)
cv2.rectangle(img, (0, 0), (250, 350), (0, 0, 255), 2) # 矩形(cv2.FILLED:填充矩形)
cv2.circle(img, (400, 50), 30, (255, 255, 0), 4) # (400,50)是圆心位置,30是半径,4是厚度
cv2.putText(img, "Open CV", (300, 400), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 150, 0), 1)
cv2.imshow("Image", img)
cv2.waitKey(0)
5、从四对对应点计算透视变换
import cv2
import numpy as np
img = cv2.imread("Resources/card.png")
width, height = 250, 350
pts1 = np.float32([[481, 29], [757, 132], [328, 457], [602, 555]])
print(pts1)
pts2 = np.float32([[0, 0], [width, 0], [0, height], [width, height]])
matrix = cv2.getPerspectiveTransform(pts1, pts2) # 透视变换
imgOut = cv2.warpPerspective(img, matrix, (width, height))
cv2.imshow("Image", img)
cv2.imshow("OutPut", imgOut)
cv2.waitKey(0)
6、图像结合
import cv2
import numpy as np
img = cv2.imread("Resources/lena.png")
imgHor = np.hstack((img, img, img)) # 水平堆叠
imgVer = np.vstack((img, img)) # 垂直堆叠
cv2.imshow("Horizontal", imgHor)
cv2.imshow("Vertical", imgVer)
cv2.waitKey(0)
7、颜色识别
python -opencv 使用滑动条
函数createTrackbar:
cv2.createTrackbar(“scale”, “display”, 0, 100, self.opencv_calibration_node.on_scale)
功能:
绑定滑动条和窗口,定义滚动条的数值
参数
- 第一个参数时滑动条的名字
- 第二个参数是滑动条被放置的窗口的名字
- 第三个参数是滑动条默认值
- 第四个参数时滑动条的最大值
- 第五个参数时回调函数,每次滑动都会调用回调函数
函数getTrackbarPos:
cv2.getTrackbarPos()
功能:
得到滑动条的数值
参数
- 第一个参数是滑动条名字
- 第二个时所在窗口,
- 返回值是滑动条的数值。
函数setTrackbarPos:
cv2.setTrackbarPos(‘Alpha’, ‘image’, 100)
功能:
设置滑动条的默认值
参数
- 第一个参数是滑动条名字,
- 第二个时所在窗口,
- 第三个参数是滑动条默认值,
import cv2
import numpy as np
def empty(a):
pass
def stackImages(scale,imgArray):
rows = len(imgArray)
cols = len(imgArray[0])
rowsAvailable = isinstance(imgArray[0], list)
width = imgArray[0][0].shape[1]
height = imgArray[0][0].shape[0]
if rowsAvailable:
for x in range ( 0, rows):
for y in range(0, cols):
if imgArray[x][y].shape[:2] == imgArray[0][0].shape [:2]:
imgArray[x][y] = cv2.resize(imgArray[x][y], (0, 0), None, scale, scale)
else:
imgArray[x][y] = cv2.resize(imgArray[x][y], (imgArray[0][0].shape[1], imgArray[0][0].shape[0]), None, scale, scale)
if len(imgArray[x][y].shape) == 2: imgArray[x][y]= cv2.cvtColor( imgArray[x][y], cv2.COLOR_GRAY2BGR)
imageBlank = np.zeros((height, width, 3), np.uint8)
hor = [imageBlank]*rows
hor_con = [imageBlank]*rows
for x in range(0, rows):
hor[x] = np.hstack(imgArray[x])
ver = np.vstack(hor)
else:
for x in range(0, rows):
if imgArray[x].shape[:2] == imgArray[0].shape[:2]:
imgArray[x] = cv2.resize(imgArray[x], (0, 0), None, scale, scale)
else:
imgArray[x] = cv2.resize(imgArray[x], (imgArray[0].shape[1], imgArray[0].shape[0]), None,scale, scale)
if len(imgArray[x].shape) == 2: imgArray[x] = cv2.cvtColor(imgArray[x], cv2.COLOR_GRAY2BGR)
hor= np.hstack(imgArray)
ver = hor
return ver
path = 'Resources/lambo.png'
cv2.namedWindow("TrackBars")
cv2.resizeWindow("TrackBars",640,240)
cv2.createTrackbar("Hue Min","TrackBars",0,179,empty)
cv2.createTrackbar("Hue Max","TrackBars",19,179,empty)
cv2.createTrackbar("Sat Min","TrackBars",110,255,empty)
cv2.createTrackbar("Sat Max","TrackBars",240,255,empty)
cv2.createTrackbar("Val Min","TrackBars",153,255,empty)
cv2.createTrackbar("Val Max","TrackBars",255,255,empty)
while True:
img = cv2.imread(path)
imgHSV = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
h_min = cv2.getTrackbarPos("Hue Min","TrackBars")
h_max = cv2.getTrackbarPos("Hue Max", "TrackBars")
s_min = cv2.getTrackbarPos("Sat Min", "TrackBars")
s_max = cv2.getTrackbarPos("Sat Max", "TrackBars")
v_min = cv2.getTrackbarPos("Val Min", "TrackBars")
v_max = cv2.getTrackbarPos("Val Max", "TrackBars")
print(h_min,h_max,s_min,s_max,v_min,v_max)
lower = np.array([h_min,s_min,v_min])
upper = np.array([h_max,s_max,v_max])
mask = cv2.inRange(imgHSV,lower,upper)
imgResult = cv2.bitwise_and(img,img,mask=mask)
# cv2.imshow("Original",img)
# cv2.imshow("HSV",imgHSV)
# cv2.imshow("Mask", mask)
# cv2.imshow("Result", imgResult)
imgStack = stackImages(0.6,([img,imgHSV],[mask,imgResult]))
cv2.imshow("Stacked Images", imgStack)
cv2.waitKey(1)
8、形状识别
import cv2
import numpy as np
def stackImages(scale, imgArray):
rows = len(imgArray)
cols = len(imgArray[0])
rowsAvailable = isinstance(imgArray[0], list)
width = imgArray[0][0].shape[1]
height = imgArray[0][0].shape[0]
if rowsAvailable:
for x in range(0, rows):
for y in range(0, cols):
if imgArray[x][y].shape[:2] == imgArray[0][0].shape[:2]:
imgArray[x][y] = cv2.resize(imgArray[x][y], (0, 0), None, scale, scale)
else:
imgArray[x][y] = cv2.resize(imgArray[x][y], (imgArray[0][0].shape[1], imgArray[0][0].shape[0]),
None, scale, scale)
if len(imgArray[x][y].shape) == 2: imgArray[x][y] = cv2.cvtColor(imgArray[x][y], cv2.COLOR_GRAY2BGR)
imageBlank = np.zeros((height, width, 3), np.uint8)
hor = [imageBlank] * rows
hor_con = [imageBlank] * rows
for x in range(0, rows):
hor[x] = np.hstack(imgArray[x])
ver = np.vstack(hor)
else:
for x in range(0, rows):
if imgArray[x].shape[:2] == imgArray[0].shape[:2]:
imgArray[x] = cv2.resize(imgArray[x], (0, 0), None, scale, scale)
else:
imgArray[x] = cv2.resize(imgArray[x], (imgArray[0].shape[1], imgArray[0].shape[0]), None, scale, scale)
if len(imgArray[x].shape) == 2: imgArray[x] = cv2.cvtColor(imgArray[x], cv2.COLOR_GRAY2BGR)
hor = np.hstack(imgArray)
ver = hor
return ver
def getContours(img):
contours, hierarchy = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
for cnt in contours:
area = cv2.contourArea(cnt)
print(area)
if area > 500:
cv2.drawContours(imgContour, cnt, -1, (255, 0, 0), 3)
peri = cv2.arcLength(cnt, True)
# print(peri)
approx = cv2.approxPolyDP(cnt, 0.02 * peri, True)
print(len(approx))
objCor = len(approx)
x, y, w, h = cv2.boundingRect(approx)
if objCor == 3:
objectType = "Tri"
elif objCor == 4:
aspRatio = w / float(h)
if aspRatio > 0.98 and aspRatio < 1.03:
objectType = "Square"
else:
objectType = "Rectangle"
elif objCor > 4:
objectType = "Circles"
else:
objectType = "None"
cv2.rectangle(imgContour, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.putText(imgContour, objectType,
(x + (w // 2) - 10, y + (h // 2) - 10), cv2.FONT_HERSHEY_COMPLEX, 0.7,
(0, 0, 0), 2)
path = 'Resources/shape.jpg'
img = cv2.imread(path)
imgContour = img.copy() #复制原有的图像来获得一副新图像。
imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
imgBlur = cv2.GaussianBlur(imgGray, (7, 7), 1)
imgCanny = cv2.Canny(imgBlur, 50, 50)
getContours(imgCanny)
imgBlank = np.zeros_like(img)
imgStack = stackImages(0.8, ([img, imgGray, imgBlur],
[imgCanny, imgContour, imgBlank]))
cv2.imshow("Stack", imgStack)
cv2.waitKey(0)
9、人脸识别
import cv2
import numpy as np
faceCascade = cv2.CascadeClassifier("Resources/haarcascade_frontalface_default.xml")
img = cv2.imread('Resources/lena.png')
imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(imgGray, 1.1, 4)
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
pass
cv2.imshow("result", img)
cv2.waitKey(0)
摄像头实时人脸检测
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
cap.set(3, 640) # 设置宽度
cap.set(4, 480) # 设置高度
cap.set(10, 100) # 设置亮度
faceCascade = cv2.CascadeClassifier("Resources/haarcascade_frontalface_default.xml")
while True:
success, img = cap.read()
imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(imgGray, 1.1, 4)
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
pass
cv2.imshow("Result", img)
if cv2.waitKey(1) & 0xFF == ord(
'q'): # 总体效果:按下q键后break。cv2.waitKey(1)在有按键按下的时候返回按键的ASCII值,否则返回-1,因为有些系统cv2.waitkey(1)的返回值不只8位
break
pass