python - 使用 OpenCV 进行图像检测

标签 python opencv image-processing computer-vision

假设我想检测图像中是否存在果酱 jar 。例如。在下表中,我的 table 上有一个果酱 jar 等等。该代码将检测图像有卡纸 jar 。如果图像中没有果酱 jar ,代码将突出显示,没有图像。

我想在 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()

这种方法有两个问题:

1)它没有正确检测到实际物体。
2)我希望代码告诉我哪些是不匹配的图像。

请在下面找到我使用的图像。

有人可以帮忙吗?任何编码示例引用都可以。

谢谢!

enter image description here

enter image description here

最佳答案

也许您可以尝试使用 Google Vision API 来识别您的问题:https://cloud.google.com/vision/

关于python - 使用 OpenCV 进行图像检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44304319/

相关文章:

python - __init__() 在转储的 json/__init__.py 中获得意外的关键字参数 'default'

Python清除列表清除错误列表?

python - 从 mp3 生成音量曲线

c++ - 使用带有命名空间的 cvShowImage

python - 图像边缘检测 Keras 模型损失没有改善

java.lang.ArrayIndexOutOfBoundsException : -1

python - 最常用的视频处理 Python 模块?

c++ - 是否可以有一个 cv::Mat 包含指向标量而不是标量的指针?

algorithm - 除了 Haar 级联之外,还有哪些算法或方法可用于自定义对象检测?

matlab - 计算区域内部或外部的邻域