python - Opencv:TypeError:轮廓不是numpy数组,也不是标量

标签 python opencv numpy

我试图弄清楚我所在地区的绿化面积。我从Google Maps拍摄了50米缩放级别的图像,并尝试将绿色与图像分开。

这些仅用于测试目的。如果结果显示良好,将在高分辨率图像上执行。

原始图片:

注意:由于2 MB的最大大小限制,我需要减小其上传尺寸。

enter image description here

在其上绘制轮廓后的图像:

enter image description here

以下是我的代码:

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()

我的问题:
  • 找到彩色的上下HSV范围的有效方法是什么。我找到了一些HSV颜色选择器,但它们并没有太大帮助。即使以[75,50,0]为下界,并以[120,100,100]为上界后,我也可以看到第二张轮廓轮廓缺失的绿色部分。
  • 使用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/

    相关文章:

    python - OpenCV-Python 中的数字识别 OCR

    numpy - 在 Numpy 中编写向量矩阵乘积的紧凑自然方式

    python - Numpy __array_prepare__ 错误

    python - numpy.concatenate float64(101,1) 和 float64(101,)

    python - 定量库 : building discount_curve from spots

    python - xmlns 命名空间破坏 lxml

    python - 聚类一组图像

    python - 让 PyQt4 在按下按钮时打开另一个 Qt 窗口

    python - 我如何在多线程应用程序中将 GIL 用于字典?

    opencv - 使用opencv查找对象旋转/比例/位置的方法