【问题标题】:newbie - attempting object tracking / locating in opencv python, problems with findContours()新手 - 尝试在 opencv python 中进行对象跟踪/定位,findContours() 的问题
【发布时间】:2017-11-12 19:48:13
【问题描述】:

我试图通过颜色来定位一个矩形对象,然后找到对象中心的坐标,或者边界,我不挑剔。

我已经成功地隔离了颜色并创建了蒙版,但是 findContours 功能不起作用,我认为这是因为我没有成功地为 findContours 提供适当的双峰图像。

%matplotlib inline
import matplotlib.image as mpimg
from matplotlib import pyplot as plt
import cv2
import numpy as np

red_image = mpimg.imread('/vagrant/notebooks/red_thing.jpg')
hsv = cv2.cvtColor(red_image, cv2.COLOR_BGR2HSV)
lower_red = np.array([30,150,50])
upper_red = np.array([255,255,180])
mask = cv2.inRange(hsv, lower_red, upper_red)
res = cv2.bitwise_and(red_image,red_image, mask= mask)
kernel = np.ones((20,20),np.uint8)
ret,thresh1 = cv2.threshold(res,60,255,cv2.THRESH_BINARY)
# perform 'open' operation to homogenize object
opened = cv2.morphologyEx(thresh1, cv2.MORPH_OPEN, kernel)
image, contours, hierarchy = cv2.findContours(opened,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

最后一行产生以下错误,我不知道如何解决。

---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
<ipython-input-130-06cc5691b64a> in <module>()
----> 1 image, contours, hierarchy = cv2.findContours(opened,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

error: /home/vagrant/opencv/modules/imgproc/src/contours.cpp:199: error: (-210) [Start]FindContours supports only CV_8UC1 images when mode != CV_RETR_FLOODFILL otherwise supports CV_32SC1 images only in function cvStartFindContours_Impl

【问题讨论】:

    标签: python opencv image-processing opencv3.0


    【解决方案1】:

    我能够通过转换为灰色来修复错误,如下所示:gray = cv2.cvtColor(res, cv2.COLOR_BGR2GRAY)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-26
      • 2017-03-21
      • 2012-11-17
      • 1970-01-01
      • 2014-01-09
      • 2019-12-07
      • 2017-04-25
      相关资源
      最近更新 更多