2017-03-15 59 views
2

我正在研究一个程序来检测页面上形状的边缘,然后裁剪并包装图像以放大形状。然而,问题在于,我的形状在每个角落都只有一个标记。下面是一个例子形象,我想工作,并具:使用OpenCV在python中检测不完整形状的角落

Example

我怎么能确定图像的角落?我已经尝试过轮廓分析,甚至是特征匹配算法,但是没有一个能够提供我需要的可靠性。

因为我是一般的新手,有没有一个方便的功能能够解决我的确切问题?

谢谢!

编辑:由于可变灯光,我说的什么,我试图处理的示例图像:

Image Raw

+0

你检查出'cv2.goodFeaturesToTrack()'? –

回答

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参数打

粘贴。

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

+0

我尝试使用好的功能进行跟踪,但第二次拍摄了真实世界照片(来自手机或类似的东西),照明中的渐变确实给goodFeaturesToTrack带来了困难。 – agupta231

+0

我明白了。良好的照明使处理更容易,但BilateralFilter可以提供一些帮助。你能分享这些真实世界的图像之一吗? –

+0

我修改了原始文章并包含了一个示例图像。 – agupta231

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