# 寻找最大的矩形
# 沿矩形边框绘制线条
import sensor, image, time
# 相机初始化
sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE) # 设置图像格式为灰度
sensor.set_framesize(sensor.QQQVGA) # 设置图像大小
sensor.skip_frames(time=2000) # 等待设置生效
clock = time.clock() # 用于跟踪帧率
while(True):
clock.tick()
img = sensor.snapshot().lens_corr(1.8)
# 使用find_rects()方法寻找图像中的矩形
rects = img.find_rects(threshold = 10000)
# 初始化变量来存储最大矩形的信息
max_area = 0
max_rect = None
# 遍历所有找到的矩形,找出面积最大的矩形
for rect in rects:
# 计算当前矩形的面积
area = rect.w() * rect.h()
# 如果当前矩形的面积大于之前记录的最大面积,则更新最大矩形和最大面积
if area > max_area:
max_area = area
max_rect = rect
# 如果找到了最大的矩形,则绘制它的边框
if max_rect:
corners = max_rect.corners() # 获取最大矩形的四个角点
# 绘制最大矩形的四条边
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)
print(corners)
https://singtown.com/learn/50001/
使用这个例程之后,移动物体与摄像头之间的距离,串口返回的数值不变的,这是为什么?
# Measure the distance
#
# This example shows off how to measure the distance through the size in imgage
# This example in particular looks for yellow pingpong ball.
import sensor, image, time
# For color tracking to work really well you should ideally be in a very, very,
# very, controlled enviroment where the lighting is constant...
yellow_threshold = (33, 96, -34, 42, -13, 28)
# You may need to tweak the above settings for tracking green things...
# Select an area in the Framebuffer to copy the color settings.
sensor.reset() # Initialize the camera sensor.
sensor.set_pixformat(sensor.RGB565) # use RGB565.
sensor.set_framesize(sensor.QQVGA) # use QQVGA for speed.
sensor.skip_frames(10) # Let new settings take affect.
sensor.set_auto_whitebal(False) # turn this off.
clock = time.clock() # Tracks FPS.
K=3920 #the value should be measured
while(True):
clock.tick() # Track elapsed milliseconds between snapshots().
img = sensor.snapshot() # Take a picture and return the image.
blobs = img.find_blobs([yellow_threshold])
if len(blobs) == 1:
# Draw a rect around the blob.
b = blobs[0]
img.draw_rectangle(b[0:4]) # rect
img.draw_cross(b[5], b[6]) # cx, cy
Lm = (b[2]+b[3])/2
length = K/Lm
print(length)
#print(Lm)
#print(clock.fps()) # Note: Your OpenMV Cam runs about half as fast while
# connected to your computer. The FPS should increase once disconnected.请在这里粘贴代码