参加了一个比赛,最近在弄openMv识别目标色块部分,主要特定颜色的矩形,而矩形里有一个特定颜色的圆或者三角形。经过调试,圆是最好识别出来的,但问题出在矩形,我想查找红色的矩形,但识别蓝色的矩形它也同样进入程序进行识别。
import sensor
import image
import time
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.set_auto_gain(False) # 关闭自动自动增益。默认开启的。
sensor.set_auto_whitebal(False) #关闭白平衡。在颜色识别中,一定要关闭白平衡
clock = time.clock()
blue_threshold = [(0, 100, -128, -20, 0, 50)]
yellow_threshold = [(70, 90, -10, 127, 0, 50)]
min_rect_width = 5 # 矩形最小宽度
min_rect_height = 5 # 矩形最小高度
def find_red_rectangles(img):
for blob in img.find_blobs([(100, 0, 0, 50, 0, 0)], pixels_threshold=200, area_threshold=1000,
merge=True): # 检测红色颜色的斑块
if blob.is_rectangle(): # 检测斑块是否为矩形
img.draw_rectangle(blob.rect(), color=(255, 0, 0), thickness=2) # 绘制检测到的矩形
print("检测到红色矩形")
while True:
clock.tick()
img = sensor.snapshot()
min_rect_area = 1000 # 设置最小矩形面积
find_red_rectangles(img)
# 查找蓝色矩形
blue_rects = img.find_rects(thresholds=blue_threshold, min_width=min_rect_width,min_height=min_rect_height)
if blue_rects:
#print("bbbbbbbbb")
for rect in blue_rects:
x, y, w, h = rect.rect()
# 在矩形区域内查找黄色圆形
roi = img.crop((x, y, w, h))
img.draw_rectangle(rect.rect(), color=(255, 0, 0), thickness=2) # 绘制检测到的矩形
yellow_circles = roi.find_circles(
thresholds=yellow_threshold, x_margin=10, y_margin=10, r_margin=10,
r_min=10, r_max=30, robust=True)
if not yellow_circles:
print("没有识别到黄色圆形")
continue
else:# 绘制识别结果
print("yyyyyy")
for c in yellow_circles:
roi.draw_circle(c.x(), c.y(), c.r(), color=(255, 255, 0)) # 绘制黄色圆形
# 输出图像
#img.draw_string(0, 0, "Blue Rect and Yellow Circle", color=(0, 255, 255), scale=2)