I want to detect squares using OpenCV. Here’s the image I’m trying to process:
There are eleven colored squares, and the example in OpenCV at https://code.ros.org/trac/opencv/browser/trunk/opencv/samples/c/squares.c?rev=1429 won’t detect all the squares. What can I do to detect all of them?

Optimize the code according to the case.Study and play with it.
One other way to detect squares is to use a signature-based generalized hough voting framework. Create a square signature and then convolute it with the image. Accumulate the results in the “Hough Space” and find maxima (e.g. mean-shift).
To achieve scale-invariance, build a multi-scale signature.
Or you could try template matching. Use dense sampling or a keypoint detector (e.g. harris edge ), and build a spatial codebook using some descriptor (e.g. SURF,ORB). Then, you can use a sliding window for matching by thresholding codebook distances.