【问题标题】:cv2.drawContours why nothing appears?cv2.drawContours 为什么什么都没有出现?
【发布时间】:2017-02-10 01:51:53
【问题描述】:

您好,我对 Python 很陌生,我正在尝试获取图像中的边界矩形。我做了以下事情:

import cv2 
from skimage import io
import numpy as np
img = cv2.imread('artelab.jpg',0)
ret,thresh = cv2.threshold(img,127,255,0)
im2,contours,hierarchy = cv2.findContours(thresh, 1, 2)
cnt = contours[0]
M = cv2.moments(cnt)
print M
rect = cv2.minAreaRect(cnt)
box = cv2.boxPoints(rect)
box = np.int0(box)
cv2.drawContours(img,[box],0,(0,0,255),2)

什么都没有出现,但我没有收到错误消息。有什么问题?

【问题讨论】:

  • 尝试在for 循环中运行相同的程序,以便识别图像中存在的所有轮廓
  • 错误信息是什么?请粘贴整个回溯。
  • 也许你的阈值太激进了。也很难在没有额外日志/图像的情况下提供任何建议。
  • @JeruLuke 谢谢,成功了。
  • @wfernandez 很高兴我能帮上忙 :D :D

标签: python opencv


【解决方案1】:

添加for循环后,我获得了所有对象周围的边界框。谢谢

import cv2
from skimage import io
import numpy as np

img = cv2.imread('paper_artelab.jpg',0)
ret,thresh = cv2.threshold(img,127,255,0)
im2,contours,hierarchy = cv2.findContours(thresh, 1, 2)
numbContours=contours.__len__()

for x in range(0,numbContours):
    cnt = contours[x]
    rect = cv2.minAreaRect(cnt)
    box = cv2.boxPoints(rect)
    box = np.int0(box)
    cv2.drawContours(img,[box],0,(0,0,255),2)
io.imshow(img)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-23
    • 1970-01-01
    • 1970-01-01
    • 2016-09-12
    • 1970-01-01
    • 2017-12-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多