【问题标题】:AttributeError: 'module' object has no attribute 'cv' Error in opencv pythonAttributeError:'module'对象在opencv python中没有属性'cv'错误
【发布时间】:2018-06-14 13:12:05
【问题描述】:

我正在检测树木并在使用 opencv Python 检测到的建筑物上绘制多边形。

当我尝试运行代码时,出现以下错误。

AttributeError: 'module' 对象没有属性 'cv'

这是我的代码,

import cv2 as cv
import numpy as np
import utils

# thresh = cv.adaptiveThreshold(gray, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY, 11, 2)
# WINDOW_NAME = "win"

def detect(segmented, original, xdim, ydim):

  # morphological opening and closing
  kernel = np.ones((3,3), np.uint8)
  img = cv.morphologyEx(segmented, cv.MORPH_OPEN, kernel)
  img = cv.morphologyEx(img, cv.MORPH_CLOSE, kernel)

  utils.show_image(img, 'open-close')

  imgcopy = img.copy()
  gray = cv.cvtColor(img, cv.COLOR_RGB2GRAY)

  num_buildings = 0

  for i in xrange(255):
    # threshold the grayscale image at that value
    binary = np.zeros((xdim, ydim), np.uint8)
    ret, binary = cv.threshold(gray, dst=binary, thresh=i, maxval=255, type=cv.THRESH_OTSU)
    #binary[gray == i] = 255
    # utils.show_image(binary, 'binary')

    # find contours, fit to polygon, and determine if rectangular
   _, contours, hierarchy = cv.findContours(binary, mode=cv.RETR_LIST, method=cv.CHAIN_APPROX_SIMPLE)

    for c in contours:
      poly = cv.approxPolyDP(np.array(c), 0.07*cv.ar
        cLength(c,True), True)
      carea = cv.contourArea(c)
      polyarea = cv.contourArea(poly)
      hull = cv.convexHull(c)
      hullarea = cv.contourArea(hull)

      # bounding box
      rect = cv.minAreaRect(c)
      box = cv.cv.BoxPoints(rect)
      box = np.int0(box)

      if polyarea > 30 and carea > 30:
        cv.drawContours(img, [c], 0, (0,0,255), 1)
      if len(poly) < 6 and carea > 100: #and carea > 5: #\
          #and abs(polyarea/carea - 1) < 0.25:
        num_buildings += 1
        cv.drawContours(imgcopy, [poly], 0, (0,0,255), 1)
        cv.drawContours(original, [poly], 0, (0,0,255), 1)

  # show images
  utils.show_image(img, 'all bounding boxes')
  utils.show_image(imgcopy, 'with some filtering')
  utils.show_image(original, 'onto original')
  print num_buildings
  return original

我正在尝试绘制在图像上检测到的多边形, 当我尝试解决这个问题时,它返回的错误与我之前所说的相同。

【问题讨论】:

  • 在 OpenCV 3.x 中,_, counters, hier = cv2.findContours(...)
  • AttributeError: 'module' object has no attribute 'BoxPoints' 我现在收到这个错误!!!

标签: python image opencv


【解决方案1】:
  box = cv2.boxPoints(rect)

当opencv 3.0以上版本时,我们必须以上述方式进行更改。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-03
    • 2016-01-18
    • 2015-05-19
    • 2021-01-15
    • 2015-05-16
    • 1970-01-01
    • 2022-12-20
    • 2023-03-22
    相关资源
    最近更新 更多