- 常规7项 *** 作
- 1.滤波
- 2.二值化
- 3.通道合并与分离
- 4.图形绘制&添加文字
- 5.图形变换
- 6.形态学
- 7.边缘检测
import cv2
import numpy as np
# 灰度图0 非灰度1
img = cv2.imread(cat.png',1)
# 模糊
blur = cv2.blur(img, (5,5))
# 高斯模糊
gaussian_blur = cv2.GaussianBlur(img, (5, 5), 0)
# 模糊中值
median_blur = cv2.medianBlur(img, 5)
# 双边滤波器
bi_blur = cv2.bilateralFilter(img, 9, 75, 75)
cv2.imshow("blur",blur) # 普通模糊图像
cv2.imshow("GaussianBlur",gaussian_blur) # 高斯模糊图像
cv2.imshow("MedianBlur",median_blur) # 模糊中值图像
cv2.imshow("BilateralFilter",bi_blur) # 双边滤波器图像
cv2.waitKey(0)
2.二值化
jupyter运行
import cv2
from matplotlib import pyplot as plt
import numpy as np
import random
# 灰度图0 非灰度1
img = cv2.imread('./datasets/cat.png',1)
import cv2
from matplotlib import pyplot as plt
img = cv2.imread('cat.jpg',1)
img_ = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(img_, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
dst = cv2.adaptiveThreshold(img_, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 5, 2)
plt.figure(figsize=(10,10), dpi=80)
plt.figure(1)
ax1 = plt.subplot(221)
ax1.imshow(thresh)
ax1.set_title("threshold") #二值化
ax2 = plt.subplot(222)
ax2.imshow(dst)
ax2.set_title("adaptiveThreshold") #二值化
3.通道合并与分离
import cv2
from matplotlib import pyplot as plt
img = cv2.imread('cat.jpg',1)
b, g, r = cv2.split(img)
r = r // 2
img_ = cv2.merge((b, g, r))
plt.figure(figsize=(10,10), dpi=80)
ax1 = plt.subplot(221)
ax1.imshow(img_)
4.图形绘制&添加文字
line_ = cv2.line(img, (0, 0), (30, 30), (255, 0, 0), 3) # pt1, pt2, color, thickness
cv2.rectangle(img, (0, 0), (50, 50), (255, 0, 0), 3) # pt1, pt2, color, thickness
cv2.circle(img, (50, 50), 3, (0, 0, 255), -1) # center, radius, color, shift
pts = np.array([[10, 5], [20, 30], [70, 20], [50, 10]], np.int32).reshape((-1, 2))
cv2.polylines(img, [pts], True, (0, 255, 255)) # pts, is_closed, color
font = cv2.FONT_HERSHEY_SIMPLEX # text, start_pt, font, fontscale, color, thickness, linetype
cv2.putText(img, 'hello', (10, 30), font, 1, (255, 255, 0), 2, cv2.LINE_AA)
plt.figure(figsize=(20,20), dpi=80)
plt.figure(1)
ax1 = plt.subplot(221)
ax1.imshow(line_)
ax1.set_title("为图像添加图形和文字")
5.图形变换
# 图像旋转
angle = random.uniform(-15, 15)
h, w = img.shape[:2]
center = (w // 2, h // 2)
M = cv2.getRotationMatrix2D(center, angle, scale=1)
rotated = cv2.warpAffine(img, M, (w, h))
# 透视变换 Perspective Transformation
pts1 = np.float32([[56, 65], [300, 52], [28, 300], [250, 300]])
pts2 = np.float32([[0, 0], [300, 0], [0, 300], [300, 300]])
M = cv2.getPerspectiveTransform(pts1, pts2)
Perspective = cv2.warpPerspective(img, M, (300, 160)) # (s1, s2): scale of the output
# 仿射变换 Affine Transformation
pts1 = np.float32([[50, 50], [100, 50], [50, 200]])
pts2 = np.float32([[10, 100], [100, 50], [100, 250]])
M = cv2.getRotationMatrix2D((150,80), -15, 1)
Affine = cv2.warpAffine(img, M, (150,80))
plt.figure(figsize=(16,10), dpi=80)
plt.figure(1)
ax1 = plt.subplot(221)
ax1.imshow(rotated)
ax1.set_title("Transformation") # 图像旋转
ax2 = plt.subplot(222)
ax2.imshow(Perspective)
ax2.set_title("Perspective Transformation") #透视变换
ax3 = plt.subplot(223)
ax3.imshow(Affine)
ax3.set_title("Affine Transformation") # 仿射变换
6.形态学
# kernel ==> element 算子
element = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
element2 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
element3 = cv2.getStructuringElement(cv2.MORPH_CROSS, (5, 5))
kernel = np.ones((5, 5), np.uint8)
# 腐蚀
erosion = cv2.erode(img, kernel, iterations=1)
# 膨胀
dilation = cv2.dilate(img,kernel)
# 通用形态学
opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
closing = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel, 2)
# 形态学梯度 = dilation - erosion,用于提取物体轮廓
gradiet = cv2.morphologyEx(img, cv2.MORPH_GRADIENT, kernel)
# 顶帽 = src - open,用来分离比邻近点亮一些的斑块
tophat = cv2.morphologyEx(img, cv2.MORPH_TOPHAT, kernel)
# 黑帽 = src - close,用来分离比邻近点暗一些的斑块
blackhat= cv2.morphologyEx(img, cv2.MORPH_BLACKHAT, kernel)
plt.figure(figsize=(16,10), dpi=80)
plt.figure(1)
ax1 = plt.subplot(241)
ax1.imshow(erosion) # 图像腐蚀
ax2 = plt.subplot(242)
ax2.imshow(dilation) # 图像膨胀
ax3 = plt.subplot(243)
ax3.imshow(opening) # 通用形态学OPEN
ax4 = plt.subplot(244)
ax4.imshow(closing) # 通用形态学CLOSE
ax5 = plt.subplot(245)
ax5.imshow(gradiet) # 形态学梯度
ax6 = plt.subplot(246)
ax6.imshow(tophat) # 顶帽
ax7 = plt.subplot(247)
ax7.imshow(blackhat) # 黑帽
7.边缘检测
# laplacian算子,cv2.CV_64F为图像格式,常见的还有cv2.CV_8U
laplacian = cv2.Laplacian(img, cv2.CV_64F)
# sobel算子 cv2.CV_16S: 16位有符号的数据类型
x = cv2.Sobel(img, cv2.CV_16S, 1, 0)
y = cv2.Sobel(img, cv2.CV_16S, 0, 1)
# Canny边缘检测:效果最好,提取精细边缘
edges = cv2.Canny(img, 100, 200)
plt.figure(figsize=(20,20), dpi=80)
plt.figure(1)
ax1 = plt.subplot(221)
ax1.imshow(edges) # 边缘检测
更多请见:
https://zhuanlan.zhihu.com/p/382191938
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)