阅读更多
# -*-encoding:utf-8-*-
import pytesseract
from PIL import Image
from PIL import ImageFilter
from PIL import ImageFont
from PIL import ImageDraw
import numpy as np
from PIL import Image
import cv2
import matplotlib.pyplot as plt
def main():
# 使用霍夫变换识别出图像中的直线和圆
# 分别使用霍夫线变换和圆变换检测图像中的直线和圆
# OpenCV函数:cv2.HoughLines(), cv2.HoughLinesP(), cv2.HoughCircles()
# =================================霍夫直线变换
# 1.加载图片,转为二值图
img = cv2.imread('test007.png')
# img = cv2.imread('shapes.jpg')
# drawing = np.zeros(img.shape[:], dtype=np.uint8)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 50, 150)
# 2.霍夫直线变换
lines = cv2.HoughLines(edges, 0.8, np.pi / 180, 90)
# 参数1:要检测的二值图(一般是阈值分割或边缘检测后的图)
# 参数2:距离r的精度,值越大,考虑越多的线
# 参数3:角度θ的精度,值越小,考虑越多的线
# 参数4:累加数阈值,值越小,考虑越多的线
# 3.将检测的线画出来(注意是极坐标噢)
drawing = np.zeros(img.shape[:], dtype=np.uint8)
for line in lines:
rho, theta = line[0]
a = np.cos(theta)
b = np.sin(theta)
x0 = a * rho
y0 = b * rho
x1 = int(x0 + 1000 * (-b))
y1 = int(y0 + 1000 * (a))
x2 = int(x0 - 1000 * (-b))
y2 = int(y0 - 1000 * (a))
cv2.line(drawing, (x1, y1), (x2, y2), (0, 0, 255))
# =================================统计概率霍夫直线变换
# 前面的方法又称为标准霍夫变换,它会计算图像中的每一个点,计算量比较大,另外它得到的是整一条线(r和θ),并不知道原图中直线的端点。
# 所以提出了统计概率霍夫直线变换(Probabilistic Transform),是一种改进的霍夫变换:
# 3.统计概率霍夫线变换
lines = cv2.HoughLinesP(edges, 0.8, np.pi /180, 50,
minLineLength=20, maxLineGap=10)
# 前面几个参数跟之前的一样,有两个可选参数:
# minLineLength:最短长度阈值,比这个长度短的线会被排除
# maxLineGap:同一直线两点之间的最大距离
#得到的结果是起始坐标点
# 3.将检测的线画出来
drawing2 = np.zeros(img.shape[:], dtype=np.uint8)
for line in lines:
x1, y1, x2, y2 = line[0]
cv2.line(drawing2, (x1, y1), (x2, y2), (0, 255, 0), 1, lineType=cv2.LINE_AA)
# cv2.LINE_AA在之前绘图功能中讲解过,表示抗锯齿线型。
# =================================统计概率霍夫直线变换
# 圆是用(x_center, y_center, r)来表示
# 2.霍夫圆变换
circles = cv2.HoughCircles(edges, cv2.HOUGH_GRADIENT, 1, 20, param2=75)
circles = np.int0(np.around(circles)) #取约数
print(circles) #[[[252 278 45]]]
# 参数2:变换方法,一般使用霍夫梯度法,详情:HoughModes
# 参数3:dp = 1:表示霍夫梯度法中累加器图像的分辨率与原图一致
# 参数4:两个不同圆圆心的最短距离
# 参数5:param2跟霍夫直线变换中的累加数阈值一样
# 将检测的圆画出来
drawing3 = np.zeros(img.shape[:], dtype=np.uint8)
for i in circles[0, :]:
cv2.circle(drawing3, (i[0], i[1]), i[2], (0, 255, 0), 2) # 画出外圆
cv2.circle(drawing3, (i[0], i[1]), 2, (0, 0, 255), 3) # 画出圆心
# cv2.imshow('equalization', equ) # 并排显示
cv2.imshow('gray', gray) #
cv2.imshow('edges', edges) #
cv2.imshow('drawing', drawing) #
cv2.imshow('drawing2', drawing2) #
cv2.imshow('drawing3', drawing3) #
cv2.waitKey(0)
if __name__ == '__main__':
main()