How to label the blank space in a image ?
2 views (last 30 days)
Show older comments
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
Adam
on 6 Dec 2016
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?
Answers (1)
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
See Also
Categories
Find more on Import, Export, and Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!