I am using OpenCV with Java, as shown here. I am stuck while writing a simple program for detecting number of red rectangles in a picture. I never worked with OpenCV before, can someone help me. Thank in advance.
Hi, I could use some more information e.g. what kind of rectangles you want to detect. There's a huge difference between detecting rectangles in a photo and those drawn by an program.
Those drawn by a program are much cleaner and easily detectable while those on a photo may need some work.
To detect something that has any kind of edge, just look at this tutorial: http://dasl.mem.drexel.edu/~noahKuntz/openCVTut7.html. The functions used should be named similar in java though I'm not sure about that as I only used opencv with C++.
While iterating trough the results, just check the rgb-value of the original image. If the edge opencv found has a value of r=255 its probably red. You can get the rgb value trough the function cvGet2d (you need to use channel 1 for r) or by directly accessing the matrix opencv provides trough the data pointer of the image object.
It would also be possible to just save all the points with red in them in a vector and try to compute a rectangle out of random points that fit into parallel lines. If you know the approximate size of the rectangles you are looking for that would be very accurate. Try googeling for a vector2df class, that should help you do the trick and save you from the most of the math that needs to be done.
Hope this helps.
Have a look at http://www.aishack.in/2010/07/tracking-colored-objects-in-opencv/ It uses a yellow ball instead of red rectangle. This, however, works for only one object.
If there are multiple rectangles, check the squares.c example (it comes with OpenCV) http://www.aishack.in/2010/01/an-introduction-to-contours/ is a short explanation on how that example works.
You'll have to do a combination of the two programs to calculate the number of rectangles in your image.
All these are in C, though I'm sure you'll be able to translate it into equivalent Java code! I hope this helps!