### 问题一

• 可以通过霍夫变换检测圆来提取到
• 可以通过二值图像分析来提取 + 轮廓分析来提取到这些点

### 代码

``````src = cv.imread("D:/images/zsxq/zsxq_01.jpg")
cv.imshow("input", src)

# 二值化处理
gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
se = cv.getStructuringElement(cv.MORPH_RECT, (3, 3))
ret, binary = cv.threshold(dst, 0, 255, cv.THRESH_OTSU | cv.THRESH_BINARY)

# 形态学处理
se = cv.getStructuringElement(cv.MORPH_ELLIPSE, (5, 5))
binary = cv.morphologyEx(binary, cv.MORPH_OPEN, se)
se = cv.getStructuringElement(cv.MORPH_ELLIPSE, (10, 10))
binary = cv.morphologyEx(binary, cv.MORPH_CLOSE, se)
cv.imshow("binary", binary)

# 轮廓分析
contours, hireachy = cv.findContours(binary, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
result = np.zeros_like(binary)
total = 0
for cnt in range(len(contours)):
area = cv.contourArea(contours[cnt])
if area < 55:
continue
rrt = cv.minAreaRect(contours[cnt])
cx, cy = rrt[0]
cv.circle(result, (np.int32(cx), np.int32(cy)), 5, (255), -1)
total += 1

# 几何纠偏
h, w = result.shape
pts = []
for row in range(h):
for col in range(w):
pv = result[row, col]
if pv == 255:
pts.append((col, row))

pts = np.array(pts)
rrt = cv.minAreaRect(pts)
print(rrt)
M = cv.getRotationMatrix2D(((w-1)/2.0,(h-1)/2.0),rrt[2],1)
dst = cv.warpAffine(result,M,(w,h))
src = cv.warpAffine(src,M,(w,h))

# Y方向投影
tbins = y_split(dst)

# X 方向投影
print("y-step", tbins)
for i in range(0, len(tbins), 1):
if i == 0:
roi = dst[0:tbins[i], 0:w]
src_roi = src[0:tbins[i], 0:w, :]
x_projection(roi, src_roi)
cv.imshow("roi", roi)
cv.waitKey(0)
if i == len(tbins)-1:
roi = dst[tbins[i]:h-1, 0:w]
src_roi = src[tbins[i]:h-1, 0:w, :]
x_projection(roi, src_roi)
cv.imshow("roi", roi)
cv.waitKey(0)
if 0 < i < (len(tbins)-1):
roi = dst[tbins[i-1]:tbins[i] - 1, 0:w]
src_roi = src[tbins[i-1]:tbins[i] - 1, 0:w,:]
x_projection(roi, src_roi)
cv.imshow("roi", roi)
cv.waitKey(0)

# 显示结果
cv.imshow("result", result)
cv.imshow("dst", dst)
cv.putText(src, "numbers: " + str(total), (50, 50), cv.FONT_HERSHEY_SIMPLEX, 1.0, (0, 0, 255), 2)
cv.imshow("detection", src)

cv.waitKey(0)
cv.destroyAllWindows()
``````

``````import cv2 as cv
import numpy as np

cv.imshow("input", src)
src = cv.GaussianBlur(src, (3, 3), 0)
gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
edge = cv.Canny(src, 50, 100)

se = cv.getStructuringElement(cv.MORPH_ELLIPSE, (10, 10))
binary = cv.morphologyEx(edge, cv.MORPH_CLOSE, se)
contours, hireachy = cv.findContours(binary, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
count = 0
for cnt in range(len(contours)):
area = cv.contourArea(contours[cnt])
if area < 100:
continue
count += 1
rrt = cv.minAreaRect(contours[cnt])

# rotated rectangle
box = cv.boxPoints(rrt)
box = np.intp(box)
cv.drawContours(src, [box], 0, (255, 0, 0), 2)

cv.imshow("binary", binary)
cv.imshow("result", src)
cv.waitKey(0)
cv.destroyAllWindows()``````

OpenCV4图像处理与视频分析 - 实战教程