1.opencv人脸识别:
对图片中的人脸识别代码如下:
# USAGE
# python detect_faces.py --image rooster.jpg --prototxt deploy.prototxt.txt --model res10_300x300_ssd_iter_140000.caffemodel
# import the necessary packages
import numpy as np
import argparse
import cv2
# construct the argument parse and parse the arguments 加载一些参数
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,
help="path to input image")
ap.add_argument("-p", "--prototxt", required=True,
help="path to Caffe 'deploy' prototxt file")
ap.add_argument("-m", "--model", required=True,
help="path to Caffe pre-trained model")
ap.add_argument("-c", "--confidence", type=float, default=0.5,
help="minimum probability to filter weak detections")
args = vars(ap.parse_args())
# load our serialized model from disk #加载模型
print("[INFO] loading model...")
net = cv2.dnn.readNetFromCaffe(args["prototxt"], args["model"]) #存储模型为net
# load the input image and construct an input blob for the image
# by resizing to a fixed 300x300 pixels and then normalizing it
image = cv2.imread(args["image"])
(h, w) = image.shape[:2] #提取维度
blob = cv2.dnn.blobFromImage(cv2.resize(image, (300, 300)), 1.0,
(300, 300), (104.0, 177.0, 123.0))
# pass the blob through the network and obtain the detections and
# predictions
print("[INFO] computing object detections...")
net.setInput(blob)
detections = net.forward()
# loop over the detections
for i in range(0, detections.shape[2]):
# extract the confidence (i.e., probability) associated with the
# prediction
confidence = detections[0, 0, i, 2]
# filter out weak detections by ensuring the `confidence` is
# greater than the minimum confidence
if confidence > args["confidence"]:
# compute the (x, y)-coordinates of the bounding box for the
# object
box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
(startX, startY, endX, endY) = box.astype("int")
# draw the bounding box of the face along with the associated
# probability
text = "{:.2f}%".format(confidence * 100)
y = startY - 10 if startY - 10 > 10 else startY + 10
#将人框出
cv2.rectangle(image, (startX, startY), (endX, endY),
(0, 0, 255), 2)
#写出概率
cv2.putText(image, text, (startX, y),
cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 255), 2)
# show the output image
cv2.imshow("Output", image)
cv2.waitKey(0)
也可以对视频流,视频文件或树莓派摄像头进行处理。
注意,我使用的opencv是 opencv-contrib-python 版本为4.2.0.34
Python版本为3.5.6
2 opencv学习
(B, G, R) = image[100, 50] *(获取x=50 and y=100的BGR值)注: 存储分别为BGR而不是RGB。
黑色为0,白色为255
宽度为纵列(column),高度为横排(raw)
深度一般为3,代表3个通道
roi = image[60:160, 320:420]
裁剪从 x=320,y=60开始到x=420,y=160结束的感兴趣区域。即image[startY:endY, startX:endX]
重修尺寸resized = cv2.resize(image, (200, 200)) 不管他的横宽比
旋转图像
center = (w // 2, h // 2) 计算图像中心
M = cv2.getRotationMatrix2D(center, -45, 1.0) 顺时针旋转45度,计算旋转矩阵
rotated = cv2.warpAffine(image, M, (w, h)) 对图像进行旋转
使用imutils模块简化过程:
rotated = imutils.rotate(image, -45) (旋转超出部分不可见)
rotated = imutils.rotate_bound(image, 45) (保留整个图像边界)
平滑图像(模糊处理,减少噪声)
blurred = cv2.GaussianBlur(image, (11, 11), 0) (高斯模糊 使用11*11的核)
在图像上添加矩形框。
output = image.copy() 复制原图
cv2.rectangle(output, (320, 60), (420, 160), (0, 0, 255), 2) (用红色框出)
该函数内的各个参数意义(图像,开始像素(左上位置处),结束位置(右下位置处),颜色(这里为红色),thickness(粗细,当为负数时,代表实心))
添加实心圆
cv2.circle(output, (300, 150), 20, (255, 0, 0), -1)
函数参数(图像,中心位置,半径,颜色(255,0,0为蓝色),粗细(-1表示实心))
画一条线
cv2.line(output, (60, 20), (400, 200), (0, 0, 255), 5)
在图像上添加文字
cv2.putText(output, "OpenCV + Jurassic Park!!!", (10, 25),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
cv2.imshow("Text", output)
cv2.waitKey(0)
参数(图像,文字,文字开始点,字型,字大小,粗细)
将图像转换为灰度图
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
image = cv2.imread(args["image"])
cv2.imshow("Image", image)
cv2.waitKey(0)
# 将图像转换为灰度
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow("Gray", gray)
cv2.waitKey(0)
检测边缘(使用canny算法检测)
edged = cv2.Canny(gray, 30, 150)
参数(灰度图像,最小阈值,最大阈值,aperture_size(sobel核大小))
索贝尔算子是把图像中每个像素的上下左右四领域的灰度值加权差,在边缘处达到极值从而检测边缘
edged = cv2.Canny(gray, 30, 150)
cv2.imshow("Edged", edged)
cv2.waitKey(0)
图像阈值处理(目的是去除较亮或较暗的图像区域和轮廓)
thresh = cv2.threshold(gray, 225, 255, cv2.THRESH_BINARY_INV)[1]
cv2.imshow("Thresh", thresh)
cv2.waitKey(0)
将所有像素值大于225的设置为0(黑色),所有小于255的设置为255(白色)
检测并画出轮廓
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
output = image.copy()
# loop over the contours
for c in cnts:
# draw each contour on the output image with a 3px thick purple
# outline, then display the output contours one at a time
cv2.drawContours(output, [c], -1, (240, 0, 159), 3)
cv2.imshow("Contours", output)
cv2.waitKey(0)