import cv2 import glob import numpy as np imgs = glob.glob("maze.png") res, L, N = [], 256, 5 for i in imgs: img = cv2.imread(i) img = cv2.resize(img, (512, 512)) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) _, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY_INV) # contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) max_contour = max(contours, key=cv2.contourArea) epsilon = 0.1 * cv2.arcLength(max_contour, True) approx = cv2.approxPolyDP(max_contour, epsilon, True) circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, 20, param1=50, param2=15, minRadius=5, maxRadius=15) if circles is not None: circles = np.round(circles[0, :]).astype("int") for (x, y, r) in circles: cv2.circle(img, (x, y), r, (0, 0, 255), 2) # edges = cv2.Canny(gray, 100, 200) contours, hierarchy = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) for contour in contours: center, (width, height), angle = cv2.minAreaRect(contour) if -5 <= (width - height) <= 5 and 30 <= width <= 50: cv2.drawContours(img, [contour], -1, (0, 0, 255), 3) res.append(cv2.resize(img, (L, L))) resImg = np.zeros((L * N, L * N, 3), dtype=np.uint8) for i, img in enumerate(res): row, col = i // N, i % N x, y = col * L, row * L resImg[y:y + L, x:x + L] = img cv2.imshow("", resImg) cv2.waitKey(0) cv2.destroyAllWindows()帮我加上详细注释
时间: 2023-12-15 12:05:52 浏览: 160
vite vue3 import.meta.glob动态路由
好的,以下是添加了详细注释的代码:
```python
import cv2
import glob
import numpy as np
# 获取所有maze.png图片的路径
imgs = glob.glob("maze.png")
# 定义每个小图像的尺寸和每行、每列的小图像数量
res, L, N = [], 256, 5
for i in imgs:
# 读取图片并调整大小
img = cv2.imread(i)
img = cv2.resize(img, (512, 512))
# 将图片转化为灰度图,并进行二值化处理
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY_INV)
# 找到图像中的所有轮廓,并选择面积最大的轮廓进行逼近处理
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
max_contour = max(contours, key=cv2.contourArea)
epsilon = 0.1 * cv2.arcLength(max_contour, True)
approx = cv2.approxPolyDP(max_contour, epsilon, True)
# 检测图像中的圆形,并在圆形周围画出红色圆圈
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, 20, param1=50, param2=15, minRadius=5, maxRadius=15)
if circles is not None:
circles = np.round(circles[0, :]).astype("int")
for (x, y, r) in circles:
cv2.circle(img, (x, y), r, (0, 0, 255), 2)
# 进行边缘检测,并再次找到图像中的所有轮廓
edges = cv2.Canny(gray, 100, 200)
contours, hierarchy = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for contour in contours:
center, (width, height), angle = cv2.minAreaRect(contour)
# 判断最小外接矩形的长宽比是否在一定范围内,如果满足条件,则在轮廓周围画出红色矩形
if -5 <= (width - height) <= 5 and 30 <= width <= 50:
cv2.drawContours(img, [contour], -1, (0, 0, 255), 3)
# 将处理后的图片添加进一个列表中
res.append(cv2.resize(img, (L, L)))
# 将所有图片通过numpy模块拼接成一个大图展示
resImg = np.zeros((L * N, L * N, 3), dtype=np.uint8)
for i, img in enumerate(res):
row, col = i // N, i % N
x, y = col * L, row * L
resImg[y:y + L, x:x + L] = img
cv2.imshow("", resImg)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
希望这份注释对您有所帮助!
阅读全文