1、matlab一维条形码码字识别程序close allI = imread(E:);J= rgb2gray(I);figure(1)imshow(J);title(灰度化图像 );e1,e2=size(J);Im=imcrop(J,e2/2-200,e1/2-200,400,400);figure(2)subplot(1,2,1),imshow(Im)title(中心区域 );subplot(1,2,2),imhist(Im)title(中心区域直方图);xa,ya=size(Im); b=double(Im); zd=double(max(Im) ; zx=double(min(Im) ; T
2、=double(zd+zx)/2; count=double(0); while 1 count=count+1; S0=; n0=; S1=; n1=; for i=1:xafor j=1:yaif double(Im(i,j)=TS1=S1+double(Im(i,j); n1=n1+1; else S0=S0+double(Im(i,j); n0=n0+1; end end end T0=S0/n0; T1=S1/n1; if abs(T-(T0+T1)/2) break;elseT=(T0+T1)/2; end end countTK=find(J=T);J(K)=255;figure
3、(3)imshow(J)title( 图像二值化 );B=medfilt2(J,5,1);figure(4)imshow(B)title(中值滤波后图像);y0,x0=size(B);BW = edge(B,log); figure(5);imshow(BW);title(边缘检测图像)%function code = barcode(pic) %条形码识别check_left = 13,25,19,61,35,49,47,59,55,11;. %左边数据编码,奇39,51,27,33,29,57, 5,17, 9,23; %左边数据编码,偶check_right = 114,102,108,
4、66,92,78,80,68,72,116; %右边数据编码first_num = 31,20,18,17,12,6,3,10,9,5;%第一位数据编码bar = imread(E:);%读输入条形码图片bar_Gray = rgb2gray(bar);%将RGB图片转换灰度图a_hist x = imhist(bar_Gray);hist_max = ;if a_hist(1)a_hist(2)hist_max = hist_max 1;endx = max(x);for i=2:xif a_hist(i)a_hist(i-1) & a_hist(i)a_hist(i+1)hist_max
5、= hist_max i;endendif a_hist(x)a_hist(x+1)hist_max = hist_max x+1;endm,n = size(hist_max);k = 0;max_1 = 0;max_2 = 0;for i=1:nif ka_hist(hist_max(i)k = a_hist(hist_max(i);max_1 = hist_max(i);end endtemp = a_hist(max_1);a_hist(max_1) = 0;k = 0;for i=1:nif kmax_2k = max_1;max_1 = max_2;max_2 = k;endT =
6、 max_1;k = a_hist(max_1);for i=max_1:max_2if ka_hist(i)k = a_hist(i);T = i;endendm,n = size(bar_Gray); %求灰度图的大小for i=1:m %对图像进行二值化处理for j=1:nif bar_Gray(i,j)T%选择适当的阈值进行二值化处理bar_10(i,j) = 1;elsebar_10(i,j) = 0;endendend%imshow(bar_10);l = 0;%检测59根条形码for i=1:mk = 1;l = l+1;for j=1:n-1if bar_10(i,j)=ba
7、r_10(i,j+1)%比较同一行相邻两点的颜色是否一致%bar_x(l,k) = i;bar_y(l,k) = j; %记录转折点的纵坐标k = k+1;%准备记录下一个数据点endif k61 %点数大于60,该行应该删掉l = l-1;breakendendif k61 %点数小于60,该行应该删掉l = l-1;endendm,n = size(bar_y);if m=1 %查看条形码是否有效code = 0;fprintf(1,GameOver!n);returnendfor i=1:m%计算每根条形码的宽度for j=1:n-1bar_num(i,j) = bar_y(i,j+1) - bar_y(i,j);if bar_num(i,j)0check_code = 10 - check_code;endif check_code=str2double(num_bar(13) %判断校验码是否正确code = num_bar;elsefprintf(1,Please Turn It Around!n);returnend
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1