Recognise a object in an image

Posted on 2006-07-12
Last Modified: 2010-04-05
I need a script that recognises an object in an image.

Lets say I have a picture (a jpg or bmp file) of a room, and in the room is a red ball. The image hast the size of lets say 400px X 400px big. I need a good script that tells me the coordinates from the rectange in which the red ball is, so if the red ball is in the center of the picture the script should give me a rectangle like this: TRect(180, 180, 40, 40).

The best would be if I could "feed" the algorithm with a clean photo of the object and it would recognise it in the picture. It only needs to recognise clean objects like a ball or a box that has a bright color.

Important is that I only need the rectagle, nothing more. I hope to find here some good help.

Question by:KBunk
LVL 17

Expert Comment

ID: 17096100
is the object that you are searching for always the same size?

Author Comment

ID: 17097266
the object itself yes, but in the picture the object may be further away and thus smaller or nearer and bigger... the purpose of this is to make my hexabot (a spider robot with 6 legs) able to recognise a red ball and follow it. For this I just need an algorithm that solves me the above mentioned problem.

It is not important in which language.. best would be delphi, but any other language woul do it to. Does maybe someone know any open source projects or sdk that handle this?
LVL 17

Accepted Solution

TheRealLoki earned 168 total points
ID: 17105574
without googling, 1 method off the top of my head would be to get a "magic wand" routine, like many paint programs have, and perform the routine at various places in your image (eg. every "half the minimum width of the image you are trying to find"). then compare the "shape points" from the "magic wand" to the shape you are trying to find (allowing for size, so you will want either angles, or % change rather than just x,y)

another thought I had is you could do a mask on "red" so everything else is removed from teh image. it's then just a simple loop to find the "red" objects, and find their borders.
The problem with tracking a red ball is the light. you get light spots on it and the colour quickly fades making it hard to find. Make the ball matte, not shiny for a start.

I checked out some other EE questions on the subject, and the general theme seems to be
use as few colours as possible.
shape recognition is usually made of the following steps:
1) contour extraction (usually made by a convolution mask applied on the image)
2) Removal of isolated pixels (and small groups of pixels too)
3) Recognition of the borders (previously extracted and cleaned) you have to find coordinates of segments that best fit with the contour pixels

some c++ references here
and autotrace my have some possibilities

sorry I couldn't be of more help.
LVL 17

Assisted Solution

by:Wim ten Brink
Wim ten Brink earned 166 total points
ID: 17106922
Unless I am mistaken, yo have multiple images which you can compare with each other. So what I would do is first "subtract" the images from each other and see where the two are different. If two images are identical, nothing will have moved. But if something has moved, then the two pictures will be slightly different and basically, what you want to analyse are the differences, not the whole picture.
Once you know the locations of where these differences are, all you need to do is check out the shapes in those locations. If you find a red ball there, using the method described by TheRealLoki, then you'll have found your target.

But you will have to deal with other issues too here. First of all, it sounds the pictures are coming from a webcam or whatever. This means that you will have to keep in mind some slight variances in lights and shadows in the two pictures. And if your robot can turn it's head or walk around, you will also have to adjust your calculations to include the small amounts that the picture might have changed, meaning that the area that you're comparing will be a bit smaller than the whole picture. For example, your bot moves it's head slightly to the left. This means that your 400x400 pictures will be completely different if you just subtract them. You will have to find the area that picture 1 and picture 2 have in common and just subtract compare those two pictures. (And you'd also have to check the area that was added to the new picture in case the ball turned up in that area.

You're dealing here with a very complex situation which requires very good math skills and good knowledge of certain algorithms. Even the experts haven't found good solutions for this.
LVL 26

Assisted Solution

EddieShipman earned 166 total points
ID: 17111600
If it always the same color...

You can use the BitmapToRegion code here:

Set the transparent color to the color you want to detect, then combine the region returned with a region
that encompasses the entire bitmap to get the region that contains the color. Then check the location of the
spider to the "ball" using PtInRgn

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now