读取图片和网络摄像头
1.1 图片读取
python
复制代码
import cv2
img = cv2.imread("Resources/jty.jpg")
cv2.namedWindow("Output", cv2.WINDOW_NORMAL) # 创建一个窗口,并指定窗口名称
cv2.resizeWindow("Output", 600, 840) # 设置窗口大小
cv2.imshow("Output",img)
cv2.waitKey(0)
1.2 视频读取
1.2.1 读取视频文件
python
复制代码
import cv2
# read video
cap = cv2.VideoCapture("Resources/test_video.mp4")
while True:
success,img = cap.read()
cv2.imshow("Video",img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
1.2.2 读取网络摄像头
python
复制代码
import cv2
# read webcam
cap = cv2.VideoCapture(0)
cap.set(3,640) #width
cap.set(4,480) #height
cap.set(10,100)
while True:
success,img = cap.read()
cv2.imshow("Video",img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
OpenCV基础功能
python
复制代码
import cv2
import numpy as np
# basic function
img = cv2.imread("Resources/jty.jpg")
kernel = np.ones((5, 5), np.uint8)
# 灰度转换
imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
imgGray = cv2.resize(imgGray,(350,300))
# 图像模糊
imgBlur = cv2.GaussianBlur(imgGray, (7, 7), 0)
#imgBlur = cv2.resize(imgBlur,(350,300))
# 边缘检测
imgCanny = cv2.Canny(img, 100, 100)
imgCanny = cv2.resize(imgCanny,(350,300))
# 膨胀
imgDialation = cv2.dilate(imgCanny, kernel, iterations=1)
#imgDialation = cv2.resize(imgDialation,(350,300))
# 腐蚀
imgEroded = cv2.erode(imgDialation, kernel, iterations=1)
#imgEroded = cv2.resize(imgEroded,(350,300))
# cv2.imshow("Output",img)
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)
调整、裁剪图像
3.1 调整图像大小
python
复制代码
import cv2
# resize image
img = cv2.imread("Resources/zjy.png")
print(img.shape)
imgResize = cv2.resize(img,(250,300))
print(imgResize.shape)
cv2.imshow("Image",img)
cv2.imshow("Image Resize",imgResize)
cv2.waitKey(0)
裁剪图像
python
复制代码
import cv2
img=cv2.imread("Resources/zjy.png")
img = cv2.resize(img,(250,300))
cv2.imshow('image',img)
print(img.shape)#height,width,dpth
crop_img=img[20:200,50:200]
cv2.imshow('crop image',crop_img)
cv2.waitKey(0)
图像上绘制形状和文本
4.1 图像上绘制形状
python
复制代码
import cv2
import numpy as np
# shapes and texts
img = np.zeros((512, 512, 3), np.uint8)
cv2.imshow('oringin image', img)
# 从左上角(0, 0)到右下角(图像的宽度和高度)绘制了一条绿色线条,线条的粗细为3个像素
cv2.line(img, (0, 0), (img.shape[1], img.shape[0]), (0, 255, 0), 3)
cv2.imshow('line image', img)
# 在图像上绘制了一个红色矩形,左上角坐标为(0, 0),右下角坐标为(250, 350),线条的粗细为2个像素。
cv2.rectangle(img, (0, 0), (250, 350), (0, 0, 255), 2)
cv2.imshow('rectangle image', img)
# 在图像上绘制了一个黄色圆形,圆心坐标为(400, 500),半径为30个像素,线条的粗细为5个像素。
cv2.circle(img, (400, 500), 30, (255, 255, 0), 5)
cv2.imshow('circle image', img)
cv2.waitKey(0)
4.2 图像上写文字
python
复制代码
import cv2
import numpy as np
img = np.zeros((512,512,3),np.uint8)
cv2.imshow('oringin image',img)
# 这将在图像 img 上绘制文本 "OPENCV",文本的左下角位于坐标 (300, 200) 处,使用 cv2.FONT_HERSHEY_COMPLEX 字体,字体大小为基本大小的 1 倍,文本颜色为蓝绿色 (0, 150, 0),线条粗细为 1 像素。
cv2.putText(img,"OPENCV",(300,200),cv2.FONT_HERSHEY_COMPLEX,1,(0,150,0),1)
cv2.imshow("putText01 Image",img)
cv2.putText(img,"I LOVE XD",(100,300),cv2.FONT_HERSHEY_COMPLEX,1,(0,150,0),1)
cv2.imshow("putText02 Image",img)
cv2.waitKey(0)
图像拼接
python
复制代码
import cv2
import numpy as np
# join images
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
# 这行代码使用了一个for循环来遍历imgArray中的每一行(imgArray应该是一个二维列表,其中每个子列表包含了一行中要堆叠的图像)。np.hstack函数用于将imgArray[x]中的所有图像在水平方向上堆叠起来,然后将堆叠后的结果赋值给hor[x]。
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
img = cv2.imread('Resources/jty.jpg')
imgGray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
imgStack = stackImages(0.15,([img,imgGray,img],[img,img,img]))
cv2.imshow("ImageStack",imgStack)
cv2.waitKey(0)
人脸识别
6.1 静态图片
python
复制代码
import cv2
# face detection
faceCascade = cv2.CascadeClassifier("Resources/haarcascade_frontalface_default.xml")
img = cv2.imread("Resources/jty.jpg")
img = cv2.resize(img,(250,300))
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)
cv2.imshow("Result",img)
cv2.waitKey(0)
6.2 摄像头
python
复制代码
import cv2
faceCascade = cv2.CascadeClassifier("Resources/haarcascade_frontalface_default.xml")
cap = cv2.VideoCapture(0)
cap.set(3,640) #width
cap.set(4,480) #height
cap.set(10,100)
while True:
success,img = cap.read()
cv2.imshow("Video",img)
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)
cv2.imshow("Result", img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break