如何检测图像中对象的实例? [英] How do I detect an instance of an object in an image?

查看:20
本文介绍了如何检测图像中对象的实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含几个特定对象的图像.我想检测这张图片中这些物体的位置.为此,我有一些模型图像,其中包含我想要检测的对象.这些图像在我要检测的对象实例周围很好地裁剪.

I have an image containing several specific objects. I would like to detect the positions of those objects in this image. To do that I have some model images containing the objects I would like to detect. These images are well cropped around the object instance I want to detect.

这是一个例子:

在这张大图中,

我想检测此模型图像中表示的对象:

I would like to detect the object represented in this model image:

推荐答案

既然你 最初发布这个作为一个 'gimme-da-codez 的问题,完全没有任何努力,我不会给你代码.我将笼统地描述该方法,并在此过程中提供提示,由您来确定执行此操作的确切代码.

Since you originally posted this as a 'gimme-da-codez' question, showing absolutely no effort, I'm not going to give you the code. I will describe the approach in general terms, with hints along the way and it's up to you to figure out the exact code to do it.

首先,如果您有一个模板,一个更大的图像,并且您想在图像中找到该模板的实例,总是要考虑 互相关.无论您是处理一维信号(在信号处理中称为匹配滤波器)还是二维图像,理论都是相同的.

Firstly, if you have a template, a larger image, and you want to find instances of that template in the image, always think of cross-correlation. The theory is the same whether you're processing 1D signals (called a matched filter in signal processing) or 2D images.

  • 将图像与已知模板进行交叉关联会在模板完全匹配的情况下为您提供峰值.查找函数 normxcorr2并理解文档中的示例.
  • 找到峰值后,您必须考虑与原始图像中实际位置的偏移量.偏移与 N 点信号与 M 点信号的互相关导致 N + M -1 点的事实有关输出.一旦您阅读了互相关,就应该清楚这一点,但您还应该查看我上面提到的文档中的示例以获得一个想法.
  • Cross-correlating an image with a known template gives you a peak wherever the template is an exact match. Look up the function normxcorr2 and understand the example in the documentation.
  • Once you find the peak, you'll have to account for the offset from the actual location in the original image. The offset is related to the fact that cross-correlating an N point signal with an M point signal results in an N + M -1 point output. This should be clear once you read up on cross-correlation, but you should also look at the example in the doc I mentioned above to get an idea.

一旦你完成了这两个,那么剩下的就是微不足道的了,只需要对你的结果进行修饰.这是我检测到上述对象后的结果.

Once you do these two, then the rest is trivial and just involves cosmetic dressing up of your result. Here's my result after detecting the object following the above.

这里有一些代码提示可以帮助您入门.在我有 ...

Here's a few code hints to get you going. Fill in the rest wherever I have ...

%#read & convert the image
imgCol  = imread('http://i.stack.imgur.com/tbnV9.jpg');
imgGray = rgb2gray(img);
obj     = rgb2gray(imread('http://i.stack.imgur.com/GkYii.jpg'));

%# cross-correlate and find the offset
corr           = normxcorr2(...); 
[~,indx]       = max(abs(corr(:))); %# Modify for multiple instances (generalize)
[yPeak, xPeak] = ind2sub(...);
corrOffset     = [yPeak - ..., xPeak - ...]; 

%# create a mask
mask      = zeros(size(...));
mask(...) = 1;
mask      = imdilate(mask,ones(size(...)));

%# plot the above result
h1 = imshow(imgGray);
set(h1,'AlphaData',0.4)
hold on
h2 = imshow(imgCol);
set(h2,'AlphaData',mask)

这篇关于如何检测图像中对象的实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆