MATLAB: Detecting the position of a previously colored rectangle in an image

digital image processingimageimage analysisimage processingMATLAB

Hello,
do you think I can use MatLab for detecting the position of a previously colored rectangle in an image? I am new at MatLab and currently thinking about if it is possible to use this software for my purpose. Do you know any similar projects or tutorials, which could help me further?
PS: I attached an example image, e. g. how can I get the position of all blue rectangles?
Best regards Max

Best Answer

Since your image is indexed, with only 4 colours (0 = reddish, 1 = bluish, 2 = yellowish and 3 = white) it's actually trivial to detects the pixels of each colour:

[img, map] = imread('example.png');
bluepixels = img == 1;

You can then use regionprops to get the pixels, bounding box, perimeter, etc. of the rectangles:

props = regionprops(bluepixels, 'basic');  %will return the bounding box, area and centroid of each blue object.