打开cv检测桌椅

时间:2016-03-28 20:30:54

标签: python-2.7 opencv

我正在尝试从图像中检测桌椅,并且在下面的代码中没有取得多大成功。它还检测其他方块等任何提示?

enter image description here

import cv2
image = cv2.imread("nao.jpg")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (3, 3), 0)
cv2.waitKey(0)
edged = cv2.Canny(gray, 10, 250)
cv2.waitKey(0)

kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (7, 7))
closed = cv2.morphologyEx(edged, cv2.MORPH_CLOSE, kernel)
cv2.waitKey(0)

(_,cnts, _) = cv2.findContours(closed.copy(), cv2.CHAIN_APPROX_NONE, cv2.CHAIN_APPROX_SIMPLE)
total = 0

for c in cnts:
    peri = cv2.arcLength(c, True)
    approx = cv2.approxPolyDP(c, 0.01 * peri, True)
    if len(approx) == 4:
         print approx
         cv2.drawContours(image, [approx], -1, (0, 255, 0), 4)
         total += 1

print "I found {0} lines in that image".format(total)
cv2.imshow("Output", image)
cv2.waitKey(0)

0 个答案:

没有答案