# Find Rects Example
#
# This example shows off how to find rectangles in the image using the quad threshold
# detection code from our April Tags code. The quad threshold detection algorithm
# detects rectangles in an extremely robust way and is much better than Hough
# Transform based methods. For example, it can still detect rectangles even when lens
# distortion causes those rectangles to look bent. Rounded rectangles are no problem!
# (But, given this the code will also detect small radius circles too)...
import sensor, image, time, lcd
threshold_index = 0 # 0 for red, 1 for green, 2 for blue
# Color Tracking Thresholds (L Min, L Max, A Min, A Max, B Min, B Max)
# The below thresholds track in general red/green/blue things. You may wish to tune them...
thresholds = [(21, 28, -14, -2, -11, 11),# generic_black_thresholds
(64, 73, 31, 52, 14, 39), # generic_red_thresholds
(30, 100, -64, -8, -32, 32), # generic_green_thresholds
(98, 100, -6, 5, -5, 5),# generic_white_thresholds
(0, 30, 0, 64, -128, 0)] # generic_blue_thresholds
lcd.init() # Initialize the lcd screen.
sensor.reset()
sensor.set_pixformat(sensor.RGB565) # grayscale is faster (160x120 max on OpenMV-M7)
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(time = 2000)
clock = time.clock()
sensor.set_auto_gain(False) # must be turned off for color tracking
sensor.set_auto_whitebal(False) # must be turned off for color tracking
while(True):
clock.tick()
img = sensor.snapshot()
# `threshold` below should be set to a high enough value to filter out noise
# rectangles detected in the image which have low edge magnitudes. Rectangles
# have larger edge magnitudes the larger and more contrasty they are...
img.draw_cross(80, 60)
for c in img.find_circles(threshold = 3900, x_margin = 10, y_margin = 10, r_margin = 10):
circle_roi = [c.x()-c.r(),c.y()-c.r(),c.x()+c.r(),c.y()+c.r()]
#for blob in img.find_blobs([thresholds[1]], roi=circle_roi, pixels_threshold=200, area_threshold=200, merge=True):
img.draw_circle(c.x(), c.y(), c.r(), color = (255, 0, 0))
img.draw_cross(c.x(), c.y())
img.draw_string(90,80,'circle',color=(0,0,0))
img.draw_string(90,90,'r=' + str(c.r()),color=(0,0,0))
img.draw_string(90,100,'x=' + str(c.x()-80),color=(0,0,0))
img.draw_string(90,110,'y=' + str(-c.y()+60),color=(0,0,0))
for r in img.find_rects(threshold = 15000):
if (r.w()-r.h())<10 and (r.w()-r.h())>-10 :
img.draw_rectangle(r.rect(), color = (255, 0, 0))
for p in r.corners(): img.draw_circle(p[0], p[1], 5, color = (0, 255, 0))
img.draw_string(20,80,'square',color=(0,0,0))
img.draw_string(20,90,'a=' + str((r.h()+r.w())/2),color=(0,0,0))
img.draw_string(20,100,'x=' + str(-r.x()+60),color=(0,0,0))
img.draw_string(20,110,'y=' + str(-r.y()+40),color=(0,0,0))
print(r)
for blob in img.find_blobs([thresholds[3]], pixels_threshold=200, area_threshold=200, merge=True):
img.draw_circle(blob.cx(), blob.cy(), int((blob.h()+blob.w())/4), color = (0, 255, 0))
img.draw_string(20,10,'led',color=(0,0,0))
img.draw_string(20,20,'x=' + str(+blob.cx()-80),color=(0,0,0))
img.draw_string(20,30,'y=' + str(-blob.cy()+60),color=(0,0,0))
lcd.display(img)
print("FPS %f" % clock.fps())
R
rdzg
@rdzg
0
声望
4
楼层
1233
资料浏览
0
粉丝
0
关注
rdzg 发布的帖子
-
RE: 图像太亮怎么办