立即学习:https://edu.youkuaiyun.com/course/play/26281/327083?utm_source=blogtoedu
课程代码
# 图像的几何变换
import cv2
import matplotlib.pyplot as plt
import numpy as np
img = cv2.imread("lena.png")
# cv2.imshow("lena.png",img)
gray= cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# 实现图像的翻转和改变大小
w,h =img.shape[0:2]
resized = cv2.resize(img,(int(w/4),int(h/2)))
flipped = cv2.flip(img,-1) # 后面的-1也可以取1,0,代表不同方向的翻转
# cv2.imshow("resized",resized)
# cv2.imshow("flipped",flipped)
# 距离变换
# 要先二值化,使用threshold函数
ret,thr =cv2.threshold(gray,100,255,cv2.THRESH_OTSU)
# print(ret)
# cv2.imshow("thr",thr) # 二值化后的图像
dist =cv2.distanceTransform(thr,cv2.DIST_L2,cv2.DIST_MASK_3)
dist_norm = cv2.convertScaleAbs(dist)
# cv2.imshow("dist",dist_norm)
# Log-Polar变换
center =(w/2,h/2)
maxRadius =0.7*min(center)
M= w/cv2.log(maxRadius) # 尺度
print(maxRadius,M[0])
log_polar = cv2.logPolar(img,center,M[0]*0.8,cv2.INTER_LINEAR + cv2.WARP_FILL_OUTLIERS)
# cv2.imshow("log_polar",log_polar)
# 灰度直方图和直方图均衡化
# plt.hist(gray.ravel(),256,[0,256])
# plt.show()
# equa = cv2.equalizeHist(gray)
# cv2.imshow("equa",equa)
# 实现hough变换
edges = cv2.Canny(thr,50,150) # 用canny算子进行边缘检测
cv2.imshow("edges",edges)
disp_edge =cv2.cvtColor(edges,cv2.COLOR_GRAY2BGR)
# cv2.imshow("disp_edge",disp_edge)
lines = cv2.HoughLinesP(edges, 1, 1*np.pi/180, 10)
for line in lines:
for x1,y1,x2,y2 in line:
cv2.line(disp_edge,(x1,y1),(x2,y2),(0,255,0),1)
pass
print("Line count:",len(lines))
cv2.imshow("hough",disp_edge)
cv2.waitKey()
cv2.destroyAllWindows()