我试图弄清楚我所在地区的绿化面积。我从Google Maps拍摄了50米缩放级别的图像,并尝试将绿色与图像分开。
这些仅用于测试目的。如果结果显示良好,将在高分辨率图像上执行。
原始图片:
注意:由于2 MB的最大大小限制,我需要减小其上传尺寸。
在其上绘制轮廓后的图像:
以下是我的代码:
import numpy as np
import cv2
image = cv2.imread('map.png')
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
# lower and upper range of green color in HSV color format
mask = cv2.inRange(hsv, np.array([75, 50, 0]), np.array([120, 100, 100]))
mask, contours, hierarchy = cv2.findContours(mask, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
# print cv2.contourArea(contours)
image_with_contours = cv2.drawContours(image, contours, -1, (0,255,0), 3)
cv2.imshow('CHAIN_APPROX_NONE', image_with_contours)
cv2.waitKey(0)
cv2.destroyAllWindows()
我的问题:
print cv2.contourArea(contours)
进行面积计算会显示以下错误TypeError: contour is not a numpy array, neither a scalar
以下对我没有帮助:
OpenCV TypeError: contour is not a numpy array, neither a scalar
谢谢
最佳答案
通常,对于遥感,他们使用多光谱,高光谱图像来计算植被。在rgb / hsv中操作不会那么准确。尝试是否可以找到一个。
无论如何,对于第一个部分,最好制作一个opencv跟踪栏以找到正确的hsv值。在此处可以找到rgb的示例。至于第二部分,轮廓是找到的所有轮廓的列表。所以用
for i in range len(contours):
print cv2.contourArea(contours[i])
关于python - Opencv:TypeError:轮廓不是numpy数组,也不是标量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46138543/