【问题标题】:Detecting the corners of an incomplete shape with OpenCV in python在 python 中使用 OpenCV 检测不完整形状的角
【发布时间】:2017-08-06 22:24:15
【问题描述】:

我正在开发一个程序来检测页面上形状的边缘,然后裁剪并包裹图像以放大形状。但是,问题是我的形状仅由每个角上的标记限制。以下是我正在尝试使用的示例图像:

如何确定图像的角?我尝试过轮廓分析,甚至是特征匹配算法,但它们都没有给我所需的可靠性。

一般来说,我是 CV 新手,是否有一个方便的功能可以解决我的确切问题?

谢谢!

编辑:由于光照变化,我添加了我正在尝试处理的示例图像:

【问题讨论】:

  • 你签出cv2.goodFeaturesToTrack()了吗?

标签: python opencv computer-vision feature-detection


【解决方案1】:

我会尝试使用cv2.goodFeaturesToTrack()。您可以从 openCV 文档 LINK 复制/粘贴代码

import numpy as np
import cv2
import matplotlib.pyplot as plt

img = cv2.imread('test.png')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
blur = cv2.bilateralFilter(gray, 16, 50, 50)

corners = cv2.goodFeaturesToTrack(blur, 40, 0.1, 10)
corners = np.int0(corners)

for i in corners:
    x,y = i.ravel()
    cv2.circle(img,(x,y),3,255,-1)

plt.circle(img, (x, y), 3, 255, -1)
plt.show()

这就是我得到的

您可能想要使用双边过滤器和 goodFeaturesToTrack 参数。

另一种选择是使用角落的匹配过滤器(这样您就不会得到单词的所有命中)。

【讨论】:

  • 我尝试使用 goodFeaturesToTrack 进行跟踪,但是当我拍摄真实世界的照片(通过手机或类似设备)时,灯光中的渐变确实让 goodFeaturesToTrack 很困难。跨度>
  • 我明白了。良好的照明使处理更容易,但双边过滤器可以提供一些帮助。你能分享其中一张真实世界的图片吗?
  • 我修改了原始帖子并包含了一个示例图像。
【解决方案2】:

你需要做的是使用cv2.findContours()找到给定图像中的所有轮廓,然后找到每个轮廓的边界矩形并在所有轮廓矩形中找到minX,minY,maxX,maxY,这会给你一个外部边界矩形,它涵盖了所有较小的轮廓,因此是您想要的结果。

import cv2
import numpy as np

img = cv2.imread("/Users/anmoluppal/Downloads/mjut8.png", 0)

# Threshold and Invert the image to find the contours
ret, thresh = cv2.threshold(img, 10, 255, cv2.THRESH_BINARY_INV)

# Find the contours
im, contours, hierarchy = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

x, y, r, b = [img.shape[1]/2, img.shape[0]/2, 0, 0]
# Iterate over all the contours, and keep updating the bounding rect

for cnt in contours:
    rect = cv2.boundingRect(cnt)
    if rect[0] < x:
        x = rect[0]
    if rect[1] < y:
        y = rect[1]
    if rect[0] + rect[2] > r:
        r = rect[0] + rect[2]
    if rect[1] + rect[3] > b:
        b = rect[1] + rect[3]

bounding_rect = [x, y, r-x, b-y]

# Debugging Purpose.
cv2.rectangle(img, (x, y), (r, b), np.array([0, 255, 0]), 3)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-06
    • 1970-01-01
    • 1970-01-01
    • 2015-06-10
    • 2014-03-28
    • 2019-02-25
    • 2015-11-05
    • 1970-01-01
    相关资源
    最近更新 更多