EDA365电子论坛网

标题: 做一个基于MATLAB的车牌识别系统,出现索引超出矩阵维度的错误?请大神指导 [打印本页]

作者: qpggup    时间: 2020-7-16 11:14
标题: 做一个基于MATLAB的车牌识别系统,出现索引超出矩阵维度的错误?请大神指导
本帖最后由 qpggup 于 2020-7-16 13:23 编辑
+ K, C+ h6 p: Z! w7 t. A( [& ]
3 x$ |0 I6 ~0 {5 f; K做一个基于MATLAB的车牌识别系统,出现索引超出矩阵维度的错误。主函数和自定义函数都写出来了(书上找的),希望大神帮助
7 ~- A: r( T# H# a7 G3 L[fn,pn,fi]=uigetfile('AD.jpg','选择图片');) E3 c3 X" X9 a8 O
I=imread([pn fn]);8 ]6 K7 n* k5 U9 q; t/ b
figure,0 Z% x5 T+ b: }) w
imshow(I);) `6 p+ {) Q, \5 ]! Z# i9 s
title('原始图像');, e, c5 i( c. Q5 `# y& C7 z) ~9 U  z% e
Im1=rgb2gray(I);7 V6 L6 ~- n+ d: o& G: v  y" y0 y
figure(2),+ A& q& H2 G  A+ S/ i( S
subplot(1,2,1),
9 K1 k, {! ]) _1 Qimshow(Im1);3 N3 H4 W, O/ L7 R: W; K( {
title('灰度图');
3 I, S( `! y& X8 _6 |# p$ \figure(2),
, Z- G" U1 \4 \6 Ssubplot(1,2,2),
+ `7 P1 `( z: K9 v. t$ Dimhist(Im1);
1 ?2 R! m9 G& m# t" btitle('灰度图的直方图');
8 O  M+ c8 ~4 ]4 I- q1 b: c& KTiao=imadjust(Im1,[0.19,0.78],[0,1]);
( @9 K- }6 c* h1 N# R! Vfigure(3),
8 T7 y2 ?6 b4 F2 ]% y- a# ysubplot(1,2,1),
2 N$ v4 F  P8 W( Q  c+ }imshow(Tiao);
5 M* D0 ^0 ?1 [' etitle('增强灰度图');
5 t# t4 ^& M) y! f1 ?figure(3),* x' ~6 _( `+ _
subplot(1,2,2),
! L: \! {9 X; `& j! A& Eimhist(Tiao);
1 }% N& C6 m8 b. I% J& mtitle('增强灰度图的直方图');/ Q5 y  x. s6 W9 B+ [" A
Im2=edge(Im1,'sobel',0.15,'both');
# ~, ]' p! p4 Bfigure(4),0 i" ?: @! A7 j
imshow(Im2);
  H* F: N- ]- V6 @title('sobel算子实现边缘检测')/ s" o8 E2 v6 M5 W# z# Q! H/ @
se=[1;1;1;];
! Z4 d) m7 G9 ]. Y9 ~9 |Im3=imerode(Im2,se);
, f! Z% r* }* }5 V1 W! dfigure(5),9 w+ Q, _6 Y2 }; S
imshow(Im3);/ h; \5 D$ r  A: X: R
title('腐蚀效果图');
+ z# Y; x$ ~6 Hse=strel('rectangle',[25,25]);8 d# I2 {/ x6 [5 V& _. ?
Im4=imclose(Im3,se);% t& @( B3 N" _( Z& x9 g
figure(6),
0 s* e: Z  S* f) [6 ximshow(Im4);
( ^1 N9 a& O6 V0 x( ]2 }1 h  J$ xtitle('平滑图像的轮廓');
. R4 {1 T* ]* G7 g! tIm5=bwareaopen(Im4,2000);% ?$ K# ?( l" g& C4 K  u- k9 a; W
figure(7),4 V. \1 W1 L7 T/ Y" z! N8 p4 x
imshow(Im5);
3 Z- X, L% e( }6 [title('移除小对象');% A7 h$ i9 w6 z6 k
[y,x,z]=size(Im5);9 w! [1 v4 s4 ~+ i: [0 w# q
Im6=double(Im5);
) ]# h$ e' d+ c2 z4 ?* zBlue_y=zeros(y,1);6 F: h$ U' }4 t9 u) E3 x' h. y
for i=1:y
; r: Y  }2 ]% D5 c% D: p4 |    for j=1:x
: S4 w; p. N3 y% y: N        if(Im6(i,j,1)==1)' A' e$ W( n* v1 @
            Blue_y(i,1)=Blue_y(i,1)+1;
2 o' G3 B% K  a, H/ l, k8 u        end
! E4 W6 ]/ j" ?2 _2 J6 `' |. R    end
" N- g0 M) a( e. F# l1 z  {end
' d, Y+ ]) P3 a9 A* S- F[temp MaxY]=max(Blue_y);, W) b# Z3 s6 |9 v
PY1=MaxY;) W: ?9 I9 e. I% p& Z- L0 b: N" U
while((Blue_y( PY1,1)>=5)&&( PY1>1))9 c2 S5 E9 e  Z, S
    PY1=PY1-1;; _( \$ d$ @: L2 c5 J
end$ \, T/ A0 v, U# @! @' G$ r$ c
PY2=MaxY;' Z. w1 b& l. P1 \- j
while((Blue_y( PY2,1)>=5)&&( PY2<y))" V* I% f1 Q, D( G$ |9 `+ m. c
    PY2=PY2+1;1 Z! ^6 m; M6 B- k
end& d. ~* O% ~5 L" H8 W
IY=I( PY1: PY2,:,: );
: g; `- H8 X7 n! M" `Blue_x=zeros(1,x);
4 y- N: _# y  R  V* zfor j=1:x
' ?% m/ H/ w5 F3 k! D4 ?& P$ s    for i=PY1: PY2
; V6 z' }6 p& |. s& t! X        if(Im6(i,j,1)==1)* n/ X' y" g/ |1 F9 n  O
            Blue_x(1,j)=Blue_x(1,j)+1;
6 f& _  Y+ u! t1 k* I5 F. t! n- ~5 \        end4 V! G6 ]# h2 I" q$ G
    end5 X% i0 {; y& J' e! ]( R
end" N, r: j3 D8 g2 Q% n
PX1=1;
' M' u) F( ~6 |$ A, T/ E, Dwhile((Blue_x(1,PX1)<3)&&( PX1<x))
: r% r3 Y# t8 T( F6 {    PX1=PX1+1;
6 O5 L9 U) L' o# ~end
( b+ H1 F& l1 U. mPX2=x;
% `' k/ Z5 e  w0 V9 M+ _( v- pwhile((Blue_x(1,PX2)<3)&&( PX2> PX1))
' X2 X+ z0 K' N* P    PX2=PX2-1;1 p% F6 F- C8 t/ ^7 ^2 J( [
end
! X5 d3 N4 |( hPX1=PX1-1;
. r& A% N. V) Y5 i5 k5 [8 r% L$ KPX2=PX2+1;
1 L2 q! V3 w$ w: r+ x7 Mdw=I(PY1: PY2-6,PX1 : PX2,: );
7 R2 u8 B4 @) ]figure(8),
7 v2 ^4 j& K0 y4 |subplot(1,2,1),
3 b+ F# D1 A. I  n* k2 W' Limshow(IY),
$ A4 V- @( r1 S# q# g. Otitle('垂直方向合理区域');# A% X& I0 n! H) y2 ^2 i
figure(8),- {1 R7 d4 r9 E- c
subplot(1,2,2),
; r' D, g1 h% T7 j9 Qimshow(dw),* [* e, Q8 r3 p3 ~  C
title('定位剪切后的彩色车牌图像')
: R2 W, @3 `8 N# B+ b( himwrite(dw,'dw.jpg');  s! C6 G4 {0 V8 X7 ~
a=imread('dw.jpg');
5 s% M; m5 C4 [b=rgb2gray(a);
" L0 m3 c9 V  k3 h: c6 Simwrite(b,'车牌灰度图像.jpg');
/ w  S2 [4 K% J1 k1 O* q5 Xfigure(9);
6 y7 g7 F# i! Rsubplot(3,2,1),
0 C# L  D- u; U; C$ i7 j. Mimshow(b),, j6 Z6 S3 r; s0 _- d) u
title('1.车牌灰度图像')* t) o7 b; U& R" V
g_max=double(max(max(b)));
* w' j3 k5 o; K5 ^g_min=double(min(min(b)));
' w& c  v3 Y5 cT=round(g_max-(g_max-g_min)/3);
0 S! @- H# y& K  r0 c[m,n]=size(b);6 `% v8 R/ u5 i
d=(double(b)>=T);
- a% I* Z" |: L2 @imwrite(d,'车牌二值图像.jpg');
) o" k3 k4 V; Tfigure(9)
/ I: b0 [, Q2 |subplot(3,2,2),
+ g7 H7 V% b: Y* k: C/ t& simshow(d),! g5 w. T7 V" s$ F5 W! l7 w8 F! ]2 n* y7 Q
title('2.车牌二值图像')9 Y2 t. ~3 S2 n
figure(9)
# l; N& a7 [* _6 E- O$ w# xsubplot(3,2,3),
! Y: _2 C8 K  a3 R( l/ Q6 F5 Bimshow(d),5 x" u: r. U' n$ M/ n4 l) J
title('3.均值滤波前')6 N; R  I, D6 d" P. q4 x
h=fspecial('average',3);" `0 L) v3 O/ M( \
d=im2bw(round(filter2(h,d)));& u; L4 O* z3 R/ L
imwrite(d,'均值滤波后.jpg');
; b( N; s/ ]' ~- v5 \% Cfigure(9)
( p& P. m/ B, `. ~# G, f2 K/ msubplot(3,2,4),) k0 [' f6 \" K# y7 v
imshow(d),
) W2 L) }0 N5 B, D5 ititle('4.均值滤波后')
* E' z) Y- b, H& g8 B! F7 V% ise=eye(2);# m8 s  b6 b" q6 y+ _9 L+ v9 `
[m,n]=size(d);
3 w8 B) Q8 u+ w( j- z( `  ?( Cif bwarea(d)/m/n>=0.365
6 }* g! n- f6 f# N    d=imerode(d,se);- G' [- Q9 o! {/ F
elseif bwarea(d)/m/n<=0.235
$ k* S: E. v) R7 w    d=imdilate(d,se);' f0 u" s& D  |% b  r
end
+ F7 ~; z- S7 @imwrite(d,'膨胀或腐蚀后处理后.jpg');0 q5 s/ u! ^" h4 z5 [+ F; J' X
figure(9)
7 P* s/ P9 |0 ^% v) E; `subplot(3,2,5),
. i8 O' e5 ?! b& ?( S; uimshow(d),4 q: Z8 x! U) A
title('5.膨胀或腐蚀后处理后'); y6 l5 t4 g. u' [
d=QieGe(d);" n) p8 D9 k; i. i0 z
[m,n]=size(d);  Z- Z8 O7 `% R% p& I: }
k1=1;k2=1;s=sum(d);j=1;, N$ g+ R+ t3 a, W/ }
while j~=n/ q4 @8 F9 V/ c: |1 f6 o
   while s(j)==0;* d8 y/ T" u# W5 _2 N) T
        j=j+1;
. X. |( A8 a- M5 y& o    end* a3 E- p2 x% q# a
        k1=j;
, t) f+ @: A3 ^' N8 h% d  n        while s(j)~=0&&j<=n-1# z& F* j3 k2 h* _7 B0 X
            j=j+1;, I/ @9 s# E: `4 \; ]% T* N
        end
$ x! _3 E& ~% _        k2=j-1;9 A1 N1 \8 y; j! E0 H2 [% K1 I& M
        if k2-k1>=round(n/6.5): d  P# E9 X  O% J5 o* S& |
            [val,num]=min(sum(d( :,[k1+5:k2-5])));: ], d/ @! y8 ?" q& T: T
            d( :,k1+num+5)=0;
) T7 I& J; y$ I! x        end
8 O2 E9 h% {0 h$ k" I: @0 ^end/ H# ^! ~: t: ~& v& s" E* a
d=QieGe(d);
4 M  k. V$ D3 ^# My1=10;y2=0.25;flag=0;word1=[];/ \4 {- b; T+ g+ h3 w! ?7 l3 D
while flag==0
, \, Q) L5 W- e4 W    [m,n]=size(d);( E8 B0 I) f8 `* j* h) x. g
    left=1;wide=0;
; `6 V6 c# r3 ?$ J    while sum(d( :,wide+1))~=07 a6 P: V4 S6 I: Y/ M! x
        wide=wide+1;
" y+ B- w) P. H! c$ F    end7 V8 i" Z  ^! Q1 Z) c/ ^  }
    if wide<y1;
' y  A2 i; v; c0 a- z* C        d( :,[1:wide])=0;
! P8 y6 a$ I5 Y; G- p        d=QieGe(d);, Y/ i) ~& e7 Z5 C, c8 ^
    else
0 o# o6 o, c7 S' J2 G$ Y3 v        temp=QieGe(imcrop(d,[1 1 wide m]));# I- q$ o4 l9 G5 U7 Q( ]  U
        [m,n]=size(temp);
# M7 l" i( D- B$ c4 a        all=sum(sum(temp));
- T" W  m3 z0 g6 Y7 Z: v        two_thirds=sum(sum(temp(round(m/3):2*round(m/3),: )));
8 v7 L5 T6 D& ?; J" U, p        if two_thirds/all>2. d0 N* ^0 @* D
            flag=1;word1=temp;  c- j, G) P0 |  A  t1 _
        end% C3 f5 l' h+ h8 w! V
        d( :,1:wide)=0;d=QieGe(d);( @0 f# Z9 z/ S' |$ L
    end& P. R. J0 M% t, E
end
  v# ?" F! X9 M7 Q[word2,d]=FenGe(d);# @; `( u- Y) Y& F! ]5 U
[word3,d]=FenGe(d);  Z/ C+ @1 K& q; x
[word4,d]=FenGe(d);- Q- c! y* X$ w. Y
[word5,d]=FenGe(d);- o/ t4 o) ^$ m& L( R
[word6,d]=FenGe(d);) F/ T; {/ d" N5 _
[word7,d]=FenGe(d);4 e, _1 H  A, `7 `
word1=imresize(word1,[40 20]);
# g. {6 A9 h& q4 |  ~7 G+ x" f& \6 fword2=imresize(word2,[40 20]);
. ^! [; x" K0 e7 h% A  oword3=imresize(word3,[40 20]);9 t  U4 S  v* j& x9 ?2 \( ~3 f. f
word4=imresize(word4,[40 20]);
2 T1 B" Y/ d( w0 hword5=imresize(word5,[40 20]);
5 O! e! J  I- K/ y* Aword6=imresize(word6,[40 20]);
6 r* K; m% ^* a6 Wword7=imresize(word7,[40 20]);4 [! W$ y1 z7 m
figure
  r4 S! o7 b( e, r( g3 p$ U2 B+ C2 Lsubplot(2,7,1),
: f6 ~7 R0 F& E/ ]/ Cimshow(word1),/ q; g; I+ \+ h, e$ o
title('1');) `+ T: p. c' \- Y' t$ V1 g
subplot(2,7,2),5 T5 v3 X, M$ f9 z; f; _5 }
imshow(word2),& d# H1 |) l8 {+ f; [' ]# U) r
title('2');5 [4 T9 |$ `+ p
subplot(2,7,3),9 Z2 w' M" V" S( f( I& e
imshow(word3),1 U) R5 A+ z) @+ |, v
title('3');3 _1 ^, J' O1 w
subplot(2,7,4),  x2 J; B5 k1 M) ^1 J- f
imshow(word4),
5 C: E! E1 \5 i& J6 @! `: w3 z* Btitle('4');* N, c" n5 x4 H
subplot(2,7,5),+ B6 t- Q  |( A) m
imshow(word5),2 R* b  T$ Y, ], Y
title('5');
0 E/ v8 J2 r: O7 d  }* Bsubplot(2,7,6),) l, j' @6 z1 N* v2 w# W0 J
imshow(word6),5 U9 y8 i0 Q4 U
title('6');
' M( ^; q, ]7 C" |* {6 h& Q; [subplot(2,7,7),
* p( t! i& \  l4 m6 D2 }; Oimshow(word7),6 l7 x  e- S: a& G4 [5 S( Y8 e" l
title('7');
0 ?' f' t4 |6 ?/ |6 M4 Z2 `+ pimwrite(word,'1.jpg');
$ n- ~! ?0 ]$ K. z# C# {6 Nimwrite(word,'2.jpg');
3 G# h; L0 l9 I$ e- ?imwrite(word,'3.jpg');3 e6 n+ }+ F: `) `* w
imwrite(word,'4.jpg');
( Q6 n, S& p% K  s  g1 nimwrite(word,'5.jpg');
" W0 s) d, ]5 Q& ~imwrite(word,'6.jpg');3 t% r+ \( z& P- Q& c# z
imwrite(word,'7.jpg');
% X# m, h5 f- p6 v5 ?% X' i" e1 `, C: X. a* W. H. H# u7 h
liccode=char('0':'9''A':'Z''京津冀晋蒙辽吉黑沪苏浙皖闽赣鲁豫鄂湘粤桂琼渝川贵云藏陕甘青宁新');
5 M# C; s; Z0 E$ h2 C/ Bl=1;
0 H9 D/ {6 L# h2 ~& Kfor I=1:7) o# j' ^2 x& l+ E+ _0 f3 F
    ii=int2str(I);7 K1 W# d* U# H
    t=imread([ii,'.jpg']);7 h2 m$ K' Y% N* i9 d' Z3 P
    SegBw2=imresize(t,[40,20],'nearest');4 t, {. O! o" h. z3 B% n: }& H* P
    if 1==1
8 ^7 V; |2 I% f$ x$ V" x1 P        kmin=37;
. U1 Y( K/ o- n" I; ]        kmax=40;' r. ^3 A; ^' n- w  v
    elseif 1>=2&&1<=39 E, q7 c6 l3 B9 r: |1 n0 N& e- \8 X
        kmin=11;/ f# e7 N) t- F4 ~0 s$ Y
        kmax=36;
+ z, i0 s4 W9 [/ ?    elseif 1>=4&1<=7
( ]1 G. E0 @8 M( {9 i$ q( l+ R        kmin=1;# y6 \: s6 `- y$ u- k+ b
        kmax=10;: s% X2 P, e  ~+ ?$ [$ i; N
    end
& R5 r- b0 g, |  `! A! J% U+ |    for k2=kmin:kmax
  n" x* w6 V' L) g9 |1 q4 e4 Y5 x        fname=stract('字符模板|',liccode(k2),'.jpg');3 Z2 N, J7 @/ }  W$ J3 q# j+ Q
        SamBw2=imread(fname);
7 R1 b$ x+ Q/ e7 T+ t9 I4 R- \        Dm=0;6 Q- [9 O5 N: z; Z5 c) X" d7 g! D
        for k1=1:40
& B) s% J* W5 n* n& N0 y5 G            for11=1:20;; z6 f! u$ p4 ~4 G( ^
            if SegBw2(k1,11)==SamBw2(k1,11)
% K" m' E' J9 ]. D( ~                Dm=Dm+1;
" c3 ~  I- B+ q, P) W( R) O3 ^            end, e' J( ]6 e5 v- U
        end* k4 l' p* X9 q5 M7 H4 V! r
    end
5 _* q# D# A; Q& G0 G  S    Error(k2)=Dm;1 E  q* W; F+ \* v
end
4 \% L, }1 Z$ P" U0 G: {1 mError1=Error(kmin:kmax);
  ~1 Q  ]1 ~& g( O1 D% Y# gMinError=max(Error1);
& U- e. G( m( }* B, d+ e- lfindc=find(Error1==MinError);4 U! f. t# U5 d. W4 |1 J& n" C
Resault(1*2-1)=liccode(find(1)+kmin-1);
$ Z" C: z- L6 q' QResault(1*2)='';
/ [% X1 ^' k2 b% W; B! I. |1 W# cl=1+1;
  @5 Q2 A: X8 M( @) K  Mt=toc;
1 g3 W; t8 n$ ?1 Y) C3 r( w  \Resault;
5 k  `9 r$ e2 ~# |2 B$ zmesgbox(Resault,'识别结果')  p+ K) d) i+ A8 s+ r) j+ W7 G1 @
fid=fopen('Data.xls','a+');
* p! W$ Y! W6 C% n, y$ R% r6 Nfprintf(fid,'%s|r|n',Resault,datestr(now));
# u$ ^* Z1 w" k9 U: `fclose(fid);
: a" @" g1 _# g索引超出矩阵维度。
9 v2 F3 o/ Y6 [# V; R7 ^6 O4 r
出错 main (line 152)  v) f/ @7 _$ P
    while sum(d( :,wide+1))~=0, a. E% }0 N2 g% s/ m

作者: SsaaM7    时间: 2020-7-16 13:23
帮你顶一下
作者: haidaowang    时间: 2020-7-16 16:28
根据错误提示:0 [4 L4 r! F( p' _6 O
出错 main (line 152)+ [& m; u' D; K- U
    while sum(d(:,wide+1))~=0
, V  K% v2 v% u. \0 q% }% @
/ x; w' T& s  O; D* s$ ]
, Z! K2 [3 x! Y. M2 X& X猜测原因是wide+1在某次循环中大于矩阵d的列数;' d( N) n' h) b9 t! t
建议在这一行设置断点,然后步进程序,查看wide变量的变化规律以找到超出矩阵维度的原因。
作者: qpggup    时间: 2020-7-16 16:45
haidaowang 发表于 2020-7-16 16:28" P% j# H# ?& ?/ ^
根据错误提示:! i$ s  a) D/ S& V6 Q
出错 main (line 152)6 e' m, m7 f: [" r1 a8 m# M3 N: i
    while sum(d(:,wide+1))~=0

# m1 z+ M1 t0 ]6 U$ s6 _/ s9 E谢谢啦,我检查一下
0 b% c. E% G3 Y/ O' a$ P




欢迎光临 EDA365电子论坛网 (https://bbs.eda365.com/) Powered by Discuz! X3.2