# [card recognition] gray binary card recognition [Matlab 464]

Keywords: Python MATLAB image processing

## 1, Introduction

1 grayscale
The process of converting color image into gray image is called image graying. The pixel value in the color image is determined by the three RGB components, and each component has 0-255 (256) choices. In this way, the pixel value of a pixel can have 16 million possibilities (256256256), while the pixel value of the pixel of the gray image is a special color image with the same three RGB component values, and there are only 256 possibilities. Therefore, in image processing, various images are often grayed into grayscale images first for subsequent processing and reduce the amount of calculation. Grayscale refers to an image that contains only brightness information and no color information. Black and white photos are grayscale images, which are characterized by continuous changes in brightness from dark to light. Like the color image, the description of gray image still reflects the distribution and characteristics of overall and local chromaticity and brightness levels of the whole image,

Benefits of using grayscale images:
① RGB values are the same.
② The image data, that is, the palette index value, is the actual RGB value, that is, the brightness value.
③ Because it is a 256 tone palette, a byte in the image data represents a pixel, which is very neat.
Therefore, gray image is generally used in image processing.
To represent a grayscale image, you need to quantify the brightness value. There are four methods:

(1) Component method
Taking the brightness of the three components in the color image as the gray value of the three gray images, a gray image can be selected according to the application needs.
(2) Maximum method
The maximum value of the three component brightness in the color image is taken as the gray value of the gray image.
(3) Mean method
The three component brightness in the color image is averaged to obtain the gray value of the gray image.
(4) Weighted average method
According to the importance and other indicators, the three components are weighted and averaged with different weights. Because human eyes are most sensitive to green and least sensitive to blue, a reasonable gray image can be obtained by weighted average of RGB three components according to the following formula, f(i,j)=0.30R(i,j)+0.59G(i,j)+0.11B(i,j)).

2 binarization
Image binarization is to set the gray value of pixels on the image to 0 or 255, that is, the whole image presents an obvious black-and-white effect. A binary image that can still reflect the overall and local characteristics of the image is obtained by selecting an appropriate threshold for 256 gray-scale images. All pixels whose gray level is greater than or equal to the threshold value are determined to belong to a specific object, and their gray level value is 255. Otherwise, these pixels are excluded from the object area, and the gray level value is 0, indicating the background or exceptional object area. In digital image processing, binary image plays a very important role. Firstly, image binarization is conducive to further image processing, makes the image simple, reduces the amount of data, and can highlight the contour of the target of interest. Secondly, to process and analyze the binary image, first binarize the gray image to get the binary image.
Common binarization algorithms include:

Global binarization: an image includes target object, background and noise. In order to directly extract target object from multi valued digital image, the most commonly used method is to set a global threshold T, and use t to divide the image data into two parts: pixel group larger than T and pixel group smaller than t. Set the pixel value of the pixel group greater than t to white (or black), and the pixel value of the pixel group less than t to black (or white). Global binarization has great defects in representing image details. In order to make up for this defect, local binarization method appears.

Local binarization: divide the whole image into n windows according to certain rules. For each of the N windows, divide the pixels in the window into two parts according to a unified threshold T for binarization. Local binarization also has a defect. The flaw lies in the selection of the uniform threshold. This threshold is obtained without reasonable calculation. Generally, it is the draw value of the window. This leads to the defect of global Binarization in every window. In order to solve this problem, local adaptive binarization method appears.

Local adaptive binarization: on the basis of local binarization, the setting of threshold is more reasonable. The threshold value of this method is calculated by setting a parameter equation for various local characteristics such as the average value E of pixels in the window, the square P of the difference between pixels and the root mean square value Q between pixels, for example: T=aE+bP+c*Q, where a, B and C are free parameters. In this way, the binary image can better show the details in the binary image.

3 reverse
The actual meaning of reverse color is to reverse the R, G and b values. If the quantization level of the color is 256, the R, G and b values of the new image are 255 minus the R, G and b values of the original image. This is for all images, including true color images, color images with color palette (also known as pseudo color images), and gray images. The true color map does not have a palette. Each pixel uses 3 bytes to represent the three components of R, G and B. Therefore, the processing is very simple. Write the inverted R, G and b values into the new graph. For example, the color of a point is (0,0,0), and the reverse color is (255255255). For a color map with a palette, the data in the bitmap is only an index value corresponding to the palette. We only need to reverse the colors in the palette to form a new palette, and the bitmap data can be reversed without moving.

## 2, Partial source code

```function varargout = poker(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
'gui_Singleton',  gui_Singleton, ...
'gui_OpeningFcn', @poker_OpeningFcn, ...
'gui_OutputFcn',  @poker_OutputFcn, ...
'gui_LayoutFcn',  [] , ...
'gui_Callback',   []);
if nargin & isstr(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% --------------------------------------------------------------------
function poker_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);

global IMAGE;global POKER;global GRAY;global BW;global BWSTR;
IMAGE=0;
POKER=0;
GRAY=0;
BW=0;
BWSTR=0;%The global variable is initialized to 0
% --------------------------------------------------------------------
function varargout = poker_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
% --------------------------------------------------------------------
function file_Callback(hObject, eventdata, handles)
% --------------------------------------------------------------------
function new_Callback(hObject, eventdata, handles)
global IMAGE;
global POKER;
global GRAY;
global BW;
global BWSTR;

name=0;
[name,path]=uigetfile({'*.bmp';'*.jpg';'*,tif'},'Open image');
if name==0;
return;%No images open
end

POKER=0;GRAY=0;BW=0;BWSTR=0;%Other image reset

subplot(1,1,1),imshow(IMAGE),title('Poker card image');
% --------------------------------------------------------------------
function operate_Callback(hObject, eventdata, handles)
% --------------------------------------------------------------------
function rectify_Callback(hObject, eventdata, handles)
global IMAGE;
global POKER;

if IMAGE==0;%Image not open
msgbox('Please open a poker image first','error','error');
else
tic;%Calculation of correction and positioning time
POKER=rectify(IMAGE);
if toc>5%Set the timeout to 3 seconds
msgbox('Image correction positioning timeout','error','error');
return;
end
time=num2str(toc);
str=strcat('Correction and positioning time',time,'second');
subplot(1,1,1),imshow(POKER),title('Playing cards');
msgbox(str,'news');
end
% --------------------------------------------------------------------
function gray_Callback(hObject, eventdata, handles)
global IMAGE;
global POKER;
global GRAY;

if IMAGE==0%Image not open
msgbox('Please open a poker image first','error','error');
elseif POKER==0%No image correction
msgbox('Please correct and locate the image first','error','error');
else
GRAY=rgb2gray(POKER);
subplot(1,1,1),imshow(GRAY),title('Image graying');
end
% --------------------------------------------------------------------
function binary_Callback(hObject, eventdata, handles)
global IMAGE;
global POKER;
global GRAY;
global BW;

if IMAGE==0%Image not open
msgbox('Please open a poker image first','error','error');
elseif POKER==0%No image correction
msgbox('Please correct and locate the image first','error','error');
elseif GRAY==0%No image graying
else
bw=im2bw(GRAY,ostu(GRAY));
BW=logical(abs(double(bw)-1));
subplot(1,1,1),imshow(BW),title('image binaryzation');
end
% --------------------------------------------------------------------
function getsymstr_Callback(hObject, eventdata, handles)
global IMAGE;
global POKER;
global GRAY;
global BW;
global BWSTR;

if IMAGE==0%Image not open
msgbox('Please open a poker image first','error','error');
elseif POKER==0%No image correction
msgbox('Please correct and locate the image first','error','error');
elseif GRAY==0%No image graying
elseif BW==0%Image binarization is not performed
else
[m,n]=size(GRAY);
pokerstr=GRAY(2:m/2,n/20:n/5.5);%Rough character positioning
bw=im2bw(pokerstr,ostu(pokerstr));
bw1=bwmorph(bw,'clean');%Clear outliers
bw2=logical(abs(double(bw)-1));%Binary image inversion
bw3=reduce(bw2);%Remove the influence of continuous non jumping edge,Custom function reduce
[m,n]=size(bw3);
for i=2:n+1
for j=1:m
bw3(j,i-1)=0;%Delete orphan line column
end
end
end
[m,n]=find(bw3);
BWSTR=bw3(min(m):max(m),min(n):max(n));
subplot(1,1,1),imshow(BWSTR),title('Card character');
end
% --------------------------------------------------------------------
function recognition_Callback(hObject, eventdata, handles)
global IMAGE;
global POKER;
global GRAY;
global BW;
global BWSTR

%Card character template,34*22 Projection
str1=[6 6 6 6 8 8 10 10 10 10 8 8 8 8 10 10 8 8 8 8 12 12 16 16 12 12 8 8 8 8 16 16 16 16];%A
str2=[10 10 16 16 12 12 12 12 12 12 6 6 4 4 8 8 8 8 6 6 6 6 6 6 6 6 8 8 10 10 22 22 22 22];%2
str3=[22 22 14 14 10 10 8 8 8 8 8 12 12 14 14 6 6 4 4 4 4 4 4 4 4 8 8 8 12 12 18 18 12 12];%3
str4=[2 2 4 4 6 6 8 8 10 10 12 12 10 10 10 10 8 8 10 10 12 12 20 20 22 22 6 6 6 6 6 6 8 8];%4
str5=[18 18 16 16 4 4 4 4 4 16 16 16 16 6 6 4 4 4 4 4 4 4 4 4 4 10 12 12 16 16 12 12 2 2];%5
str6=[9 9 15 15 7 7 5 5 3 3 5 5 14 14 18 18 12 12 9 9 9 9 11 11 11 11 7 7 10 10 14 14 11 11];%6
str7=[15 15 22 22 14 14 9 9 5 5 5 5 5 5 4 4 4 4 6 6 4 4 4 4 6 6 4 4 4 4 4 4 4 4];%7
str8=[10 10 16 16 10 10 6 6 8 8 6 6 12 12 14 14 14 14 10 10 8 8 8 8 8 8 8 8 14 14 18 18 10 10];%8
str9=[14 14 16 16 8 8 8 8 12 12 12 10 10 12 12 12 12 18 18 16 16 4 4 4 4 6 6 6 12 12 14 14 8 8];%9
str10=[10 10 16 16 14 14 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 16 16 16 16 10 10];%10
str11=[13 13 11 11 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 9 9 9 9 14 14 13 13 4 4];%J
str12=[11 11 10 10 10 10 6 6 6 6 6 6 6 6 6 6 6 10 10 14 14 16 16 12 12 10 10 10 10 10 18 18 12 12];%Q
str13=[20 20 20 20 12 12 10 10 10 10 10 10 8 8 12 12 12 12 12 12 10 10 10 10 10 10 8 8 10 10 18 18 20 20];%K
STR=[str1;str2;str3;str4;str5;str6;str7;str8;str9;str10;str11;str12;str13];

%Card pattern template,24*20 Projection
style1=[4 4 6 6 8 8 12 12 16 16 16 16 20 20 20 20 20 20 14 14 6 6 6 6];%spade
style2=[12 12 20 20 20 20 20 20 18 18 16 16 14 14 12 12 8 8 6 6 4 4 2 2];%heart
style3=[4 4 8 8 10 10 10 10 8 8 16 16 20 20 20 20 20 20 12 12 4 4 4 4];%Plum blossom
style4=[2 2 4 4 6 6 12 12 16 16 20 20 20 20 16 16 12 12 6 6 6 6 2 2];%Square slice
STYLE=[style1;style2;style3;style4];

if IMAGE==0%Image not open
msgbox('Please open a poker image first','error','error');
elseif POKER==0%No image correction
msgbox('Please correct and locate the image first','error','error');
elseif GRAY==0%No image graying
elseif BW==0%Image binarization is not performed
elseif  BWSTR==0%Feature not extracted
else
sym=imclose(BWSTR,strel('disk',3));%Rough estimation of characteristic area
if max(shadow)>=40%Too large area,Identified as JOKER
result=strcat('Identification results:','JOKER');
msgbox(result,'news','warn');
else
sym=bwmorph(BWSTR,'clean');%Clear outliers
for i=2:len-1%Search the dividing point between numeric symbols and flower symbols
if shadow(i)~=0&shadow(i+1)==0%A boundary close to the side of the number is detected
bonder1=i+1;
i=bonder1;
continue;
end
```

## 4, matlab version and references

1 matlab version
2014a

2 references
[1] Cai Limei. MATLAB image processing theory, algorithm and example analysis [M]. Tsinghua University Press, 2020
[2] Yang Dan, Zhao Haibin, long Zhe. Detailed explanation of MATLAB image processing examples [M]. Tsinghua University Press, 2013
[3] Zhou pin. MATLAB image processing and graphical user interface design [M]. Tsinghua University Press, 2013
[4] Liu Chenglong. Proficient in MATLAB image processing [M]. Tsinghua University Press, 2015

Posted by mykmallett on Wed, 01 Sep 2021 21:47:38 -0700