Object Detection Algorithms
SURF works well for objects with distinctive textures. Unfortunately, all lego blocks have essentially the same texture, so it is not a good cue for telling them apart.  The other difficulty is that your blocks can have different 3D orientations.

The good news is that they are brightly colored. The Color Thresholder app is a good way to segment out objects by color.  Once you have identified individual blocks, you can try to use other cues, such as size and shape to identify different blocks of the same color.  Try using the regionprops function.  For example Eccentricity can help you distinguish between a square block and an elongated block, assuming they are viewed from the top.

Unfortunately, I can only give you a starting point, not the complete solution. You are going to have to do some experimenting here.