How to find X and O using Image Processing toolbox?

2 views (last 30 days)
Dear All,
here is my problem: I have an image with X and O (of different colors) and I need to determine the position of such objects. Precisely, I want to obtain the centers of the circles and the ones of the crosses.
So far, I was able to do it for the circles using imfindcircles. Any idea? I should mention that each object has fixed dimension.
Best, Michele

Accepted Answer

Image Analyst
Image Analyst on 11 Nov 2014
I don't think the easiest way is not to use a template and run it around the image, but if you do want to use that method, see my attached demo.
By far the easiest way is to simply split the image into red, green, and blue channels.
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
Threshold and combine them to get binary images of just the red and green objects. I'll let you do that simple part. Display them first so it will be easier to determine which you need to combine. There's only 3 lines of code you need to do here.
Then simply call bwlabel and regionprops to get the centroids
labeledImage = bwlabel(binaryImage);
measurements = regionprops(labeledImage, 'Centroid');
  2 Comments
Michele Berra
Michele Berra on 11 Nov 2014
Edited: Michele Berra on 11 Nov 2014
What do you suggest instead? I have to process thousand of images like that one. Thank you for your answer! It works perfectly!
Image Analyst
Image Analyst on 11 Nov 2014
The normxcorr2 method requires a known template that you can scan the image with. The simpler color segmentation method does not. Plus the segmentation method does not require complicated indexing to find the location, which normxcorr2 does because the correlated output image is larger than the input image by the width of the template. So a spot at (100,150) will not show up at 100,150 in the output image - it will be shifted over by the width of the template (or is it half width?). I recommend the color segmentation method I showed in the body of my message. However, it just finds colored spots without regard to their shape. It will find red crossed, squares, diamonds, blobs of any shape. If you really need just crosses and not any other thing that's red, then you should use normxcorr2. Since you don't have any other red shapes, this is not something you have to worry about and you can do the simple color segmentation method. See my File Exchange for more color segmentation demos. http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862

Sign in to comment.

More Answers (1)

yonatan gerufi
yonatan gerufi on 11 Nov 2014
Edited: yonatan gerufi on 11 Nov 2014
if the X and O have fixed dimension and shape, is to build a mask for X and mask for O, and run it on the picture.
  2 Comments
Michele Berra
Michele Berra on 11 Nov 2014
Dear Jonatan, yes the shapes and the dimensions are fixed. What I need is to retrieve in one goal all the centers for the Xs and all the one for the Os. If what you suggest solves this, would you mind to give me some references on how to create a mask for this kind of objects?
Thank you!
Best,
Michele
yonatan gerufi
yonatan gerufi on 11 Nov 2014
Edited: yonatan gerufi on 11 Nov 2014
first you should do your picture to binary one using "im2bw". (make sure you pick a threshold that doesn't turn X & O to zeros)
crop small rectangle around the X, and one around O. those are your masks.
now, you should run all over the picture, and sum the matrix results from multiplying the mask with the current part of the picture (a.k.a 2D convolution) this will give you a value, that will be high if the picture and mask are correlated, and you have an X or O pattern.
hopefully it is understandable. regards.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!