How to label the blank space in a image ?

Hi.I have a problem in identifying an object in a image. I have 9 squares organized like a matrix,3x3,in each square I can have the letter X,number 0 or blank space.I managed to identify the X and 0 with bwlabel and OCR but I can't think of a solution to identify the blank space.If I can recognize the blank space then I can predict if the winner will be X or 0.Any ideas? This image is pretty similar with the images i'm working Image Example . Thanks.

4 Comments

Surely if there are only 3 options and you can identify two of them then whatever is left is the 3rd option?
If you can figure out the number of items in each row or each column, you can subtract it from 3 and get the number of blank spaces.
@Mohammad Taheri,yes,i can find the number of blank spaces but i need the position,not the numbers. @Adam if the line is completely empty,or just 1 x or 0 there how can i figure out if the X/0 it's on (0,1) position,(0,2) or (0,3) ?
Well I don't know how you are identifying it, but I assumed that such identification would come with a location, otherwise how does it count as being identified?

Sign in to comment.

Answers (1)

Cyrus
Cyrus on 6 Dec 2016
Edited: Cyrus on 6 Dec 2016
Here you go:
The matrix named Blank_Matrix has the location of blank squares as 1; else the value is 0.
clear all; close all; clc;
I = imread('xsio.gif');
figure(), imshow(I), title('Input Image')
impixelinfo
Crop_Begin_X = 7;
Crop_Begin_Y = 21;
Width = 219; %%Col
Height = 219; %%Row
%%Cropping the Image.
Cropped_Image = imcrop (I, [Crop_Begin_X, Crop_Begin_Y, Width, Height]);
figure(), imshow(Cropped_Image), title('Cropped_Image')
impixelinfo
%%Convertnig to Black and White
BW_Image = im2bw(Cropped_Image);
figure(), imshow(BW_Image), title('BW_Image')
impixelinfo
[Row, Col] = size(BW_Image);
Crop_Begin_X = 7;
Crop_Begin_Y = 21;
Width = 219; %%Col
Height = 219; %%Row
Blank_Matrix = zeros(3, 3);
Matrix_Row = 0;
Matrix_Col = 0;
Width_of_the_Square = 72;
y=1;
while y + Width_of_the_Square <= Col
Matrix_Col = Matrix_Col+1;
Matrix_Row = 0;
fprintf('Matrix_Col = %d\n', Matrix_Col);
x = 1;
while x + Width_of_the_Square <= Row
Matrix_Row= Matrix_Row+ 1;
fprintf('Matrix_Row = %d\n\n', Matrix_Row);
TMP_title = ' Not Blank' ;
Square = imcrop (BW_Image, [x, y, 72, 72]);
Number_of_White_Pixels = 0;
for i = 1 : Width_of_the_Square
for j = 1 : Width_of_the_Square
if Square(i, j) == 1
Number_of_White_Pixels = Number_of_White_Pixels + 1;
end
end
end
if Number_of_White_Pixels >= 4500
TMP_title = 'Blank';
Blank_Matrix(Matrix_Col, Matrix_Row) = 1;
end
hold on
figure()
imshow(Square), title(TMP_title)
pause(0.5);
hold off
x =x + Width_of_the_Square;
end
y = y + Width_of_the_Square;
end

1 Comment

This is a good solution if the size of the squares are always the same but in my case,i forgot to mention, the squares will be handwritten on a board or a paper and the size will be different almost every time...

Sign in to comment.

Categories

Find more on Display Image in Help Center and File Exchange

Asked:

on 5 Dec 2016

Commented:

on 6 Dec 2016

Community Treasure Hunt

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

Start Hunting!