植物图像NDVI计算报错TypeError:仅尺寸为1的数组可转为Python标量
问题描述
运行多年前可用的植物图像NDVI计算代码时,执行cv2.applyColorMap(color_mapped_prep, fastiecm)触发错误:TypeError: only size-1 arrays can be converted to Python scalars,尝试过map()、list()、np.vectorized及LUT方案均未解决。
原代码如下:
import cv2 import numpy as np from fastiecm import fastiecm from picamera import PiCamera import picamera.array cam = PiCamera() cam.rotation = 360 cam.resolution = (1920, 1088) # Uncomment if using a Pi Noir camera stream = picamera.array.PiRGBArray(cam) cam.capture(stream, format='bgr', use_video_port=True) original = stream.array def display(image, image_name): image = np.array(image, dtype=float)/float(255) shape = image.shape height = int(shape[0] / 2) width = int(shape[1] / 2) image = cv2.resize(image, (width, height)) cv2.namedWindow(image_name) cv2.imshow(image_name, image) cv2.waitKey(0) cv2.destroyAllWindows() def contrast_stretch(im): in_min = np.percentile(im, 5) in_max = np.percentile(im, 55) out_min = 0.0 out_max = 255.0 out = im - in_min out *= ((out_min - out_max) / (in_min - in_max)) out += in_min return out def calc_ndvi(image): b, g, r = cv2.split(image) bottom = (r.astype(float) + b.astype(float)) bottom[bottom==0] = 0.01 ndvi = (r.astype(float) - b) / bottom return ndvi display(original, 'Original') contrasted = contrast_stretch(original) display(contrasted, 'Contrasted original') cv2.imwrite('contrasted.png', contrasted) ndvi = calc_ndvi(contrasted) display(ndvi, 'NDVI') ndvi_contrasted = contrast_stretch(ndvi) display(ndvi_contrasted, 'NDVI Contrasted') cv2.imwrite('ndvi_contrasted.png', ndvi_contrasted) color_mapped_prep = ndvi_contrasted.astype(np.uint8) color_mapped_image = cv2.applyColorMap(color_mapped_prep, fastiecm) display(color_mapped_image, 'Color mapped') cv2.imwrite('color_mapped_image.png', color_mapped_image) cv2.imwrite('original.png', original)
问题原因
cv2.applyColorMap对第二个参数有严格要求:要么是OpenCV内置的颜色映射常量(如cv2.COLORMAP_JET),要么是256行×3列的uint8类型数组(自定义颜色表)。报错的核心原因是:
- 导入的
fastiecm不符合上述格式(维度错误、数据类型非uint8) - 原
contrast_stretch函数公式错误,导致ndvi_contrasted数值超出0-255范围,转uint8时溢出
解决方案
1. 修复fastiecm颜色表
确保fastiecm是256×3的uint8数组,可直接替换为标准FastieCM颜色表(将以下代码替换原from fastiecm import fastiecm行):
# 标准FastieCM颜色表(256×3 uint8) fastiecm = np.array([ [0, 0, 0], [0, 0, 9], [0, 0, 17], [0, 0, 26], [0, 0, 34], [0, 0, 43], [0, 0, 51], [0, 0, 60], [0, 0, 68], [0, 0, 77], [0, 0, 85], [0, 0, 94], [0, 0, 102], [0, 0, 111], [0, 0, 119], [0, 0, 128], [0, 1, 136], [0, 2, 145], [0, 3, 153], [0, 4, 162], [0, 6, 170], [0, 7, 179], [0, 8, 187], [0, 9, 196], [0, 10, 204], [0, 12, 213], [0, 13, 221], [0, 14, 230], [0, 15, 238], [0, 17, 247], [0, 18, 255], [0, 20, 255], [0, 21, 255], [0, 23, 255], [0, 24, 255], [0, 26, 255], [0, 27, 255], [0, 29, 255], [0, 30, 255], [0, 32, 255], [0, 33, 255], [0, 35, 255], [0, 36, 255], [0, 38, 255], [0, 39, 255], [0, 41, 255], [0, 42, 255], [0, 44, 255], [0, 46, 255], [0, 47, 255], [0, 49, 255], [0, 50, 255], [0, 52, 255], [0, 53, 255], [0, 55, 255], [0, 57, 255], [0, 58, 255], [0, 60, 255], [0, 61, 255], [0, 63, 255], [0, 65, 255], [0, 66, 255], [0, 68, 255], [0, 69, 255], [0, 71, 255], [0, 73, 255], [0, 74, 255], [0, 76, 255], [0, 77, 255], [0, 79, 255], [0, 81, 255], [0, 82, 255], [0, 84, 255], [0, 86, 255], [0, 87, 255], [0, 89, 255], [0, 90, 255], [0, 92, 255], [0, 94, 255], [0, 95, 255], [0, 97, 255], [0, 99, 255], [0, 100, 255], [0, 102, 255], [0, 103, 255], [0, 105, 255], [0, 107, 255], [0, 108, 255], [0, 110, 255], [0, 112, 255], [0, 113, 255], [0, 115, 255], [0, 116, 255], [0, 118, 255], [0, 120, 255], [0, 121, 255], [0, 123, 255], [0, 125, 255], [0, 126, 255], [0, 128, 255], [0, 129, 255], [0, 131, 255], [0, 133, 255], [0, 134, 255], [0, 136, 255], [0, 138, 255], [0, 139, 255], [0, 141, 255], [0, 142, 255], [0, 144, 255], [0, 146, 255], [0, 147, 255], [0, 149, 255], [0, 151, 255], [0, 152, 255], [0, 154, 255], [0, 155, 255], [0, 157, 255], [0, 159, 255], [0, 160, 255], [0, 162, 255], [0, 164, 255], [0, 165, 255], [0, 167, 255], [0, 168, 255], [0, 170, 255], [0, 172, 255], [0, 173, 255], [0, 175, 255], [0, 177, 255], [0, 178, 255], [0, 180, 255], [0, 181, 255], [0, 183, 255], [0, 185, 255], [0, 186, 255], [0, 188, 255], [0, 190, 255], [0, 191, 255], [0, 193, 255], [0, 194, 255], [0, 196, 255], [0, 198, 255], [0, 199, 255], [0, 201, 255], [0, 203, 255], [0, 204, 255], [0, 206, 255], [0, 207, 255], [0, 209, 255], [0, 211, 255], [0, 212, 255], [0, 214, 255], [0, 216, 255], [0, 217, 255], [0, 219, 255], [0, 220, 255], [0, 222, 255], [0, 224, 255], [0, 225, 255], [0, 227, 255], [0, 229, 255], [0, 230, 255], [0, 232, 255], [0, 233, 255], [0, 255, 235], [0, 255, 233], [0, 255, 232], [0, 255, 230], [0, 255, 229], [0, 255, 227], [0, 255, 225], [0, 255, 224], [0, 255, 222], [0, 255, 220], [0, 255, 219], [0, 255, 217], [0, 255, 216], [0, 255, 214], [0, 255, 212], [0, 255, 211], [0, 255, 209], [0, 255, 207], [0, 255, 206], [0, 255, 204], [0, 255, 203], [0, 255, 201], [0, 255, 199], [0, 255, 198], [0, 255, 196], [0, 255, 194], [0, 255, 193], [0, 255, 191], [0, 255, 190], [0, 255, 188], [0, 255, 186], [0, 255, 185], [0, 255, 183], [0, 255, 181], [0, 255, 180], [0, 255, 178], [0, 255, 177], [0, 255, 175], [0, 255, 173], [0, 255, 172], [0, 255, 170], [0, 255, 168], [0, 255, 167], [0, 255, 165], [0, 255, 164], [0, 255, 162], [0, 255, 160], [0, 255, 159], [0, 255, 157], [0, 255, 155], [0, 255, 154], [0, 255, 152], [0, 255, 151], [0, 255, 149], [0, 255, 147], [0, 255, 146], [0, 255, 144], [0, 255, 142], [0, 255, 141], [0, 255, 139], [0, 255, 138], [0, 255, 136], [0, 255, 134], [0, 255, 133], [0, 255, 131], [0, 255, 129], [0, 255, 128], [0, 255, 126], [0, 255, 125], [0, 255, 123], [0, 255, 121], [0, 255, 120], [0, 255, 118], [0, 255, 116], [0, 255, 115], [0, 255, 113], [0, 255, 112], [0, 255, 110], [0, 255, 108], [0, 255, 107], [0, 255, 105], [0, 255, 103], [0, 255, 102], [0, 255, 100], [0, 255, 99], [0, 255, 97], [0,
相关产品推荐
相关产品推荐

