import cv2
import numpy as np
# 读取图像
img = cv2.imread('input.jpg')
# 图像滤波
blur = cv2.GaussianBlur(img, (5, 5), 0)
# 边缘检测
edges = cv2.Canny(img, 100, 200)
# 图像变换
rows, cols = img.shape[:2]
M = cv2.getRotationMatrix2D((cols/2, rows/2), 45, 1)
dst = cv2.warpAffine(img, M, (cols, rows))
# 颜色空间转换
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# 显示结果
cv2.imshow('Original', img)
cv2.imshow('Blurred', blur)
cv2.imshow('Edges', edges)
cv2.imshow('Transformed', dst)
cv2.imshow('HSV', hsv)
cv2.waitKey(0)
cv2.destroyAllWindows()
三、第二个示例代码
import cv2
import numpy as np
# 读取图像
img = cv2.imread('input.jpg')
# 转换为灰度图像
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 二值化
ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
# 膨胀和腐蚀
kernel = np.ones((5,5),np.uint8)
dilation = cv2.dilate(thresh, kernel, iterations=1)
erosion = cv2.erode(thresh, kernel, iterations=1)
# 透视变换
pts1 = np.float32([[56,65],[368,52],[28,387],[389,390]])
pts2 = np.float32([[0,0],[300,0],[0,300],[300,300]])
M = cv2.getPerspectiveTransform(pts1,pts2)
perspective = cv2.warpPerspective(img,M,(300,300))
# 转换为LAB颜色空间
lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
# 显示结果
cv2.imshow('Original', img)
cv2.imshow('Gray', gray)
cv2.imshow('Thresh', thresh)
cv2.imshow('Dilation', dilation)
cv2.imshow('Erosion', erosion)
cv2.imshow('Perspective', perspective)
cv2.imshow('LAB', lab)
cv2.waitKey(0)
cv2.destroyAllWindows()
四、第三个示例代码
import cv2
import numpy as np
# 读取图像
img = cv2.imread('input.jpg')
# 图像缩放
resized = cv2.resize(img, None, fx=0.5, fy=0.5, interpolation=cv2.INTER_CUBIC)
# 旋转图像
rows, cols = img.shape[:2]
M = cv2.getRotationMatrix2D((cols/2, rows/2), 90, 1)
rotated = cv2.warpAffine(img, M, (cols, rows))
# 图像平移
M = np.float32([[1, 0, 100], [0, 1, 50]])
translated = cv2.warpAffine(img, M, (cols, rows))
# 图像融合
img2 = cv2.imread('input2.jpg')
blended = cv2.addWeighted(img, 0.7, img2, 0.3, 0)
# 显示结果
cv2.imshow('Resized', resized)
cv2.imshow('Rotated', rotated)
cv2.imshow('Translated', translated)
cv2.imshow('Blended', blended)
cv2.waitKey(0)
cv2.destroyAllWindows()
五、第四个示例代码
import cv2
import numpy as np
# 读取图像
img = cv2.imread('input.jpg')
# 边缘保留滤波
dst = cv2.edgePreservingFilter(img, flags=1, sigma_s=60, sigma_r=0.4)
# 图像修复
mask = np.zeros(img.shape[:2], np.uint8)
mask[100:300, 100:400] = 255
inpainted = cv2.inpaint(img, mask, inpaintRadius=3, flags=cv2.INPAINT_TELEA)
# 角点检测
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
corners = cv2.goodFeaturesToTrack(gray, maxCorners=25, qualityLevel=0.01, minDistance=10)
# 标记角点
for corner in corners:
x, y = corner.ravel()
cv2.circle(img, (x, y), 5, (0, 0, 255), -1)
# 显示结果
cv2.imshow('Edge Preserving Filter', dst)
cv2.imshow('Inpainted', inpainted)
cv2.imshow('Corners', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
六、第五个示例代码
import cv2
import numpy as np
# 读取图像
img = cv2.imread('input.jpg')
# 图像金字塔
lower_reso = cv2.pyrDown(img)
higher_reso = cv2.pyrUp(img)
# 角点检测与追踪
feature_params = dict( maxCorners = 100, qualityLevel = 0.3, minDistance = 7, blockSize = 7 )
lk_params = dict( winSize = (15,15), maxLevel = 2, criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03))
p0 = cv2.goodFeaturesToTrack(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY), mask = None, \*\*feature_params)
old_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 创建随机颜色
color = np.random.randint(0,255,(100,3))
# 光流追踪
mask = np.zeros_like(img)
while True:
ret, frame = cap.read()
**收集整理了一份《2024年最新Python全套学习资料》免费送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。**






**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上Python知识点,真正体系化!**
**了解详情:https://docs.qq.com/doc/DSnl3ZGlhT1RDaVhV**