报错AttributeError: 'Image' object has no attribute 'to_hsv'
-
import sensor, image, time # 初始化摄像头 sensor.reset() sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QVGA) sensor.skip_frames(time = 2000) # 设置颜色阈值,以分离污水和纯净水 green_threshold = (0, 80, -70, -10, 0, 30) blue_threshold = (0, 30, -10, 10, -50, 0) # 设置形态学操作的内核大小和迭代次数 kernel_size = 3 morph_iterations = 2 while(True): # 捕获图像 img = sensor.snapshot() # 将图像转换为HSV颜色空间 img_hsv = img.to_hsv() #一直报错 # 分离污水和纯净水的颜色通道 img_green = img_hsv.channel(1) img_blue = img_hsv.channel(2) # 对颜色通道应用颜色阈值 img_green_binary = img_green.binary([green_threshold]) img_blue_binary = img_blue.binary([blue_threshold]) # 对二值化图像应用形态学操作,以去除噪点和进一步分离污水和纯净水 kernel = image.get_kernel(kernel_size, kernel_size) img_green_binary = img_green_binary.erode(kernel, iterations=morph_iterations) img_green_binary = img_green_binary.dilate(kernel, iterations=morph_iterations) img_blue_binary = img_blue_binary.erode(kernel, iterations=morph_iterations) img_blue_binary = img_blue_binary.dilate(kernel, iterations=morph_iterations) # 计算污水和纯净水的像素数 green_pixels = img_green_binary.count(True) blue_pixels = img_blue_binary.count(True) # 计算污水和纯净水的比例 water_ratio = green_pixels / (green_pixels + blue_pixels) # 打印污水和纯净水的比例 print("Water ratio: %f" % water_ratio) # 暂停一段时间,以允许下一次循环 time.sleep(100)
-
没有这个函数,没有这个功能。