立即学习:https://edu.csdn.net/course/play/26281/327083?utm_source=blogtoedu
1.距离变换
2.Log-polar变换
3.计算直方图
4.直方图均衡化
5.标准Hough变换
6.累计概率Hough变换:结果常常比标准Hough变换更好。
7.图像放缩
cv.resize(image,(width,height))
8.图像翻转(flipCode取值:0-沿x轴翻转,>0-沿y轴翻转,<0-沿x,y轴同时翻转)
cv.flip(image, flipCode)
import cv2 as cv
import matplotlib.pyplot as plt
import numpy as np
filename= "d:/lena.jpg"
img=cv.imread(filename)
#图像改变大小
w,h = img.shape[0:2]
resized = cv.resize(img, (int(w/2), int(h/2)))
flipped_0 = cv.flip(resized, 0)
flipped_1 = cv.flip(resized, 1)
cv.imshow("resized", resized)
cv.imshow("flipped 0", flipped_0)
cv.imshow("flipped 1", flipped_1)
cv.waitKey()
cv.destroyAllWindows()
#实现图像的距离变换
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
#二值化
ret, thr = cv.threshold(gray, 100, 255, cv.THRESH_OTSU)
dist = cv.distanceTransform(thr, cv.DIST_L2, cv.DIST_MASK_3)
dist_norm = cv.convertScaleAbs(dist)
cv.imshow("gray", gray)
cv.imshow("thr", thr)
cv.imshow("dist", dist_norm)
cv.waitKey()
cv.destroyAllWindows()
#实现Log-polar变换
center = (w/2, h/2)
maxRadius = 0.7*min(center)
M = w/cv.log(maxRadius)
print(maxRadius, M[0])
log_polar = cv.logPolar(img, center, M[0]*0.8, cv.INTER_LINEAR + cv.WARP_FILL_OUTLIERS)
cv.imshow("logpolar", log_polar)
#显示灰度直方图
plt.hist(gray.ravel(), 256, [0,256])
plt.show()
#均衡化
equa = cv.equalizeHist(gray)
cv.imshow("equalized image", equa)
cv.waitKey()
cv.destroyAllWindows()
#Hough变换
edge = cv.Canny(thr, 50, 150)
disp_edge = cv.cvtColor(edge, cv.COLOR_GRAY2BGR)
lines = cv.HoughLinesP(edge, 1, 1*np.pi/180, 10)
for line in lines:
for x1, y1, x2, y2 in line:
#画出直线
cv.line(disp_edge, (x1,y1), (x2,y2), (0, 255, 0), 1)
pass
print("Line count: ", len(lines))
cv.imshow("edge", edge)
cv.imshow("draw_line", disp_edge)
cv.waitKey()
cv.destroyAllWindows()
效果图: