Python 2 OpenCV 2 - Convex Hull 关闭打开的 Canny Edge?

标签 python c++ python-2.7 opencv

我正在创建一个 Python 程序,该程序将拍摄图像(一张床的照片),然后运行 ​​Canny 边缘检测,然后运行凸包轮廓检测以找出床的位置。

这是我的图像(请原谅丑陋的 Photoshop 工作)

bed

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

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)

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

这是我目前得到的输出:

Output

我接下来要做的是运行凸包轮廓检测,这样床的右上角将被关闭,并希望得到一个很好的矩形估计床的位置。

现在我卡住了主要是因为我不确定如何使用船体点?我试过类似的东西:

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 2 OpenCV 2 - Convex Hull 关闭打开的 Canny Edge?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42677845/

相关文章:

Python:为什么不将 "next()"称为 "__next__()"

c++ - 有经验的程序员对 closesocket() 的关注

C++/QML交互式组合框实现

python - 使用bs4提取html文件中的文本

python - 在 Python 中使用多个条件索引数组?

python - 如何在迁移过程中访问不在 django 模型中但在数据库中可用的字段

python - 按值拆分为等份

python - 除非我手动调用,否则我无法调用我的函数之一

Python 请求 - 上传 Zip 文件

c++ - 如何将 SQLite C++ 库添加到 Eclipse(C++ 版本)