2

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.

Here is an example:

In this big image,

enter image description here

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

enter image description here

Dima
  • 38,860
  • 14
  • 75
  • 115
Tomescu George
  • 155
  • 1
  • 2
  • 16
    If you want people to help you, you need to take your question seriously. Write a proper title. Explain your problem specifically. **At the very least, use the right tags.** It's not that difficult. http://stackoverflow.com/questions/how-to-ask – BoltClock Dec 21 '11 at 14:37
  • @Tom: Explain your problem clearly. – karthik Dec 21 '11 at 14:39
  • 3
    Title: Identifying Objects in an Image in `MATLAB` and the tags are [C#], [javascript] and [c++]. so which one is it ?! you are a new user so I won't -1... – Roee Gavirel Dec 21 '11 at 14:41
  • modeling, texturing and lighting is good in the image. good work. mostly mental ray has been used for rendering...and for the question, god knows. – Sandy Dec 21 '11 at 14:43
  • Well, at least say what have you tried so far, or if you don't know even how to start. – Jav_Rock Dec 21 '11 at 14:43
  • How can I reopen this question?? I was about to post my answer, when it was closed.... – Oli Dec 21 '11 at 14:48
  • @Oli See http://stackoverflow.com/faq#close on the FAQ about closed questions. "Closed questions cannot be answered, but are eligible for improvement (and eventual re-opening) through editing, voting, and commenting." So if the question were edited and improved then it may be reopened. – Chris Dec 21 '11 at 14:54
  • 2
    @Oli Can you make a suggested edit that could bring this question up to a better standard of quality? I realize that the OP might just not realize how to articulate the question, you may be able to help. If you can get it in shape, I'd be happy to re-open it. – Tim Post Dec 21 '11 at 14:57
  • 3
    Ok, I rewrote the question. But I don't have enough points to re-open it. The question was fine I think, the main problem was that there was the wrong tags.... – Oli Dec 21 '11 at 15:04

3 Answers3

38

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.

  • 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.

enter image description here

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

%#read & convert the image
imgCol  = imread('https://i.stack.imgur.com/tbnV9.jpg');
imgGray = rgb2gray(img);
obj     = rgb2gray(imread('https://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)
Community
  • 1
  • 1
abcd
  • 41,765
  • 7
  • 81
  • 98
  • 1
    Just out of curiosity, why did you use `abs()`? A negative correlation is a good candidate? – cyborg Dec 21 '11 at 18:28
  • 1
    @cyborg A correlation of -1 implies a signal that's exactly 180º out of phase with the original and as such, I'd consider it to be the same signal. Perhaps it's not necessary here, but I tend to use `abs`. – abcd Dec 21 '11 at 18:52
  • 1
    please could you tell what to enter values for creating a mask – vini Mar 10 '12 at 06:31
9

Here is the answer that I was about to post when the question was closed. I guess it's similar to yoda's answer.

You can try to use normalized cross corelation:

im=rgb2gray(imread('di-5Y01.jpg'));
imObj=rgb2gray(imread('di-FNMJ.jpg'));
score = normxcorr2(imObj,im);
imagesc(score)

The result is: (As you can see, the whitest point corresponds to the position of your object.)

enter image description here

Oli
  • 15,935
  • 7
  • 50
  • 66
4

The Mathworks has a classic demo of image registration using the same technique as in @yoda's answer:

Registering an Image Using Normalized Cross-Correlation

yuk
  • 19,098
  • 13
  • 68
  • 99
  • 1
    @yoda, is it you? I don't pretend and don't want this answer to be accepted, but though it will be a useful information for OP and future readers. – yuk Dec 21 '11 at 19:40