Recognise a object in an image

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.

Who is Participating?
TheRealLokiSenior DeveloperCommented:
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.
TheRealLokiSenior DeveloperCommented:
is the object that you are searching for always the same size?
KBunkAuthor Commented:
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?
Wim ten BrinkSelf-employed developerCommented:
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.
Eddie ShipmanAll-around developerCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.