close all; clc; warning off;
load('svmstruct_each_new_algo.mat');
load('template_numbers.mat');
global device_ip;
global mode;
final_time=datenum(clock+[0,0,0,0,0,5]);
firstcall=1;
while(1)
try
%display('i tried again');
final_time=datenum(clock+[0,0,0,0,0,5]);
while(datenum(clock)<final_time)
ss=imread(device_ip);
imshow(ss);
drawnow;
hold on;
RawImg=ss;
%% converting to binary with black background
makebw = @(I) im2bw(I.data,median(double(I.data(:)))/300);
Bin_Img_wBorder = ~blockproc(RawImg,[50 50],makebw);
Bin_Img = imclearborder(Bin_Img_wBorder);
%% find the biggest box and the index
RegionStats = regionprops(bwlabel(Bin_Img),'Area','PixelList');
maxIndx = 0;
maxArea = 0;
for k = 1:numel(RegionStats)
if RegionStats(k).Area > maxArea
maxArea = RegionStats(k).Area;
maxIndx = k;
end
end
%% finding vertices of the corners
vec1 = sum(RegionStats(maxIndx).PixelList,2);
vec2 = diff(RegionStats(maxIndx).PixelList,1,2);
[m,pt1] = min(vec1);
[m,pt2] = max(vec1);
[m,pt3] = min(vec2);
[m,pt4] = max(vec2);
pts = RegionStats(maxIndx).PixelList([pt1 pt3 pt2 pt4 pt1],:);
org=pts;
plot(pts(:,1),pts(:,2),'r','LineWidth',2,'HandleVisibility','on');
pause(0.5);
cla
end
% figure,imshow(ss);
%% performing cp2 transform
transformMatrix = cp2tform([pts(1:end-1,1) pts(1:end-1,2)],[0 0;200 0;200 200;0 200],'projective');
TransformedImg = imtransform(Bin_Img,transformMatrix,'size', size(Bin_Img));
TransformedOriginalImg = imtransform(RawImg,transformMatrix,'size', size(Bin_Img));
%% find the biggest box and the index
RegionStats = regionprops(bwlabel(TransformedImg),'Area','PixelList');
maxIndx = 0;
maxArea = 0;
for k = 1:numel(RegionStats)
if RegionStats(k).Area > maxArea
maxArea = RegionStats(k).Area;
maxIndx = k;
end
end
%% finding vertices of the corners
vec1 = sum(RegionStats(maxIndx).PixelList,2);
vec2 = diff(RegionStats(maxIndx).PixelList,1,2);
[m,pt1] = min(vec1);
[m,pt2] = max(vec1);
[m,pt3] = min(vec2);
[m,pt4] = max(vec2);
pts = RegionStats(maxIndx).PixelList([pt1 pt3 pt2 pt4 pt1],:);
%% Inverse transform
pts_original=pts;
transformInv=cp2tform([pts_original(1:end-1,1) pts_original(1:end-1,2)],[org(1:end-1,1) org(1:end-1,2)],'projective');
TransformedOriginalInv = imtransform(TransformedOriginalImg,transformInv,'size', size(Bin_Img));
%% croping the imag
BigImgcroped = imcrop(TransformedImg,[pts(1,1) pts(1,2) pts(2,1)-pts(1,1) pts(3,2)-pts(2,2)]);
Sudoku_Box_original = imcrop(TransformedOriginalImg,[pts(1,1) pts(1,2) pts(2,1)-pts(1,1) pts(3,2)-pts(2,2)]);
%% Test za global-local threshold
level_test = graythresh(Sudoku_Box_original);
test_box = im2bw(Sudoku_Box_original,level_test);
%% converting to binary with black background
makebw = @(I) im2bw(I.data,median(double(I.data(:)))/1.2/255);
Bin_Img_Local = ~blockproc(Sudoku_Box_original,[70 70],makebw);
Bin_Img_Local = imclearborder(Bin_Img_Local);
% figure,imshow(Bin_Img_Local);
[Sudoku_Crppd_Xsize,Sudoku_Crppd_Ysize]= size(BigImgcroped);
Sudoku_widening_scale= Sudoku_Crppd_Xsize/Sudoku_Crppd_Ysize;
Sudoku_widening_Mtrx= [Sudoku_widening_scale 0 0;0 1 0; 0 0 1];
Sudoku_widening_Trsfrm = maketform('affine',Sudoku_widening_Mtrx);
Sudoku_widened= imtransform(BigImgcroped,Sudoku_widening_Trsfrm);
Sudoku_Original_Widened=imtransform(Bin_Img_Local,Sudoku_widening_Trsfrm);
Sudoku_resize_scale=270/size(Sudoku_widened,1);
Sudoku_Original_Resized=imresize(Sudoku_Original_Widened,Sudoku_resize_scale);
[M,N]=size(BigImgcroped);
scale=M/N;
[a,b]=size(Sudoku_Box_original);
TransMtrx=[scale 0 0;0 1 0; 0 0 1];
t1=maketform('affine',TransMtrx);
BigImgcropedTrnsformed=imtransform(BigImgcroped,t1);
Sudoku_Box_original=imtransform(Sudoku_Box_original,t1);
%% cropping the whole big box
[M1,N1]=size(BigImgcropedTrnsformed);
def_size=9*30; % 30x30 box %insert dimension of box here
scale1=def_size/M1;
BigBoxScaledCropped=imresize(BigImgcropedTrnsformed,scale1);
Sudoku_Box_original=imresize(Sudoku_Box_original,scale1);
[M2,N2]=size(Sudoku_Box_original);
%% initialize detected matrix
detectednumbers=zeros(9);
%% cropping pictures and saving
picsize=def_size/9; % one block of (picsize x picsize) will be declared for
thresholArea=80; % minimum number of pixels in a number
%Cropping templates
for i=2:2 % for all the 9*9 small boxes
for j=5:5
Xstart=(i-1)*picsize+1;
Xend= picsize*i;
Ystart=(j-1)*picsize+1;
Yend= picsize*j;
image_num=Sudoku_Box_original(Xstart+3:Xend,Ystart+3:Yend-3);
end
end
for i=8:8 % for all the 9*9 small boxes
for j=8:8
Xstart=(i-1)*picsize+1;
Xend= picsize*i;
Ystart=(j-1)*picsize+1;
Yend= picsize*j;
image_em=Sudoku_Box_original(Xstart:Xend,Ystart:Yend);
end
end
image_num1=imresize(image_num,20/27);
level1 = graythresh(image_num1);
Bin_Num1 = im2bw(image_num1,level1);
Broj1=bwlabel(~Bin_Num1);
[R1,C1]=find(Broj1==1);
for m1=1:length(R1)
im1(R1(m1),C1(m1))=image_num1(R1(m1),C1(m1));
end
for i=1:9 % for all the 9*9 small boxes
for j=1:9
% cropping small blocks
Xstart=(i-1)*picsize+1;
Xend= picsize*i;
Ystart=(j-1)*picsize+1;
Yend= picsize*j;
image=Sudoku_Original_Resized(Xstart:Xend,Ystart:Yend);
image_org=Sudoku_Box_original(Xstart:Xend,Ystart:Yend);
bez_ivica=imclearborder(image,4);
[CC, num]=bwlabel(image);
[CC_small, num_small]=bwlabel(image(10:25,10:20));
% if there is nothing in box
if (num_small==0)
%box is empty
netimg=zeros(30,20);
netimg2=zeros(30,20);
end
% if there is something in box
if (num_small~=0)
%box has something
if (num_small==1) %if there is only one connected component in the small box
[r_small,c_small]=find(CC_small==1);
flag=CC(10+r_small(1)-1,10+c_small(1)-1);
else
for m=1:num_small %compute the number of pixels in the cc
pixel_num(m)=size(find(CC_small==m),2);
end
[biggest_cc, biggest_ind]=max(pixel_num);
[r_small,c_small]=find(CC_small==biggest_ind);
flag=CC(10+r_small(1)-1,10+c_small(1)-1);
end
switch (flag)
case 1
[r, c] = find(CC==1);
CC(find(CC~=1))=0;
case 2
[r, c] = find(CC==2);
CC(find(CC~=2))=0;
case 3
[r, c] = find(CC==3);
CC(find(CC~=3))=0;
case 4
[r, c] = find(CC==4);
CC(find(CC~=4))=0;
case 5
[r, c] = find(CC==5);
CC(find(CC~=5))=0;
case 6
[r, c] = find(CC==6);
CC(find(CC~=6))=0;
otherwise
warning('Something is wrong');
end
%getting template numbers
if ((i==1) && (j==3))
three_bin=CC; %%CC don't need to be 1
three_org=image_org;
end
if ((i==1) && (j==5))
nine_bin=CC; %%CC don't need to be 1
nine_org=image_org;
end
matab_sudoku_solver.zip_realtime_sudoku solver
版权申诉
141 浏览量
2022-09-23
00:03:37
上传
评论
收藏 408KB ZIP 举报
小贝德罗
- 粉丝: 68
- 资源: 1万+
最新资源
- XILINXFPGA源码Xilinxspratan3xcs100E(VGAPS2)
- XILINXFPGA源码XilinxSPARTAN-3E入门开发板实例
- XILINXFPGA源码XilinxSdramVerilog和VHDL版本文档
- 物联网智能家居方案-基于Nucleo-STM32L073&机智云(大赛作品,文档齐全,可直接运行)(文档加Matlab源码)
- XILINXFPGA源码XilinxISE9.xFPGACPLD设计源码
- 成都市地图含高新区(高新南区,高新西区),天府新区,东部新区虚拟行政区划
- XILINXFPGA源码XilinxEDK设计试验
- XILINXFPGA源码XilinxEDKMicroBlaze内置USB固件程序
- 基于 django 的视频点播后台管理系统源代码+数据库
- 基于Java的网上医院预约挂号系统的设计与实现(部署视频)-kaic.mp4
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈