2017-06-01 132 views
0

假設我想檢測圖像中是否存在果醬罐。例如。在下表中,我在桌子上還有一個果醬罐。代碼將檢測到圖像有果醬罐。如果圖像中沒有果醬罐,代碼將突出顯示,沒有圖像。使用OpenCV進行圖像檢測

我想用python中的openCV創建一個代碼來檢測圖像。

我發現「模板匹配」是一種方法。我正在使用的代碼如下:

import cv2 
import numpy as np 
from matplotlib import pyplot as plt 
img = cv2.imread('flower.jpg',0) 
img2 = img.copy() 
template = cv2.imread('jam_image.jpg',0) 
w, h = template.shape[::-1] 
# All the 6 methods for comparison in a list 
methods = ['cv2.TM_CCOEFF', 'cv2.TM_CCOEFF_NORMED', 'cv2.TM_CCORR', 
      'cv2.TM_CCORR_NORMED', 'cv2.TM_SQDIFF', 'cv2.TM_SQDIFF_NORMED'] 
for meth in methods: 
    img = img2.copy() 
    method = eval(meth) 
    # Apply template Matching 
    res = cv2.matchTemplate(img,template,method) 
    min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res) 
    # If the method is TM_SQDIFF or TM_SQDIFF_NORMED, take minimum 
    if method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]: 
     top_left = min_loc 
    else: 
     top_left = max_loc 
    bottom_right = (top_left[0] + w, top_left[1] + h) 
    cv2.rectangle(img,top_left, bottom_right, 255, 2) 
    plt.subplot(121),plt.imshow(res,cmap = 'gray') 
    plt.title('Matching Result'), plt.xticks([]), plt.yticks([]) 
    plt.subplot(122),plt.imshow(img,cmap = 'gray') 
    plt.title('Detected Point'), plt.xticks([]), plt.yticks([]) 
    plt.suptitle(meth) 
    plt.show() 

有2個問題,這種方法:

1)不正確地檢測實際的對象。 2)我想讓代碼告訴我哪些圖片不匹配。

請找到我在下面使用的圖像。

任何人都可以請幫忙嗎?任何編碼示例參考都可以。

謝謝!

enter image description here

enter image description here

+0

我已經更新的問題。 – Beta

+0

嗨,任何人都可以解釋爲什麼我的問題仍然擱置。我想這是一個明確定義的問題。 – Beta

+0

我刪除了我的答案,可能會讓其他人更願意回答你。祝你好運。 –

回答

0

使用機器學習的圖像中檢測果醬瓶裏。首先使用正面和負面訓練示例訓練您的系統,然後使用訓練好的模型預測圖像是否包含果醬罐。

您可以使用CNN,SVM來達到此目的。 見鏈接:
http://www.pyimagesearch.com/2015/11/09/pedestrian-detection-opencv/
HOG training and detection in Python using OpenCV
http://docs.opencv.org/2.4/modules/gpu/doc/object_detection.html

+0

感謝您的評論!我會研究你的建議。 – Beta