import sensor, image, time
Initialize the camera
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time=2000)
width = sensor.width()
height = sensor.height()
Calculate the coordinates of the center ROI
Threshold for rectangular shape
threshold = ((42, 96, -31, 12, -20, 20))
def find_max(blobs):
max_size=0
for blob in blobs:
if blob[2]*blob[3] > max_size :
max_blob=blob
max_size = blob[2]*blob[3]
return max_blob
while True:
clock = time.clock()
clock.tick()
# Capture a snapshot from the camera
img = sensor.snapshot()
img.lens_corr(strength=1.8, zoom=1.0)
# Find blobs that match the rectangular shape
blobs = img.find_blobs([threshold], pixels_threshold=200, area_threshold=300, merge=True)
max_blob=find_max(blobs)
if max_blob:
for r in img.find_rects(threshold = 10000):
img.draw_rectangle(r.rect(), color = (255, 0, 0))
# Get the four corners' coordinates of the detected shape
x, y, w, h = r.rect()
x1, y1 = x, y
x2, y2 = x + w, y
x3, y3 = x, y + h
x4, y4 = x + w, y + h
# Draw the four corners as crosses
img.draw_cross(x1, y1, color=(255, 0, 0))
img.draw_cross(x2, y2, color=(255, 0, 0))
img.draw_cross(x3, y3, color=(255, 0, 0))
img.draw_cross(x4, y4, color=(255, 0, 0))
# Print the coordinates
print("Corner 1: ({}, {})".format(x1, y1))
print("Corner 2: ({}, {})".format(x2, y2))
print("Corner 3: ({}, {})".format(x3, y3))
print("Corner 4: ({}, {})".format(x4, y4))