问题就出在
for line in lines:
这句话上
请问我的代码运行时为什么返回如标题错误
import sensor, image, time, lcd, gc
sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.CIF)
sensor.set_windowing([0,0,136,200])
sensor.skip_frames(time = 2000)
clock = time.clock()
#==========================寻找离视野中心最近的直线函数================
def find_theline(lines):
min_dis = 100
for line in lines:
if (abs(line.x1() - img.width()/2) < min_dis):
the_line = line
min_dis = abs(line.x1() - img.width()/2)
return the_line
#=================================================================
#=============================滤波参数==============================
kernel_size = 1
kernel = [-1, -1, -1,\
-1, +8, -1,\
-1, -1, -1]
thresholds = [(0, 36)]
#=================================================================
while(True):
clock.tick()
img = sensor.snapshot().lens_corr(strength = 1.0,zoom = 1)
img.morph(kernel_size, kernel)
img.binary(thresholds)
img.erode(1, threshold = 2)
lines = img.get_regression([(0,36)], robust = True)
if (lines):
theline = find_theline(lines)
if(theline.magnitude() > 8):
rho_err = abs(theline.rho())-img.width()/2
print("rho_error:",rho_err)
img.draw_line(theline.line())
gc.collect()
print("fps:",clock.fps())
请问openmv能不能实现在特定区域进行图像滤波,例如在特定区域进行膨胀滤波,不使用sensor.set_windowing,因为涉及lcd显示。
使用官方例程,我只是加入了lcd.init和lcd.display两句函数,但是学习完颜色阈值后,lcd上并未显示绘制出的矩形
# Automatic RGB565 Color Tracking Example
#
# This example shows off single color automatic RGB565 color tracking using the OpenMV Cam.
import sensor, image, time, lcd
print("Letting auto algorithms run. Don't put anything in front of the camera!")
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False) # must be turned off for color tracking
sensor.set_auto_whitebal(False) # must be turned off for color tracking
clock = time.clock()
lcd.init()
# Capture the color thresholds for whatever was in the center of the image.
r = [(320//2)-(50//2), (240//2)-(50//2), 50, 50] # 50x50 center of QVGA.
print("Auto algorithms done. Hold the object you want to track in front of the camera in the box.")
print("MAKE SURE THE COLOR OF THE OBJECT YOU WANT TO TRACK IS FULLY ENCLOSED BY THE BOX!")
for i in range(60):
img = sensor.snapshot()
img.draw_rectangle(r)
print("Learning thresholds...")
threshold = [50, 50, 0, 0, 0, 0] # Middle L, A, B values.
for i in range(60):
img = sensor.snapshot()
hist = img.get_histogram(roi=r)
lo = hist.get_percentile(0.01) # Get the CDF of the histogram at the 1% range (ADJUST AS NECESSARY)!
hi = hist.get_percentile(0.99) # Get the CDF of the histogram at the 99% range (ADJUST AS NECESSARY)!
# Average in percentile values.
threshold[0] = (threshold[0] + lo.l_value()) // 2
threshold[1] = (threshold[1] + hi.l_value()) // 2
threshold[2] = (threshold[2] + lo.a_value()) // 2
threshold[3] = (threshold[3] + hi.a_value()) // 2
threshold[4] = (threshold[4] + lo.b_value()) // 2
threshold[5] = (threshold[5] + hi.b_value()) // 2
for blob in img.find_blobs([threshold], pixels_threshold=100, area_threshold=100, merge=True, margin=10):
img.draw_rectangle(blob.rect())
img.draw_cross(blob.cx(), blob.cy())
img.draw_rectangle(r)
print("Thresholds learned...")
print("Tracking colors...")
while(True):
clock.tick()
img = sensor.snapshot()
for blob in img.find_blobs([threshold], pixels_threshold=100, area_threshold=100, merge=True, margin=10):
img.draw_rectangle(blob.rect())
img.draw_cross(blob.cx(), blob.cy())
lcd.display(sensor.snapshot())
#print(clock.fps())
而且我的LCD大小为240*280,使用QVGA的分辨率LCD显示会有缺失
当我把图像分辨率设置为VGA并img.set_windowing(240,280)时,image.draw_rectangle又开始报如标题的错误,请问我该如何修改代码?