2017-03-15 58 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