|
| 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 | + |
0 commit comments