【问题标题】:How do I find an image similar to the input image from a directory?如何从目录中找到与输入图像相似的图像?
【发布时间】:2019-07-04 16:03:00
【问题描述】:

我有一个 python 程序,可以检测来自网络摄像头的图像。现在,我想将网络摄像头识别的图像与我目录中的图像进行比较,并检查是否已经存在完全相似的图像。

我尝试过使用this 识别算法,但它不起作用。无论输入图像有多么不同,程序总是输出单个图像。

输入图像(网络摄像头扫描的图像)有点模糊like this,而数据集中的图像looks like this

我需要一种能够更准确地识别这些图像的算法。

【问题讨论】:

  • 我推荐你使用 K-nearest-neighbors 方法,from sklearn.neighbors import KNeighborsClassifier,sklearn 已经有那个模型了。只要记住将目录中的图像调整为与查询图像相同的形状
  • 我是机器学习新手。你能给我发一份参考资料,让我了解更多关于 K 近邻的信息吗?
  • 这完全取决于你如何定义“相似”,这通常是一项非常艰巨的任务,所以不要期望得到一个真正适合你的任务的令人满意的答案。如果您可以限制可能的失真(例如“仅模糊和一些压缩伪影”),它变得更好回答
  • 谢谢。 KNN 算法运行良好

标签: python opencv image-recognition


【解决方案1】:

在这里我为你写了一个小脚本,希望它能解决你的问题

import cv2
import os
import numpy as np
from matplotlib import pyplot as plt
from PIL import Image

def read_img_from_dir(directory, query_shape):
    # query_shape is a tuple which contain the size (width, height) of query image
    # directory is your dir contain image you wanna find
    name_image = []
    shape = query
    first = True
    for pics in os.listdir(directory):
        name_image.append(pics)
        image = Image.open(pics)
        image = image.resize(shape)
        image = np.array(image)
        image = np.reshape(image,(1,-1))
        if first:
            img_array = np.copy(image)
            first = False
        else:
            img_array = np.concatenate((img,array,image),axis=0)
    return name_image, img_array    

def find_by_knn(img, list_name, list_array):
    # image_query is path of your picture you wanna find
    # list_name and list_array is result of above function
    img = np.reshape(img,(1,-1))
    num_pics = list_array.shape[0]
    dists = np.zeros((num_pics,1))
    dists = list(np.sqrt(np.sum((list_array-img)**2,axis = 1)))
    idx = dists.index(max(dists))
    return list_name[idx]

img = cv2.imread(image_query)
shape = img.shape[:2]
name_image, img_array = read_img_from_dir(directory,shape)
result = find_by_knn(img, name_image, img_array)
print(result)

如果您想了解更多关于 KNN 的信息,请查看此链接:http://cs231n.github.io/classification/#nn

【讨论】:

  • 非常感谢!!我已经使用了 KNN 算法,它的工作非常完美。我没有使用您提到的代码,因为它显示的错误很少。但是你提到的那个资源对我很有帮助。
【解决方案2】:

使用 python openCV 进行图像搜索。

打开简历的链接

opencv_python-4.1.0+contrib-cp35-cp35m-win_amd64.whl

下载导入 cv2

import numpy as np

从 matplotlib 导入 pyplot 作为 plt

img = cv2.imread('watch.jpg',cv2.IMREAD_GRAYSCALE)

cv2.imshow('image',img)
cv2.waitKey(0)

cv2.destroyAllWindows()

pip 安装 numpy

pip 安装 matplotlib

Matplotlib 用于显示视频或图像中的帧。

Numpy 用于所有“数字和 Python”。 我们主要是利用 Numpy 的数组功能。

   import cv2

将 numpy 导入为 np

从 matplotlib 导入 pyplot 作为 plt

img = cv2.imread('watch.jpg',cv2.IMREAD_GRAYSCALE)

cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

首先我们要导入一些东西 接下来,我们将 img 定义为 cv2.read(image file, parms)。

默认值为 IMREAD_COLOR,即没有任何 Alpha 通道的颜色。

对于第二个参数,可以使用-1、0或1。颜色为1,灰度为0,不变的是-1。因此,对于灰度,可以执行 img = cv2.imread('watch.jpg', 0)

加载后,我们使用 cv2.imshow(title,image) 来显示图像。从这里, 我们使用cv2.waitKey(0) 等到按下任意键。一旦完成, 我们使用cv2.destroyAllWindows() 关闭所有内容。

加载视频源 Open CV Python 带有视频和网络摄像头。

处理视频中的帧与处理图像相同。

代码--

 import numpy as np

import cv2

cap = cv2.VideoCapture(0)

 while(True):

ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
 cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):

    break

cap.release()

cv2.destroyAllWindows()

我们导入 numpy 和 cv2 接下来,我们可以使用 cap = cv2.VideoCapture(0)。

  while(True):
ret, frame = cap.read()

我们将 ret 和 frame 定义为 cap.read()。

  gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

*我们定义了一个新的变量gray,作为frame,转换为gray。

注意**

*OpenCV 将颜色读取为 BGR(蓝绿红), 大多数计算机应用程序读取为 RGB(红绿蓝)。

 cv2.imshow('frame',gray)

在这里,我们显示的是转换为灰色的 Feed。

if cv2.waitKey(1) & 0xFF == ord('q'):
  break

如果 key 是 q,我们将退出 while 循环,然后运行:

cap.release()
cv2.destroyAllWindows()

这会释放网络摄像头,然后关闭所有 imshow() 窗口。

如果你想保存录音,然后使用,

 import numpy as np
import cv2

cap = cv2.VideoCapture(1)


fourcc = cv2.VideoWriter_fourcc(*'XVID')

out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))


while(True):

ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
out.write(frame)
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
    break

cap.release()
out.release()

cv2.destroyAllWindows()

【讨论】:

  • 然后尝试 reverse_image_search,
猜你喜欢
  • 2015-12-11
  • 1970-01-01
  • 2015-06-09
  • 1970-01-01
  • 2020-07-07
  • 1970-01-01
  • 2013-07-12
  • 2014-11-30
  • 1970-01-01
相关资源
最近更新 更多