
import sensor, image, time
sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.VGA)
sensor.set_windowing((240, 240)) # look at center 240x240 pixels of the VGA resolution.
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False) # must turn this off to prevent image washout...
clock = time.clock()
sum = 0
max = 0
while(True):
clock.tick()
img = sensor.snapshot() #得到图像
for i in range(40, 180, 3):
for x in range(0, 640, 10):
for y in range(0, 480, 10):
color = img.get_pixel(x, y) #获取图像二值化的像素值
value = abs(color - i)
square = pow (value ,2)
sum += square #计算取到什么阈值时方差值相差最大
if(sum > max):
max = sum #保留最大方差
j = i #保留阈值
img.median(1, percentile=0.5) #中值滤波
img.laplacian(1, sharpen=True) #锐化图像
high_threshold = (j, 255) #二维码阈值
img.binary([high_threshold]) #二值化
print(j) #打印阈值
for code in img.find_qrcodes(): #识别二维码
img.draw_rectangle(code.rect(), color = 127)
print(code) #打印数据
print(clock.fps())像素超出范围了,不可能有640,480的。
哦哦哦哦 发现了 十分感谢