MATLAB: Does MATLAB have a way to detect squares in an image

image segmentation

I am given a series of images taken from similar heights and am tasked to count how many coins there are in each picture. I used imfindcircles and completed it but is there a similar approach that can be used to find square/rectangular objects such as dice? If not, then how am I able to get MATLAB to pick out the white in the die and count each one. I attempted to select a few white pixels from the image and use a range to find the die but I didn't know where to go afterward.

Best Answer

If all the images look like the one above, use a single threshold to get a binary mask. Then use regionprops to get the eccentricity of your objects.
img=imread('FILENAME');
mask=img>THRESHOLD;
ecc=regionprops(mask,'eccentricity')
You can get even more properties such as the area and circumference that allow you to categorize objects based on their geometric properties. The details of the implementation however depend on your specific problem.
Note that regionprops also takes a label-matrix as an input which you can use to select the circles inside the dice.
edit: you might need to flatten the color channels using
img=rgb2gray(img);
Related Question