Skip to content

Commit f3e26bc

Browse files
committed
Has all user-defined functions and algortihm functions
1 parent af72bfc commit f3e26bc

12 files changed

+350
-0
lines changed

CheckForEmptyBox.m

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function[count]=CheckForEmptyBox(ANSWER)
2+
count=0;
3+
for x3=1:9
4+
for y3=1:9
5+
if(ANSWER(x3,y3)=='0')
6+
count=count+1;
7+
end
8+
end
9+
end
10+
end

CheckForSUDOKUconsistency.m

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
function[flag]=CheckForSUDOKUconsistency(ANSWER)
2+
flag=0;
3+
for i=1:9
4+
k=1;
5+
l=1;
6+
for j=1:9
7+
if ANSWER(i,j)~='0'
8+
row(1,k)=ANSWER(i,j);
9+
k=k+1;
10+
end
11+
if ANSWER(j,i)~='0'
12+
column(1,l)=ANSWER(j,i);
13+
l=l+1;
14+
end
15+
end
16+
u_row=unique(row);
17+
u_column=unique(column);
18+
srow=size(row);
19+
su_row=size(u_row);
20+
scolumn=size(column);
21+
su_column=size(u_column);
22+
if ((srow(2)~=su_row(2)) || (scolumn(2)~=su_column(2)))
23+
flag = 1;
24+
return
25+
end
26+
for minimumarray=1:9
27+
elements=CollectElements(minimumarray,ANSWER);
28+
elements=reshape(elements,[1,9]);
29+
i=1;
30+
for a=1:9
31+
if elements(1,a)~='0'
32+
element_array(1,i)=elements(1,a);
33+
i=i+1;
34+
end
35+
end
36+
selement_array=size(element_array);
37+
suelement_array=size(unique(element_array));
38+
if selement_array(2)~=suelement_array(2)
39+
flag=1;
40+
return
41+
end
42+
clear elements;
43+
clear element_array;
44+
end
45+
clear row;
46+
clear column;
47+
clear u_row;
48+
clear u_column;
49+
end
50+
end

CollectElements.m

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
function[miniarray]=CollectElements(n,OUT)
2+
switch n
3+
case 1
4+
for i=1:3
5+
for j=1:3
6+
miniarray(i,j)=OUT(i,j);
7+
end
8+
end
9+
case 2
10+
i1=1;
11+
j1=1;
12+
for i=4:6
13+
for j=1:3
14+
miniarray(i1,j1)=OUT(i,j);
15+
j1=j1+1;
16+
end
17+
i1=i1+1;
18+
j1=1;
19+
end
20+
case 3
21+
i1=1;
22+
j1=1;
23+
for i=7:9
24+
for j=1:3
25+
miniarray(i1,j1)=OUT(i,j);
26+
j1=j1+1;
27+
end
28+
i1=i1+1;
29+
j1=1;
30+
end
31+
case 4
32+
i1=1;
33+
j1=1;
34+
for i=1:3
35+
for j=4:6
36+
miniarray(i1,j1)=OUT(i,j);
37+
j1=j1+1;
38+
end
39+
i1=i1+1;
40+
j1=1;
41+
end
42+
case 5
43+
i1=1;
44+
j1=1;
45+
for i=4:6
46+
for j=4:6
47+
miniarray(i1,j1)=OUT(i,j);
48+
j1=j1+1;
49+
end
50+
i1=i1+1;
51+
j1=1;
52+
end
53+
case 6
54+
i1=1;
55+
j1=1;
56+
for i=7:9
57+
for j=4:6
58+
miniarray(i1,j1)=OUT(i,j);
59+
j1=j1+1;
60+
end
61+
i1=i1+1;
62+
j1=1;
63+
end
64+
case 7
65+
i1=1;
66+
j1=1;
67+
for i=1:3
68+
for j=7:9
69+
miniarray(i1,j1)=OUT(i,j);
70+
j1=j1+1;
71+
end
72+
i1=i1+1;
73+
j1=1;
74+
end
75+
case 8
76+
i1=1;
77+
j1=1;
78+
for i=4:6
79+
for j=7:9
80+
miniarray(i1,j1)=OUT(i,j);
81+
j1=j1+1;
82+
end
83+
i1=i1+1;
84+
j1=1;
85+
end
86+
case 9
87+
i1=1;
88+
j1=1;
89+
for i=7:9
90+
for j=7:9
91+
miniarray(i1,j1)=OUT(i,j);
92+
j1=j1+1;
93+
end
94+
i1=i1+1;
95+
j1=1;
96+
end
97+
end
98+
end

FilterAndOCR.m

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
%first finding sudoku grid and cropping it. but need to filter image to show clear grid
2+
3+
image = imread('sudokuexample.jpg'); %read imagefile
4+
adjustedimage=imadjust(image,[0.4 0.7],[0.0 1.0]); %adjusting contrast as per histogram analysis
5+
grayimage=rgb2gray(adjustedimage); %convert to gray image
6+
removeborder=imclearborder(grayimage); %remove elements beyond border
7+
binarymask=removeborder>50; %convert gray image to a binary mask
8+
binarymask2=imopen(binarymask,strel('disk',5)); %remove noises using morpholgical functions
9+
connComp=bwconncomp(binarymask2); %find connected components in image
10+
stats=regionprops(connComp,'Area'); %store property 'Area' of connect components in stats variable
11+
mask=binarymask2; %make a mask of binarymask2
12+
mask(vertcat(connComp.PixelIdxList{[stats.Area]<3000}))=0; %remove all connected components pixels whose area < 3000 pixels in stats
13+
[x y]=find(mask); %find all pixels x and y coordinate whose pixel value is 1
14+
15+
%find rectangle of sudoku box to be cropped. so we find (xmin,ymin)
16+
17+
xmin=min(x); %find least x coordinate
18+
xmax=max(x); %find max x coordinate
19+
ymin=min(y); %find least y coordinate
20+
ymax=max(y); %find max y coordinate
21+
crop=imcrop(grayimage,[ymin xmin ymax-ymin xmax-xmin]); %crop using xmin ymin and length and width of crop area
22+
finalbinary=crop>90; %convert cropped image to binary with required threshold
23+
finalbinary=~finalbinary; %negate the image
24+
finalbinary=imopen(finalbinary,strel('square',3)); %disintegrates grid lines of image captured so area becomes either less or very high.
25+
26+
%final process on finalbinary before applyin ocr
27+
connComp2=bwconncomp(finalbinary); %find connected objects and filter out which are not in area range
28+
stats2=regionprops(connComp2,'Area');
29+
finalbinarymask=finalbinary;
30+
finalbinarymask(vertcat(connComp2.PixelIdxList{[stats2.Area]<825 | [stats2.Area]>2500}))=0;
31+
out=imerode(finalbinarymask,strel('disk',3)); %filter out remaining bug pixels
32+
figure, imshow(out); %final output image
33+
34+
SettingUpAGrid;
35+
ocrtext=ocr(out,roi,'TextLayout','Block','CharacterSet','123456789'); %perform ocr and consider onlu 0-9 for recognition
36+
ocrtext.Text; %display text recognised
37+

FindMinArray.m

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
function[mininumber] = FindMinArray(col,row)
2+
if(col>0 && col<4)
3+
if(row>0 && row<4)
4+
mininumber=1;
5+
elseif(row>3 && row<7)
6+
mininumber=4;
7+
elseif(row>6 && row<10)
8+
mininumber=7;
9+
end
10+
elseif(col>3 && col<7)
11+
if(row>0 && row<4)
12+
mininumber=2;
13+
elseif(row>3 && row<7)
14+
mininumber=5;
15+
elseif(row>6 && row<10)
16+
mininumber=8;
17+
end
18+
elseif(col>6 && col<10)
19+
if(row>0 && row<4)
20+
mininumber=3;
21+
elseif(row>3 && row<7)
22+
mininumber=6;
23+
elseif(row>6 && row<10)
24+
mininumber=9;
25+
end
26+
end
27+
end

MoreAccurateSpace.m

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
function[minimumarray]=MoreAccurateSpace(ANSWER)
2+
i5=1;
3+
4+
for x3=1:9 %this loop finds an array of all
5+
for y3=1:9 %coordinates of empty box and size of possible elements in them
6+
if(ANSWER(x3,y3)=='0')
7+
min_array=FindMinArray(x3,y3);
8+
elements=CollectElements(min_array,ANSWER);
9+
possible_elements=PossibleValues(elements,x3,y3,ANSWER);
10+
s=size(possible_elements);
11+
possible_element_array(i5,1)=x3;
12+
possible_element_array(i5,2)=y3;
13+
possible_element_array(i5,3)=s(2);
14+
i5=i5+1;
15+
end
16+
end
17+
end
18+
s2=size(possible_element_array); %find minimum size of possible elements
19+
minimum=min(possible_element_array(:,3));
20+
j=1;
21+
clear minimumarray;
22+
for i=1:s2(1)
23+
if (minimum==possible_element_array(i,3)) %return only minimum size of possible elements with coordinates
24+
minimumarray(j,1)=possible_element_array(i,1);
25+
minimumarray(j,2)=possible_element_array(i,2);
26+
minimumarray(j,3)=possible_element_array(i,3);
27+
j=j+1;
28+
end
29+
end
30+
end

PossibleValues.m

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
function[possvalues]=PossibleValues(array,x,y,OUT)
2+
j=1;
3+
for i=1:9
4+
if(array(i)~='0')
5+
values(j)=array(i);
6+
j=j+1;
7+
end
8+
end
9+
j=j-1;
10+
k=1;
11+
for i=1:9
12+
if(OUT(x,i)~='0')
13+
values(j+k)=OUT(x,i);
14+
k=k+1;
15+
end
16+
end
17+
k=k-1;
18+
l1=1;
19+
for i=1:9
20+
if(OUT(i,y)~='0')
21+
values(j+k+l1)=OUT(i,y);
22+
j=j+1;
23+
end
24+
end
25+
valuessorted=unique(sort(values));
26+
set=['1','2','3','4','5','6','7','8','9'];
27+
possvalues=setxor(valuessorted,set);
28+
end

SettingOutputMatrix.m

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
%This script arranges all OCR(Optical Character Recognize) cells into an
2+
%array and outputs as OUT.
3+
for i=1:81
4+
a=isempty(ocrtext(i).Words);
5+
if(a==1)
6+
word(i)={'0'};
7+
else word(i)=ocrtext(i).Words;
8+
end
9+
end
10+
wordMatrix=cell2mat(word);
11+
o=1;
12+
for i=1:9
13+
for j=1:9
14+
OUT(j,i)=wordMatrix(o);
15+
o=o+1;
16+
end
17+
end
18+
19+
%corrections
20+
OUT(8,1)='5';

SettingUpAGrid.m

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[row column]=size(out);
2+
r_round=round((row)/9);
3+
c_round=round((column)/9);
4+
u=1;
5+
for i=0:8
6+
for j=0:8
7+
xinit= i*c_round+20; %xcoordinate is constant
8+
yinit= j*r_round+35;
9+
roi(u,1:4)=[xinit yinit 125 125];
10+
u=u+1;
11+
end
12+
end
13+

SolveBoxes.m

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
%This function does actual solving of SUDOKU
2+
function[ANSWER,nextcandidate,changecoordinates]=SolveBoxes(minimumarray,ANSWER)
3+
s3=size(minimumarray);
4+
for i=1:s3(1)
5+
min_array=FindMinArray(minimumarray(i,1),minimumarray(i,2)); %Finds which smaller 3x3 array where element (x,y) belongs
6+
elements=CollectElements(min_array,ANSWER); %extract that 3x3array
7+
possible_elements=PossibleValues(elements,minimumarray(i,1),minimumarray(i,2),ANSWER);%finds XOR of all elements in row,column and 3x3 array with 1-9 characters
8+
ANSWER(minimumarray(i,1),minimumarray(i,2))=possible_elements(1,1);%replace coordinate on ANSWER with singleton element
9+
if numel(possible_elements)>1 %if possible elements were more than 1
10+
nextcandidate=possible_elements(1,2); %store next element for further swapping if required
11+
changecoordinates=[minimumarray(i,1) minimumarray(i,2)]; %store coordinates of supposition change
12+
return
13+
end
14+
end
15+
end

SolvingAlgo.m

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
FilterAndOCR; %ImageProcess and fix bugs maunally
2+
SettingOutputMatrix; %Set a Matrix to be fed to SudokuSolver
3+
ANSWER=OUT; %ANSWER is answer matrix while OUT is question matrix
4+
count=CheckForEmptyBox(ANSWER);
5+
while count ~= 0
6+
count=0;
7+
i5=1;
8+
k=1;
9+
minimumarray=MoreAccurateSpace(ANSWER); %Function finds all coordinates where least no. of elements can exist
10+
if minimumarray(1,3)==1 %minimumarray[row col no.of_possible_elements]
11+
ANSWER=SolveBoxes(minimumarray,ANSWER); %Solves boxes with 1 element possibilty
12+
else
13+
%minimumarray=MoreAccurateSpaceIfNot1(ANSWER);
14+
%ANSWER=SolveBoxes(minimumarray,ANSWER);
15+
break;
16+
end
17+
count=CheckForEmptyBox(ANSWER); %counts number of empty boxes
18+
end

subset.m

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
function[flag]=subset(U,B)
2+
u=union(U,B);
3+
flag=isequal(u,U);
4+
end

0 commit comments

Comments
 (0)