%function testsegment
clc;clear;
I = imread('E:/8.jpg');
if size(I,3)>1 % 转灰度图
I = rgb2gray(I);
end
I = im2double(I);
H=fspecial('average',floor(size(I)/8)); %% 用滤波函数求图片背景
avgI=imfilter(I,H,'replicate');
I_s = 1 - avgI + I; %%用原图减去背景去阴影
I_s = I_s.^16;
bws = im2bw(I_s,graythresh(I_s)); %二值化
bws = ~bws; %%图像取反
figure;
imshow(bws);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Ie=edge(I,'canny');
S1 = regionprops(Ie,'BoundingBox','PixelIdxList');
max_area = 0;
for i = 1:length(S1) %%%找图像最大连通域ROI
area = S1(i).BoundingBox(3)*S1(i).BoundingBox(4);
if area>max_area
max_area = area;
pos = i;
end
end
bbox = S1(pos).BoundingBox;
figure;
imshow(I(bbox(2):bbox(2)+bbox(4),bbox(1):bbox(1)+bbox(3)))
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ROIbw=zeros(size(I));
ROIbw(S1(pos).PixelIdxList)=Ie(S1(pos).PixelIdxList);
corners = detectHarrisFeatures(ROIbw); %%%角点检测
[c_number,ww]=size(corners);
imshow(I);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
hold on;
plot(corners.selectStrongest(c_number));%%%%%角点标记
hold off
hc = corners.selectStrongest(c_number);
hcxy = hc.Location;
loccir = mean(hcxy); %%%%图像中点
quadrant=[]; %%%%象限
center_x=loccir(:,1);
center_y=loccir(:,2);
for i=1:c_number
quadrant(i)=1;
end
for i=1:c_number %%%%%四个象限初始化
if hcxy(i,1)-center_x>0 && hcxy(i,2)-center_y<=0
quadrant(i)=1;
end
if hcxy(i,1)-center_x<=0 && hcxy(i,2)-center_y<0
quadrant(i)=2;
end
if hcxy(i,1)-center_x<0 && hcxy(i,2)-center_y>=0
quadrant(i)=3;
end
if hcxy(i,1)-center_x>=0 && hcxy(i,2)-center_y>0
quadrant(i)=4;
end
end
point_len=0; %四个点
max_len=0;
quadrant1=0;
quadrant2=0;
quadrant3=0;
quadrant4=0;
for i=1:length(quadrant) %%计算四个象限距离中心点最远的四个点
if quadrant(i)==1 %%第一象限
point_len=sqrt((hcxy(i,1)-center_x)^2+(hcxy(i,2)-center_y)^2);
if max_len<point_len
max_len=point_len;
quadrant1=i;
end
end
end
point_len=0;
max_len=0;
for i=1:length(quadrant)
if quadrant(i)==2 %%第二象限
point_len=sqrt((hcxy(i,1)-center_x)^2+(hcxy(i,2)-center_y)^2);
if max_len<point_len
max_len=point_len;
quadrant2=i;
end
end
end
point_len=0;
max_len=0;
for i=1:length(quadrant)
if quadrant(i)==3 %%第三象限
point_len=sqrt((hcxy(i,1)-center_x)^2+(hcxy(i,2)-center_y)^2);
if max_len<point_len
max_len=point_len;
quadrant3=i;
end
end
end
point_len=0;
max_len=0;
for i=1:length(quadrant)
if quadrant(i)==4 %%第四象限
point_len=sqrt((hcxy(i,1)-center_x)^2+(hcxy(i,2)-center_y)^2);
if max_len<point_len
max_len=point_len;
quadrant4=i;
end
end
end
figure;
imshow(I);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
hold on
plot( center_x,center_y ,'r+'); %%标记四个角点
plot( hcxy(quadrant1,1),hcxy(quadrant1,2) ,'r+');
plot( hcxy(quadrant2,1),hcxy(quadrant2,2) ,'g+');
plot( hcxy(quadrant3,1),hcxy(quadrant3,2) ,'b+');
plot( hcxy(quadrant4,1),hcxy(quadrant4,2) ,'g+');
ROIpoints(1,1)= hcxy(quadrant1,1);
ROIpoints(1,2)= hcxy(quadrant1,2);
ROIpoints(2,1)= hcxy(quadrant2,1);
ROIpoints(2,2)= hcxy(quadrant2,2);
ROIpoints(3,1)= hcxy(quadrant3,1);
ROIpoints(3,2)= hcxy(quadrant3,2);
ROIpoints(4,1)= hcxy(quadrant4,1);
ROIpoints(4,2)= hcxy(quadrant4,2);
X=[510 10 10 510];
Y=[10 10 510 510]; %%%变换后的图像模板大小
tform = fitgeotrans(ROIpoints,[X' Y'],'Projective'); %%图像变换矫正
outputView = imref2d(size(I));
Is = imwarp(I,tform, 'OutputView', outputView);
Is=Is(X(2):X(1),Y(2):Y(3));
figure;
imshow(Is);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
H=fspecial('average',floor(size(Is)/8)); %%矫正后二值化
avgI=imfilter(Is,H,'replicate');
I_s = 1 - avgI + Is;%
I_s = I_s.^16;
bws = im2bw(I_s,graythresh(I_s));
bws=~bws;
figure;
imshow(bws);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
I_tem=bws(172:361,6:497); %%选择题区域提取
% I_show=I_tem;%%%%%%%%%%%%%%%%%%
I_tem= bwareaopen(I_tem,50); %%过滤噪声点
figure;
imshow(I_tem);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure;
imshow(I_tem);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%% choose %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
answer=cell(1,50);
choose={'A','B','C','D'}; %%%%%%选择题识别,遍历像素点
for i=1:3
for j=1:4
for k=1:5
for p=(j-1)*124+32:26:(j-1)*124+32+(26*3)
is_done=I_tem((i-1)*66+4+(k-1)*13,p);
if is_done==1
a_num=(i-1)*20+(j-1)*5+k;
answer(a_num)= choose(floor((p-((j-1)*124+32))/26)+1);
end
end
end
end
end
bws=bwareaopen(bws,10); %%%试卷号和身份证号区域提取
S2 = regionprops(bws,'BoundingBox','Area');
bwArea=cat(1,S2.Area);
[bwAreasort,idx]=sort(bwArea);
bbx1=round(S2(idx(end-2)).BoundingBox);
bbx2=round(S2(idx(end-1)).BoundingBox);
bwss1=bws(bbx1(2):bbx1(2)+bbx1(4),bbx1(1):bbx1(1)+bbx1(3));
bwss2=bws(bbx2(2):bbx2(2)+bbx2(4),bbx2(1):bbx2(1)+bbx2(3));
figure(7);
subplot(121);imshow(bwss1);title('paper number');
subplot(122);imshow(bwss2);title('ID');
L1 = bwlabel(bwss1);
L2 = bwlabel(bwss2);
S1 = regionprops(L1,'BoundingBox');
S2 = regionprops(L2,'BoundingBox');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% paper num %%%%%%%%%%%%%%%%%%%%%%%%
%文件操作
cd('E:\'); % 设置当前目录
if ~exist('paper')
rmdir('paper','s'); % 删除文件夹
end
folderName = 'paper';
mkdir(folderName); % 新建一个文件夹
for i=2:length(S1) %%数字分割
s1 = sprintf('E:/paper/num(%d).jpg',i-1);
tem_img = imcrop(bwss1,S1(i).BoundingBox);
imwrite(~tem_img,s1,'jpg');
end
paper_value=[];
for id_index=1:6 %%数字分割
s1 = sprintf('E:/paper/num(%d).jpg',id_index);
tem_img=imread(s1);
[m,n]=size(tem_img);
if n<7
paper_value(id_index)=1;
continue;
end
tem_img=imresize(tem_img,[25,15]);%图像大小调整
thresh = graythresh(tem_img); %二值化
bw = im2bw(tem_img,thresh);
flag1=0;%%%%分别检测数字的
没有合适的资源?快使用搜索试试~ 我知道了~
matlab实现对机读卡识别
共58个文件
jpg:54个
ds_store:2个
m:1个
4星 · 超过85%的资源 需积分: 5 49 下载量 103 浏览量
2018-02-19
17:22:57
上传
评论 1
收藏 107.72MB ZIP 举报
温馨提示
matlab 2016a完美运行,本资源主要实现对手机拍摄的机读卡照片进行图像识别,识别图中的身份信息及选择题答案等
资源推荐
资源详情
资源评论
收起资源包目录
matlab机读卡识别.zip (58个子文件)
matlab机读卡识别
测试图片
试卷较脏,左边距大于右边距.jpg 155KB
._.DS_Store 4KB
._试卷较脏,左边距大于右边距.jpg 4KB
图3.png 156KB
.DS_Store 6KB
1
ipadair (1).jpg 2.96MB
苹果6s (14).JPG 1.81MB
ipadair (16).jpg 2.95MB
苹果6s (11).JPG 1.88MB
ipadair (4).jpg 2.96MB
ipadair (6).jpg 2.95MB
苹果6s (1).JPG 1.8MB
苹果6s (7).JPG 1.83MB
苹果6s (4).JPG 1.86MB
ipadair (19).jpg 2.94MB
ipadair (8).jpg 2.86MB
._苹果6s (24).JPG 4KB
._ipadair (21).jpg 4KB
苹果6s (15).JPG 1.79MB
ipadair (21).jpg 2.73MB
苹果6s (23).JPG 1.85MB
ipadair (13).jpg 2.97MB
ipadair (3).jpg 2.9MB
ipadair (11).jpg 2.96MB
苹果6s (20).JPG 1.9MB
._ipadair (19).jpg 4KB
苹果6s (24).JPG 2.02MB
苹果6s (16).JPG 1.82MB
苹果6s (12).JPG 1.87MB
苹果6s (8).JPG 1.86MB
._苹果6s (1).JPG 4KB
苹果6s (6).JPG 1.85MB
ipadair (2).jpg 2.96MB
苹果6s (17).JPG 1.8MB
ipadair (18).jpg 2.84MB
ipadair (14).jpg 2.98MB
ipadair (15).jpg 2.89MB
苹果6s (10).JPG 1.85MB
ipadair (17).jpg 2.9MB
苹果6s (25).JPG 1.97MB
苹果6s (9).JPG 1.87MB
苹果6s (5).JPG 1.84MB
ipadair (5).jpg 2.94MB
苹果6s (26).JPG 1.83MB
ipadair (9).jpg 2.89MB
苹果6s (19).JPG 1.84MB
ipadair (20).jpg 2.85MB
苹果6s (22).JPG 1.9MB
苹果6s (13).JPG 1.81MB
ipadair (10).jpg 2.93MB
ipadair (12).jpg 2.96MB
ipadair (7).jpg 2.83MB
苹果6s (3).JPG 1.82MB
苹果6s (18).JPG 1.82MB
苹果6s (2).JPG 1.81MB
苹果6s (21).JPG 1.91MB
答题卡上有重影.jpg 163KB
matlab机读卡识别.m 18KB
共 58 条
- 1
资源评论
- qq_458964662020-12-03代码不能正常运行,想加个联系方式请教一下大佬
- 小太阳的向日葵2018-08-25写得不错。
Tron_future
- 粉丝: 1854
- 资源: 8
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功