安装 OpenCV:
Python:直接 pip install opencv-python
(核心库)和 opencv-contrib-python
(扩展功能)。
pip install opencv-python
pip install opencv-contrib-python
验证安装:
import cv2
print(cv2.__version__) # 输出版本号
以下代码来源于:链接
if __name__ == '__main__':# 读取图像image = cv2.imread('1.png')# 显示图像# cv2.imshow('Image', image)gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)print('gray_image = ', gray_image)# cv2.imshow('Image', gray_image)edges = cv2.Canny(gray_image, threshold1=50, threshold2=150)print('edges = ', edges)kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))dilated = cv2.dilate(edges, kernel, iterations=1)cv2.imshow('Image', dilated)cv2.waitKey(0)cv2.destroyAllWindows()# 特征检测# image = cv2.imread('3.png')# orb = cv2.ORB_create()# keypoints, descriptors = orb.detectAndCompute(image, None)# image_with_keypoints = cv2.drawKeypoints(image, keypoints, None, color=(0, 255, 0))# cv2.imshow('Keypoints', image_with_keypoints)# cv2.waitKey(0)# cv2.destroyAllWindows()# 人脸检测# image = cv2.imread('3.png')# face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')# gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)# faces = face_cascade.detectMultiScale(gray_image, scaleFactor=1.1, minNeighbors=5)# for (x, y, w, h) in faces:# cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)# cv2.imshow('Faces', image)# cv2.waitKey(0)# cv2.destroyAllWindows()# 交通标志识别# image = cv2.imread('5.png')## gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)# edges = cv2.Canny(gray_image, threshold1=50, threshold2=150)## kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))# dilated = cv2.dilate(edges, kernel, iterations=1)# contours, _ = cv2.findContours(dilated, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)# for contour in contours:# if cv2.contourArea(contour) > 100:# x, y, w, h = cv2.boundingRect(contour)# cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)## cv2.imshow('Traffic Sign Detection', image)# cv2.waitKey(0)# cv2.destroyAllWindows()# 显示图像# image = cv2.imread('3.png')# gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)# # 下面这行代码效果等价于上面两行代码# gray_scare_image = cv2.imread('3.png', cv2.IMREAD_GRAYSCALE)# ret, binary = cv2.threshold(gray_scare_image, 127, 255, cv2.THRESH_BINARY)# ret_inv, binary_inv = cv2.threshold(gray_scare_image, 127, 255, cv2.THRESH_BINARY_INV)# cv2.imshow('gray_image', gray_image)# cv2.imshow('gray_scare_image', gray_scare_image)# cv2.imshow('binary', binary)# cv2.imshow('binary_inv', binary_inv)# cv2.waitKey(0)# cv2.destroyAllWindows()
这个链接也看了:链接
拆分通道:
# # 显示图像# image = cv2.imread('3.png')# cv2.imshow('Image', image)# gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)# cv2.imshow('gray_image', gray_image)# hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)# cv2.imshow('hsv_image', hsv_image)# yuv_image = cv2.cvtColor(image, cv2.COLOR_BGR2YUV)# cv2.imshow('yuv_image', yuv_image)## # BGR通道拆分显示# b, g, r = cv2.split(image)# cv2.imshow("BGR - B", b)# cv2.imshow("BGR - G", g)# cv2.imshow("BGR - R", r)## # HSV通道拆分显示# h, s, v = cv2.split(hsv_image)# cv2.imshow("HSV - H", h) # 色相(颜色种类)# cv2.imshow("HSV - S", s) # 饱和度(颜色鲜艳程度)# cv2.imshow("HSV - V", v) # 明度(亮度)## # YUV通道拆分显示# y, u, v = cv2.split(yuv_image)# cv2.imshow("YUV - Y", y) # 亮度(类似灰度图)# cv2.imshow("YUV - U", u) # 蓝色色度# cv2.imshow("YUV - V", v) # 红色色度
腐蚀与膨胀:
image = cv2.imread('3.png')kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))# 腐蚀(Erosion):将图像中的白色区域收缩。eroded_img = cv2.erode(image, kernel, iterations=1)# 膨胀(Dilation):将图像中的白色区域扩展。dilated_img = cv2.dilate(image, kernel, iterations=1)# 开运算(先腐蚀再膨胀):用于去除小物体。opening_img = cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel)# 闭运算(先膨胀再腐蚀):用于填补图像中的小孔洞。closing_img = cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel)cv2.imshow('Image', image)cv2.imshow('eroded_img', eroded_img)cv2.imshow('dilated_img', dilated_img)cv2.imshow('opening_img', opening_img)cv2.imshow('closing_img', closing_img)cv2.waitKey(0)cv2.destroyAllWindows()
图像旋转、平移、翻转:
# 图像旋转image = cv2.imread('3.png')(h, w) = image.shape[:2]center = (w // 2, h // 2)M = cv2.getRotationMatrix2D(center, 45, 1.0) # 旋转 45 度rotated_img = cv2.warpAffine(image, M, (w, h))cv2.imshow('rotated_img', rotated_img)# 图像平移M = np.float32([[1, 0, 100], [0, 1, 50]]) # 向右平移 100 像素,向下平移 50 像素translated_img = cv2.warpAffine(image, M, (w, h))cv2.imshow('translated_img', translated_img)# 图像翻转flipped_img = cv2.flip(image, 1) # 水平翻转cv2.imshow('flipped_img', flipped_img)cv2.waitKey(0)cv2.destroyAllWindows()