import sensor
import image
import time
import math
# 初始化摄像头
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(time=2000)
clock = time.clock()
# 检测黄色的颜色阈值
yellow_threshold = (35, 90, -20, 60, 40, 100)
# 插值函数,用于计算每个格子的角点
def interpolate(p1, p2, factor):
return (p1[0] + (p2[0] - p1[0]) * factor, p1[1] + (p2[1] - p1[1]) * factor)
find_blobs = True
# 主循环
while True:
clock.tick()
img = sensor.snapshot().lens_corr(strength=1.0, zoom=1.0)
if find_blobs:
# 使用颜色阈值来识别黄色区域
yellow_blobs = img.find_blobs([yellow_threshold], pixels_threshold=3000, area_threshold=3000, merge=True)
max_area = 0
max_rect = None
# 遍历所有黄色区域
for blob in yellow_blobs:
# 在黄色区域内寻找矩形
rects = img.find_rects(roi=blob.rect(), threshold=5000)
for rect in rects:
area = rect.w() * rect.h()
aspect_ratio = rect.w() / rect.h()
# 判断面积和长宽比是否符合条件
if 0.8 < aspect_ratio < 1.2 and area > max_area and 50 < rect.w() < 100 and 50 < rect.h() < 100:
max_area = area
max_rect = rect
# 如果找到符合条件的最大矩形
if max_rect:
corners = max_rect.corners()
angle = -math.degrees(math.atan2(corners[1][1] - corners[0][1], corners[1][0] - corners[0][0]))
if -45 < angle < 45:
print("矩形旋转角度:", angle)
# 绘制矩形的四个角点和边框
for corner in corners:
img.draw_circle(corner[0], corner[1], 5, color=(255, 0, 0), thickness=2, fill=False)
for i in range(len(corners)):
start_point = corners[i]
end_point = corners[(i + 1) % 4]
img.draw_line(start_point[0], start_point[1], end_point[0], end_point[1], color=(255, 0, 0))
# 在棋盘上添加编号
num = 9
for row in range(3):
for col in range(3):
# 计算每个格子的四个顶点的插值
top_left = interpolate(interpolate(corners[0], corners[3], row / 3.0), interpolate(corners[1], corners[2], row / 3.0), 1 - col / 3.0)
top_right = interpolate(interpolate(corners[0], corners[3], row / 3.0), interpolate(corners[1], corners[2], row / 3.0), 1 - (col + 1) / 3.0)
bottom_left = interpolate(interpolate(corners[0], corners[3], (row + 1) / 3.0), interpolate(corners[1], corners[2], (row + 1) / 3.0), 1 - col / 3.0)
bottom_right = interpolate(interpolate(corners[0], corners[3], (row + 1) / 3.0), interpolate(corners[1], corners[2], (row + 1) / 3.0), 1 - (col + 1) / 3.0)
# 计算格子的中心坐标
center_x = int((top_left[0] + bottom_right[0]) / 2)
center_y = int((top_left[1] + bottom_right[1]) / 2)
# 在每个格子的中心位置绘制编号
img.draw_string(center_x - 10, center_y - 10, str(num), color=(255, 0, 0), scale=2)
num -= 1
Y
y4oi
@y4oi
0
声望
2
楼层
110
资料浏览
0
粉丝
0
关注
y4oi 发布的帖子
-
识别棋盘最大矩形不成功,有的时候能识别到,感觉没什么问题,但没有效果
-
怎么距离近的时候能识别到九宫格,距离一远就就识别的不精确,抬高镜头取阈值也不行,还有怎么给识别到的九宫格编号呢,谢谢。
import sensor import image import time import math # 初始化摄像头 sensor.reset() sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QVGA) sensor.skip_frames(time=2000) # 设置颜色阈值,以下是一个黄色的示例 yellow_threshold = (30, 100, -10, 50, -30, 50) # H: 30-100, S: 10-50, V: 30-50 # 创建一个时钟对象以计算FPS clock = time.clock() def draw_rotated_rect(img, center, angle, width, height, color=(255, 0, 0)): """ 绘制旋转矩形 """ # 计算矩形四个角的坐标 half_width = width / 2 half_height = height / 2 # 旋转矩形的四个角 points = [ (-half_width, -half_height), (half_width, -half_height), (half_width, half_height), (-half_width, half_height) ] # 进行旋转 rotated_points = [] for point in points: x = point[0] * math.cos(angle) - point[1] * math.sin(angle) y = point[0] * math.sin(angle) + point[1] * math.cos(angle) rotated_points.append((x + center[0], y + center[1])) # 绘制旋转后的矩形 img.draw_line(int(rotated_points[0][0]), int(rotated_points[0][1]), int(rotated_points[1][0]), int(rotated_points[1][1]), color=color) img.draw_line(int(rotated_points[1][0]), int(rotated_points[1][1]), int(rotated_points[2][0]), int(rotated_points[2][1]), color=color) img.draw_line(int(rotated_points[2][0]), int(rotated_points[2][1]), int(rotated_points[3][0]), int(rotated_points[3][1]), color=color) img.draw_line(int(rotated_points[3][0]), int(rotated_points[3][1]), int(rotated_points[0][0]), int(rotated_points[0][1]), color=color) while True: clock.tick() img = sensor.snapshot() # 查找黄色色块 blobs = img.find_blobs([yellow_threshold], pixels_threshold=200, area_threshold=200, merge=True) # 检测到的小框的角度和中心点 centers = [] angles = [] rects = [] # 遍历找到的色块 for blob in blobs: # 过滤掉小的噪声 if blob.area() > 100: # 获取色块的矩形框 rect = blob.rect() rects.append(rect) # 判断是否为正方形 if abs(rect[2] - rect[3]) < 10: # 允许宽高差异小于10像素 # 计算中心点和旋转角度 center = (blob.cx(), blob.cy()) angle = math.atan2(rect[3], rect[2]) # 计算旋转角度 centers.append(center) angles.append(angle) # 绘制九宫格小框 img.draw_rectangle(rect) # 如果检测到了九个小框 if len(centers) == 9: # 计算九个小框的外接矩形 min_x = min([rect[0] for rect in rects]) min_y = min([rect[1] for rect in rects]) max_x = max([rect[0] + rect[2] for rect in rects]) max_y = max([rect[1] + rect[3] for rect in rects]) # 计算大矩形的中心点和角度 center_x = (min_x + max_x) // 2 center_y = (min_y + max_y) // 2 avg_angle = sum(angles) / len(angles) # 计算大矩形的宽度和高度 width = max_x - min_x height = max_y - min_y # 绘制旋转后的大矩形 draw_rotated_rect(img, (center_x, center_y), avg_angle, width, height, color=(0, 255, 0)) print("FPS: {}".format(clock.fps()))
请在这里粘贴代码