我正在创建一个Python程序,该程序将拍摄一张图像(一张床的照片),然后运行Canny Edge检测,然后运行一个Convex Hull Contour Detection以找出床在哪里。

这是我的图片(对不起,Photoshop作业很丑)

python - Python 2 OpenCV 2-凸包关闭打开的Canny Edge?-LMLPHP

到目前为止,这是我的代码:

import numpy as np
import argparse
import glob
import cv2
import sys

from pyimagesearch import imutils
from skimage import exposure


def auto_canny(image, sigma = 0.35):
    # compute the mediam of the single channel pixel intensities
    v = np.median(image)

    # apply automatic Canny edge detection using the computed median
    lower = int(max(0, (1.0 - sigma) * v))
    upper = int(min(255, (1.0 + sigma) *v))
    edged = cv2.Canny(image, lower, upper)

    # return edged image
    return edged

image = cv2.imread("bed_cv.jpg")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (3, 3), 0)

wide = auto_canny(blurred, 0.35)
tight = auto_canny(blurred, 0.95)
auto = auto_canny(blurred)



cv2.imshow("Original", image)
cv2.imshow("Edges", tight)

cv2.waitKey(0)

我们在这里基本上要做的是获取图像,将其变成灰色,对其进行模糊处理,然后运行精巧边缘检测。

这是我到目前为止的输出:

python - Python 2 OpenCV 2-凸包关闭打开的Canny Edge?-LMLPHP

我接下来要做的是运行“凸包轮廓检测”,以便关闭床的右上角,并希望对床的位置进行矩形估算。

现在,我被卡住的主要原因是我不确定如何使用船体点?我尝试了类似的东西:
hull = cv2.convexHull(cnts, returnPoints = False)

但是我不知道以后该怎么办。

我现在的主要负责人是this post from the opencv website。这正是我所需要的。他的初始输出是两组断开的边,答案设法将它们连接起来。但是,我对C++相当生疏,不确定如何将C++代码转换为Python。而且我认为,如果获得此版本的Python,则基本上可以解决我的问题。

最佳答案

这是仅使用OpenCV的解决方案,对我来说非常有用:

mask = cv2.Canny(gray, lowerThresh, upperTresh)
cnts = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for i, cnt in enumerate(cnts):
    cnts[i] = cv2.convexHull(cnts[i])

希望能帮助到你!

关于python - Python 2 OpenCV 2-凸包关闭打开的Canny Edge?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42677845/

10-12 21:14
查看更多