
import time, sensor, image, math
from image import SEARCH_EX, SEARCH_DS
#从imgae模块引入SEARCH_EX和SEARCH_DS。使用from import仅仅引入SEARCH_EX,
#SEARCH_DS两个需要的部分,而不把image模块全部引入。
# 重置传感器
sensor.reset()
# 设置传感器
sensor.set_contrast(1)
sensor.set_gainceiling(16)
# Max resolution for template matching with SEARCH_EX is QQVGA
# 模板与SEARCH_EX匹配的最大分辨率是QQVGA
sensor.set_framesize(sensor.QQVGA)
# 你可以设置windowing窗口来减少搜索图片。
#sensor.set_windowing(((640-80)//2, (480-60)//2, 80, 60))
sensor.set_pixformat(sensor.GRAYSCALE)
# 加载模板。
# 模板应该是一个小的(例如。32x32像素)灰度图像。
#img = image.Image("/~~0.jpg")
clock = time.clock()
while(True):
clock.tick() # Update the FPS clock.
for v in range(0,4):
img = image.Image("/~~0.jpg",copy_to_fb = True)
#img = sensor.snapshot()
#img = image.Image("/~~0.jpg")
#histogram = img.get_histogram()
#stastistics = histogram.get_stastistics()
x = img.width()
y = img.height()
N = x*y
H = 0
for z in range(0,256):
s = 0
for i in range(0,x):
for j in range(0,y):
print(img.get_pixel(i,j))
if img.get_pixel(i,j) == z :
s += 1
if s != 0:
p = float(s/N)
k = math.log10(p)
#print(k)
H += float(-(p*math.log10(p)))
print(H)jpg是压缩图像,不能操作。
解决办法:用bmp。