使用 OpenCV 检测一个图像中的对象是否在另一个图

Detecting if an object from one image is in another image with OpenCV(使用 OpenCV 检测一个图像中的对象是否在另一个图像中)

本文介绍了使用 OpenCV 检测一个图像中的对象是否在另一个图像中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含对象的示例图像,例如下图中的耳环:

I have a sample image which contains an object, such as the earrings in the following image:

http://imgur.com/luj2Z

然后我有一大组候选图像,我需要确定哪一个最有可能包含对象,例如:

I then have a large candidate set of images for which I need to determine which one most likely contains the object, e.g.:

http://imgur.com/yBWgc

所以我需要为每个图像生成一个分数,其中最高分数对应于最有可能包含目标对象的图像.现在,在这种情况下,我有以下条件/约束可以使用/解决:

So I need to produce a score for each image, where the highest score corresponds to the image which most likely contains the target object. Now, in this case, I have the following conditions/constraints to work with/around:

1) 我可以获取多个不同角度的样本图像.

1) I can obtain multiple sample images at different angles.

2) 样本图像的分辨率、角度和距离可能与候选图像不同.

2) The sample images are likely to be at different resolutions, angles, and distances than the candidate images.

3) 有很多候选图像 (> 10,000),所以它必须相当快.

3) There are a LOT of candidate images (> 10,000), so it must be reasonably fast.

4) 我愿意为速度牺牲一些精度,所以如果这意味着我们必须搜索前 100 名而不是只搜索前 10 名,那很好,可以手动完成.

4) I'm willing to sacrifice some precision for speed, so if it means we have to search through the top 100 instead of just the top 10, that's fine and can be done manually.

5) 我可以手动操作样本图像,例如勾勒出我希望检测的对象;候选图像太多,无法手动操作.

5) I can manipulate the sample images manually, such as outlining the object that I wish to detect; the candidate images cannot be manipulated manually as there are too many.

6) 我根本没有真正的 OpenCV 或计算机视觉背景,所以我在这里从头开始.

6) I have no real background in OpenCV or computer vision at all, so I'm starting from scratch here.

我最初的想法是先在示例图像中的对象周围画一个粗略的轮廓.然后,我可以识别对象中的角点和候选图像中的角点.我可以分析每个角周围的像素,看看它们是否相似,然后按每个角的最大相似度得分之和进行排名.我也不确定如何量化相似的像素.我猜只是它们的 RGB 值的欧几里得距离?

My initial thought is to start by drawing a rough outline around the object in the sample image. Then, I could identify corners in the object and corners in the candidate image. I could profile the pixels around each corner to see if they look similar and then rank by the sum of the maximum similarity scores of every corner. I'm also not sure how to quantify similar pixels. I guess just the Euclidean distance of their RGB values?

问题在于它有点忽略了对象的中心.在上面的例子中,如果耳环的角都靠近金框,那么就不会考虑耳环里面的红绿蓝宝石.我想我可以通过查看所有角对并通过沿它们之间的线采样一些点来确定相似性来改进这一点.

The problem there is that it kind of ignores the center of the object. In the above examples, if the corners of the earrings are all near the gold frame, then it would not consider the red, green, and blue stones inside the earring. I suppose I could improve this by then looking at all pairs of corners and determining similarity by sampling some points along the line between them.

所以我有几个问题:

A) 这种思路总体上是否有意义,还是我遗漏了什么?

A) Does this line of thinking make sense in general or is there something I'm missing?

B) 我应该使用 OpenCV 中的哪些特定算法进行调查?我知道有多种角点检测算法,但我只需要一个,如果差异都在边缘优化,那么我可以用最快的.

B) Which specific algorithms from OpenCV should I investigate using? I'm aware that there are multiple corner detection algorithms, but I only need one and if the differences are all optimizing on the margins then I'm fine with the fastest.

C) 任何使用有助于我理解的算法的示例代码?

C) Any example code using the algorithms that would be helpful to aid in my understanding?

我的语言选择是 Python 或 C#.

My options for languages are either Python or C#.

推荐答案

查看 SURF 功能,这是 openCV 的一部分.这里的想法是你有一个算法可以在两个图像中找到兴趣点".您还有一个算法可以计算每个兴趣点周围的图像块的描述符.通常,此描述符捕获补丁中边缘方向的分布.然后您尝试找到点对应关系,即.e.对于图像 A 中的每个兴趣点,尝试在图像 B 中找到相应的兴趣点.这是通过比较描述符并寻找最接近的匹配来完成的.然后,如果您有一组通过某种几何变换相关的对应关系,那么您就有了检测.

Check out the SURF features, which are a part of openCV. The idea here is that you have an algorithm for finding "interest points" in two images. You also have an algorithm for computing a descriptor of an image patch around each interest point. Typically this descriptor captures the distribution of edge orientations in the patch. Then you try to find point correspondences, i. e. for each interest point in image A try to find a corresponding interest point in image B. This is accomplished by comparing the descriptors, and looking for the closest matches. Then, if you have a set of correspondences that are related by some geometric transformation, you have a detection.

当然,这是一个非常高级的解释.魔鬼在细节中,对于那些你应该阅读一些论文.从 David Lowe 的 来自尺度不变关键点的独特图像特征开始,然后阅读 SURF 上的论文.

Of course, this is a very high level explanation. The devil is in the details, and for those you should read some papers. Start with Distinctive image features from scale-invariant keypoints by David Lowe, and then read the papers on SURF.

另外,考虑将此问题移至信号和图像处理堆栈交换

Also, consider moving this question to Signal and Image Processing Stack Exchange

这篇关于使用 OpenCV 检测一个图像中的对象是否在另一个图像中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:使用 OpenCV 检测一个图像中的对象是否在另一个图

基础教程推荐