赞
踩
OpenCV(Open Source Computer Vision Library)是一个开源的计算机视觉和机器学习软件库。OpenCV-Python是其Python接口,结合了OpenCV C++ API和Python语言的特点,使得在Python中实现高效、强大的计算机视觉任务变得简单。
在Python环境中,通常可以使用pip进行安装:
pip install opencv-python
如果需要包含OpenCV的contrib模块,可以使用:
pip install opencv-contrib-python
import cv2
image = cv2.imread('image.jpg')
cv2.imshow('Image', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imwrite('gray_image.jpg', gray_image)
cap = cv2.VideoCapture('video.mp4')
while True:
ret, frame = cap.read()
if not ret:
break
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('Video', gray_frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
OpenCV提供了预训练的Haar级联分类器,可以用于检测图像中的人脸。以下是一个简单的示例:
import cv2 # 加载Haar级联分类器 face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml') # 读取图像 img = cv2.imread('group_photo.jpg') # 转换为灰度图 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 检测人脸 faces = face_cascade.detectMultiScale(gray, 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('Detected Faces', img) cv2.waitKey(0) cv2.destroyAllWindows()
OpenCV中的Meanshift和Camshift算法可以用于跟踪视频中的移动对象。
import cv2 # 初始化视频捕获对象 cap = cv2.VideoCapture('tracking_video.mp4') # 读取第一帧 ret, frame = cap.read() # 设置初始跟踪窗口 x, y, w, h = 300, 200, 100, 50 track_window = (x, y, w, h) # 设置ROI用于跟踪 roi = frame[y:y+h, x:x+w] # 转换为HSV并创建掩模 hsv_roi = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV) mask = cv2.inRange(hsv_roi, np.array((0., 60., 32.)), np.array((180., 255., 255.))) # 计算直方图,用于反向投影 roi_hist = cv2.calcHist([hsv_roi], [0], mask, [180], [0, 180]) cv2.normalize(roi_hist, roi_hist, 0, 255, cv2.NORM_MINMAX) # 设置终止条件,迭代10次或移动至少1 pt term_crit = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1) while True: ret, frame = cap.read() if not ret: break # 转换为HSV hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) dst = cv2.calcBackProject([hsv], [0], roi_hist, [0, 180], 1) # 应用Meanshift获取新位置 ret, track_window = cv2.meanShift(dst, track_window, term_crit) # 在图像上绘制它 x, y, w, h = track_window img2 = cv2.rectangle(frame, (x, y), (x+w, y+h), 255, 2) cv2.imshow('Meanshift Tracking', img2) k = cv2.waitKey(60) & 0xff if k == 27: break cv2.destroyAllWindows() cap.release()
使用SIFT、SURF等算法进行特征提取和匹配,可以用于图像拼接、3D重建等。
import cv2 import numpy as np # 读取两幅图像 img1 = cv2.imread('box.png', cv2.IMREAD_GRAYSCALE) img2 = cv2.imread('box_in_scene.png', cv2.IMREAD_GRAYSCALE) # 初始化SIFT检测器 sift = cv2.SIFT_create() # 计算关键点和描述符 kp1, des1 = sift.detectAndCompute(img1, None) kp2, des2 = sift.detectAndCompute(img2, None) # 使用BFMatcher进行匹配 bf = cv2.BFMatcher() matches = bf.knnMatch(des1, des2, k=2) # 应用比率测试 good = [] for m, n in matches: if m.distance < 0.75 * n.distance: good.append(m) # 绘制匹配结果 img3 = cv2.drawMatches(img1, kp1, img2, kp2, good, None, flags=2) cv2.imshow('Feature Matches', img3) cv2.waitKey(0) cv2.destroyAllWindows()
深度学习模型和算法时,OpenCV-Python可能需要额外的依赖项,如TensorFlow、PyTorch或ONNX Runtime等。
import cv2 import tensorflow as tf # 加载TensorFlow的物体检测模型(假设已经有一个冻结的GraphDef模型) model_path = 'frozen_inference_graph.pb' labels_path = 'mscoco_label_map.pbtxt' num_classes = 90 # 加载模型和标签 detection_graph = tf.Graph() with detection_graph.as_default(): od_graph_def = tf.GraphDef() with tf.gfile.GFile(model_path, 'rb') as fid: serialized_graph = fid.read() od_graph_def.ParseFromString(serialized_graph) tf.import_graph_def(od_graph_def, name='') # 加载标签 label_map = label_map_util.load_labelmap(labels_path) categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=num_classes, use_display_name=True) category_index = label_map_util.create_category_index(categories) # 读取图像 image_np = np.array(cv2.imread('object_detection.jpg')) # 执行物体检测 with detection_graph.as_default(): with tf.Session(graph=detection_graph) as sess: # 扩展图像维度 image_np_expanded = np.expand_dims(image_np, axis=0) image_tensor = detection_graph.get_tensor_by_name('image_tensor:0') boxes = detection_graph.get_tensor_by_name('detection_boxes:0') scores = detection_graph.get_tensor_by_name('detection_scores:0') classes = detection_graph.get_tensor_by_name('detection_classes:0') num_detections = detection_graph.get_tensor_by_name('num_detections:0') # 运行模型 (boxes, scores, classes, num_detections) = sess.run( [boxes, scores, classes, num_detections], feed_dict={image_tensor: image_np_expanded}) # 可视化结果 v = visualize_boxes_and_labels_on_image_array( image_np, np.squeeze(boxes), np.squeeze(classes).astype(np.int32), np.squeeze(scores), category_index, use_normalized_coordinates=True, line_thickness=8) # 显示图像 cv2.imshow('Object Detection', image_np) cv2.waitKey(0) cv2.destroyAllWindows()
import cv2 # 加载人脸识别模型(假设已经训练好了) face_recognizer = cv2.face.LBPHFaceRecognizer_create() face_recognizer.read('face_model.yml') # 加载Haar级联分类器 face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml') # 启动视频捕获 cap = cv2.VideoCapture(0) while True: ret, frame = cap.read() if not ret: break gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) faces = face_cascade.detectMultiScale(gray, 1.1, 4) for (x, y, w, h) in faces: face_roi = gray[y:y+h, x:x+w] # 进行预测 label, confidence = face_recognizer.predict(face_roi) cv2.putText(frame, str(label), (x, y-5), cv2.FONT_HERSHEY_PLAIN, 1.5, (0, 255, 0), 2) cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2) cv2.imshow('Face Recognition', frame) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows()
OpenCV-Python是一个非常强大的工具,它为计算机视觉任务提供了一个丰富的函数库。通过结合深度学习和其他机器学习技术,OpenCV-Python可以用于解决复杂的问题,如图像识别、物体检测、人脸识别等。随着技术的发展,OpenCV-Python也在不断更新和改进,以支持更多的功能和算法。如果您对特定功能或应用有更多的问题,欢迎继续提问。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。