在python中使用OpenCV检测不完整形状的角

时间:2017-03-15 23:01:38

标签: python opencv computer-vision feature-detection

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

Example

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

由于我一般都是CV的新手,是否有一个方便的功能能够解决我的确切问题?

谢谢!

编辑:由于灯光变化,我添加了一个我想要处理的示例图片:

Image Raw

2 个答案:

答案 0 :(得分:0)

我会尝试使用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()

这就是我得到的results

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

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

答案 1 :(得分:0)

你需要做的是使用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)

enter image description here