飞道的博客

【图像识别】身份证号码识别matlab源码

301人阅读  评论(0)

简介:

       本文主要介绍几种基于灰度的图像匹配算法:平均绝对差算法(MAD)、绝对误差和算法(SAD)、误差平方和算法(SSD)、平均误差平方和算法(MSD)、归一化积相关算法(NCC)、序贯相似性检测算法(SSDA)、hadamard变换算法(SATD)。下面依次对其进行讲解。

MAD算法

介绍

        平均绝对差算法(Mean Absolute Differences,简称MAD算法),它是Leese1971年提出的一种匹配算法。是模式识别中常用方法,该算法的思想简单,具有较高的匹配精度,广泛用于图像匹配。

S(x,y)是大小为mxn的搜索图像,T(x,y)MxN的模板图像,分别如下图(a)(b)所示,我们的目的是:在(a)中找到与(b)匹配的区域(黄框所示)。

算法思路

        在搜索图S中,以(i,j)为左上角,取MxN大小的子图,计算其与模板的相似度;遍历整个搜索图,在所有能够取到的子图中,找到与模板图最相似的子图作为最终匹配结果。

        MAD算法的相似性测度公式如下。显然,平均绝对差D(i,j)越小,表明越相似,故只需找到最小的D(i,j)即可确定能匹配的子图位置:

其中:

算法评价:

优点:

思路简单,容易理解(子图与模板图对应位置上,灰度值之差的绝对值总和,再求平均,实质:是计算的是子图与模板图的L1距离的平均值)。

运算过程简单,匹配精度高。

缺点:

运算量偏大。

对噪声非常敏感。

——————————————————————————————————————————————————————————————————————————————

SAD算法

介绍

        绝对误差和算法(Sum of Absolute Differences,简称SAD算法)。实际上,SAD算法与MAD算法思想几乎是完全一致,只是其相似度测量公式有一点改动(计算的是子图与模板图的L1距离),这里不再赘述。


  
  1. function varargout = homework2(varargin)
  2. % HOMEWORK2 M-file for homework2.fig
  3. % HOMEWORK2, by itself, creates a new HOMEWORK2 or raises the existing
  4. % singleton*.
  5. %
  6. % H = HOMEWORK2 returns the handle to a new HOMEWORK2 or the handle to
  7. % the existing singleton*.
  8. %
  9. % HOMEWORK2('CALLBACK',hObject,eventData,handles,...) calls the local
  10. % function named CALLBACK in HOMEWORK2.M with the given input arguments.
  11. %
  12. % HOMEWORK2('Property','Value',...) creates a new HOMEWORK2 or raises the
  13. % existing singleton*. Starting from the left, property value pairs are
  14. % applied to the GUI before homework2_OpeningFcn gets called. An
  15. % unrecognized property name or invalid value makes property application
  16. % stop. All inputs are passed to homework2_OpeningFcn via varargin.
  17. %
  18. % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
  19. % instance to run (singleton)".
  20. %
  21. % See also: GUIDE, GUIDATA, GUIHANDLES
  22. % Edit the above text to modify the response to help homework2
  23. % Last Modified by GUIDE v2.5 20-May-2013 21:21:00
  24. % Begin initialization code - DO NOT EDIT
  25. gui_Singleton = 1;
  26. gui_State = struct( 'gui_Name', mfilename, ...
  27. 'gui_Singleton', gui_Singleton, ...
  28. 'gui_OpeningFcn', @homework2_OpeningFcn, ...
  29. 'gui_OutputFcn', @homework2_OutputFcn, ...
  30. 'gui_LayoutFcn', [] , ...
  31. 'gui_Callback', []);
  32. if nargin && ischar(varargin{ 1})
  33. gui_State.gui_Callback = str2func(varargin{ 1});
  34. end
  35. if nargout
  36. [varargout{ 1:nargout}] = gui_mainfcn(gui_State, varargin{:});
  37. else
  38. gui_mainfcn(gui_State, varargin{:});
  39. end
  40. % End initialization code - DO NOT EDIT
  41. % --- Executes just before homework2 is made visible.
  42. function homework2_OpeningFcn(hObject, eventdata, handles, varargin)
  43. % This function has no output args, see OutputFcn.
  44. % hObject handle to figure
  45. % eventdata reserved - to be defined in a future version of MATLAB
  46. % handles structure with handles and user data (see GUIDATA)
  47. % varargin command line arguments to homework2 (see VARARGIN)
  48. % Choose default command line output for homework2
  49. handles.output = hObject;
  50. % Update handles structure
  51. guidata(hObject, handles);
  52. % UIWAIT makes homework2 wait for user response (see UIRESUME)
  53. % uiwait(handles.figure1);
  54. % --- Outputs from this function are returned to the command line.
  55. function varargout = homework2_OutputFcn(hObject, eventdata, handles)
  56. % varargout cell array for returning output args (see VARARGOUT);
  57. % hObject handle to figure
  58. % eventdata reserved - to be defined in a future version of MATLAB
  59. % handles structure with handles and user data (see GUIDATA)
  60. % Get default command line output from handles structure
  61. varargout{ 1} = handles.output;
  62. %载入原始身份证图像的回调函数
  63. % --- Executes on button press in OriginalImg.
  64. function OriginalImg_Callback(hObject, eventdata, handles)
  65. % hObject handle to OriginalImg (see GCBO)
  66. % eventdata reserved - to be defined in a future version of MATLAB
  67. % handles structure with handles and user data (see GUIDATA)
  68. [FileName,PathName] = uigetfile( '*.jpg', 'Select an image');
  69. if PathName~= 0
  70. str = [PathName,FileName];
  71. T=imread(str);
  72. axes(handles.Img);
  73. imshow(T);
  74. end
  75. %图像自动亮度调整的回调函数
  76. % --- Executes on button press in autoLight.
  77. function autoLight_Callback(hObject, eventdata, handles)
  78. % hObject handle to autoLight (see GCBO)
  79. % eventdata reserved - to be defined in a future version of MATLAB
  80. % handles structure with handles and user data (see GUIDATA)
  81. axes(handles.Img);
  82. T=getimage;
  83. low_out= 0.2; high_out= 0.9;
  84. gamma= 1.518;
  85. hsv=rgb2hsv(T);
  86. I=hsv(:,:, 3);
  87. minL=min(min(I));
  88. maxL=max(max(I));
  89. J=imadjust(I,[minL;maxL],[low_out;high_out],gamma);
  90. hsv(:,:, 3)=J;
  91. rgb_atuoI=hsv2rgb(hsv);
  92. axes(handles.Light);
  93. imshow(rgb_atuoI);
  94. %图像二值化的回调函数
  95. % --- Executes on button press in DIP.
  96. function DIP_Callback(hObject, eventdata, handles)
  97. % hObject handle to DIP (see GCBO)
  98. % eventdata reserved - to be defined in a future version of MATLAB
  99. % handles structure with handles and user data (see GUIDATA)
  100. axes(handles.Img);
  101. I=getimage;
  102. [m,n,r]=size(I); %图像的像素为width*height
  103. %%%%%蓝色字体变黑
  104. myI=double(I);
  105. for i= 1:m
  106. for j= 1:n
  107. if((myI(i,j, 1)>= 15)&&(myI(i,j, 1)<= 130)&&((myI(i,j, 2)<= 165)&&(myI(i,j, 2)>= 90))&&((myI(i,j, 3)<= 220)&&(myI(i,j, 3)>= 135))) % 蓝色RGB的灰度范围
  108. I(i,j, 1)= 40; %红色分量
  109. I(i,j, 2)= 40; %绿色分量
  110. I(i,j, 3)= 40; %蓝色分量
  111. end
  112. end
  113. end
  114. %figure, imshow(I);title('变色后的图像');
  115. width=round( 0.9*n);height=round( 0.87*m);
  116. rx=round( 0.05*n);cy=round( 0.075*m);
  117. I=subim(I,height,width,rx,cy);
  118. %figure,imshow(I);
  119. if sum(size(I)> 0)== 3 %倘若是彩色图--2维*3,先转换成灰度图
  120. I=rgb2gray(I);
  121. end
  122. %figure,imhist(I);
  123. x= 3; %行数分为x部分
  124. y= 1; %列数分为y部分
  125. BW=erzhihua(I,x,y);
  126. [n m l]=size(BW); %图像的像素为m*n
  127. c = [ 0.65*m 0.65*m m m];
  128. r = [ 0 0.85*n 0.85*n 0];
  129. BW = roifill(BW,c,r);
  130. BW=imadjust(BW); %使用imadjust函数对图像进行增强对比度
  131. % Convert to BW
  132. threshold = graythresh(BW);
  133. BW =~im2bw(BW, 0.6*threshold);
  134. [image_h image_w]=size(BW);
  135. % Remove all object containing fewer than (imagen/80) pixels
  136. BW = bwareaopen(BW,floor(image_w/ 80));
  137. % 滤波
  138. %h=fspecial('average',1);
  139. %BW=im2bw(round(filter2(h,BW)));
  140. %imwrite(d,'4.均值滤波后.jpg');
  141. axes(handles.Binary);
  142. imshow(BW);
  143. %图像分割与识别按钮的回调函数
  144. % --- Executes on button press in OCR.
  145. function OCR_Callback(hObject, eventdata, handles)
  146. % hObject handle to OCR (see GCBO)
  147. % eventdata reserved - to be defined in a future version of MATLAB
  148. % handles structure with handles and user data (see GUIDATA)
  149. axes(handles.Binary);
  150. imagen = getimage;
  151. [image_h image_w]=size(imagen);
  152. %figure;imshow(imagen);title('INPUT IMAGE')
  153. % Convert to gray scale
  154. if size(imagen, 3)== 3 %RGB image
  155. imagen=rgb2gray(imagen);
  156. end
  157. %Storage matrix word from image
  158. word=[ ];
  159. re=imagen;
  160. %Opens text.txt as file for write
  161. fid = fopen( 'ID_card.txt', 'wt');
  162. % Load templates
  163. load templates
  164. global templates
  165. % Compute the number of letters in template file
  166. num_letras=size(templates, 2);
  167. figure;
  168. plot_flag= 1;
  169. while 1
  170. %Fcn 'lines' separate lines in text
  171. [fl re]=lines(re);
  172. imgn=fl;
  173. [line_h line_w]=size(fl); %记录下切割出来的一行字符的长宽
  174. %Uncomment line below to see lines one by one
  175. % imshow(fl);pause(1)
  176. %-----------------------------------------------------------------
  177. % Label and count connected components
  178. [L Ne] = bwlabel(imgn);
  179. n= 1; %记录循环次数
  180. while(n<=Ne)
  181. char_flag= 0; %为0时,是第一次判断这个连通域
  182. flag= 1; %初始化两个连通域属于同个字符
  183. while(flag== 1)
  184. if char_flag== 0
  185. [r,c] = find(L==n);
  186. Width0=max(r)-min(r); %连通域宽度
  187. Height0=max(c)-min(c); %连通域高度
  188. Radio0=Width0/Height0; %连通域宽高比
  189. Square0=Width0*Height0; %连通域面积
  190. maxr=max(r);
  191. maxc=max(c);
  192. minr=min(r);
  193. minc=min(c);
  194. end
  195. if n<Ne
  196. [r1,c1] = find(L==(n+ 1)); %寻找下一个连通域
  197. Width1=max(r)-min(r); %连通域宽度
  198. Height1=max(c)-min(c); %连通域高度
  199. Radio1=Width1/Height1; %连通域宽高比
  200. Square1=Width1*Height1; %连通域面积
  201. Uheight=max(maxc,max(c1))-min(minc,min(c1)); %合并后高度
  202. Uwidth=max(maxr,max(r1))-min(minr,min(r1)); %合并后宽度
  203. Uradio=Uwidth/Uheight; %合并后的宽高比
  204. Oheigth=Height0+Height1-Uheight; %重叠高度
  205. Owidth=Width0+Width1-Uwidth; %重叠宽度
  206. Osquare=Oheigth*Owidth; %重叠面积
  207. else
  208. flag= 0; %这是这一行最后一个连通域
  209. end
  210. ph= 5; %边界因子
  211. pw= 7;
  212. if(flag== 1)&&((Owidth>=-(image_w/pw)&&Owidth<= 0)||(Oheigth>=-(line_h* 0.3)&&Oheigth<= 0)) %两个连通域较近,但不重叠
  213. if((Uradio>= 0.8)&&(Uradio<= 1.2)) %认为两个连通域属于同一个字符
  214. elseif Uheight<line_h* 0.4; %连通域的合并之后高度过小的,认为是一个字符的一部分,很可能是边旁部首
  215. else flag= 0; %否则这两个连通域属于不同字符
  216. end
  217. elseif(flag== 1)&&(Owidth<-(image_w/pw)) %两个连通域里相距较远
  218. flag= 0; %两个连通域属于不同字符
  219. % elseif(flag==1)&&((Owidth>0)||(Oheigth>0))%两连通域重叠
  220. elseif(flag== 1)&&((Owidth> 0)) %两连通域重叠
  221. if(((Uradio>= 0.78)&&(Uradio<= 1.3))) %认为两个连通域属于同一个字符
  222. elseif(Osquare>= 0.4*min(Square0,Square1)&&(Uwidth<image_w/ 45))
  223. else
  224. flag= 0; %两个连通域属于不同字符
  225. end
  226. else flag= 0; %两个连通域属于不同字符
  227. end
  228. if flag== 1 %经过上面判断,两个连通域属于同一个字符,进行连通域合并
  229. Width0=Uwidth; %连通域宽度
  230. Height0=Uheight; %连通域高度
  231. Radio0=Width0/Height0; %连通域宽高比
  232. Square0=Width0*Height0; %连通域面积
  233. maxr=max(maxr,max(r1));
  234. maxc=max(maxc,max(c1));
  235. minr=min(minr,min(r1));
  236. minc=min(minc,min(c1));
  237. n=n+ 1; %指向下一个连通域
  238. char_flag= 1;
  239. end
  240. end %while(flag==1)的end
  241. % Extract letter
  242. n1=imgn(minr:maxr,minc:maxc);
  243. % Resize letter (same size of template)
  244. img_r=imresize(n1,[ 36 23]);
  245. subplot( 10, 10,plot_flag),imshow(img_r);title(plot_flag);
  246. plot_flag=plot_flag+ 1;
  247. %Uncomment line below to see letters one by one
  248. % imshow(img_r);title(n);pause(0.5)
  249. %-------------------------------------------------------------------
  250. % Call fcn to convert image to text
  251. letter=read_letter(img_r,num_letras);
  252. % Letter concatenation
  253. word=[word letter];
  254. n=n+ 1;
  255. end % while(n<=Ne)的end
  256. %fprintf(fid,'%s\n',lower(word));%Write 'word' in text file (lower)
  257. fprintf(fid, '%s\n',word); %Write 'word' in text file (upper)
  258. % Clear 'word' variable
  259. word=[ ];
  260. %*When the sentences finish, breaks the loop
  261. if isempty(re) %See variable 're' in Fcn 'lines'
  262. break
  263. end
  264. end
  265. fclose(fid);
  266. %Open 'ID_card.txt' file
  267. winopen( 'ID_card.txt')
  268. % --- Executes on button press in Exit.
  269. function Exit_Callback(hObject, eventdata, handles)
  270. % hObject handle to Exit (see GCBO)
  271. % eventdata reserved - to be defined in a future version of MATLAB
  272. % handles structure with handles and user data (see GUIDATA)
  273. clc;
  274. close all;
  275. close(gcf);

完整代码添加QQ1575304183


转载:https://blog.csdn.net/weixin_50197058/article/details/116505906
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场