【条形码识别】条形码中数字的识别的matlab仿真
仿真结果为:
第一个图:原始的图片
第二个图:灰度图
第三个图:加入噪声的图片
第四个图:滤波以后的图片
最后识别出来的条形码为:
部分代码:
clc;
clear;
close all;
warning off;
%% 读取图片并模拟实际情况,添加噪声并滤波
%% 读取图片并模拟实际情况,添加噪声并滤波
%% 读取图片并模拟实际情况,添加噪声并滤波
%噪声的大小
noise_level = 0.02;
%滤波参数
filter_area = 3;
figure;
%读取图片
bar_image = imread('images\2.bmp'); %读输入条形码图片
subplot(221);imshow(bar_image);title('原始图像');
%图片转换为灰度图
if isrgb(bar_image) == 1
bar_image = rgb2gray(bar_image);
else
bar_image = bar_image;
end
subplot(222);imshow(bar_image);title('原始图像的灰度图');
%添加噪声
bar_image_noise = imnoise(bar_image,'salt & pepper',noise_level);
subplot(223);imshow(bar_image_noise);title('加入噪声后的图像');
%进行中值滤波
bar_image_filter= medfilt2(bar_image_noise,[filter_area filter_area]);
subplot(224);imshow(bar_image_filter);title('滤波之后的图像');
%% 参数初始化
%% 参数初始化
%% 参数初始化
%二值化参数
level = 0.8;
%左边和右边数据编码
codes = [3211,2221,2122,1411,1132,1231,1114,1312,1213,3112;
1123,1222,2212,1141,2311,1321,4111,2131,3121,2113];
%第一位数据编码
first_codes = [31,20,18,17,12,6,3,10,9,5];
%求灰度图的大小
[height,width] = size(bar_image_filter);
%二值化参数
bar_image_filter_10 = im2bw(bar_image_filter,level);
%% 条码检测
%% 条码检测
%% 条码检测
%检测59根条形码
l = 0;
for i=1:height
k = 1;
l = l+1;
for j=1:width-1
%比较同一行相邻两点的颜色是否一致
if bar_image_filter_10(i,j)>bar_image_filter_10(i,j+1) | bar_image_filter_10(i,j)< bar_image_filter_10(i,j+1)
Y_position(l,k) = j; %记录坐标
k = k+1;
end
if k>61
l = l-1;
break
end
end
if k<61
l = l-1;
end
end
[height,width] = size(Y_position);
if height<=1
disp('无效的条形码');
else
%条形码的宽度
bar_width = func_Tiaox_width(Y_position,height,width);
%条形码的宽度
[bar_sum2,Left_bar_number,Right_bar_number]=func_eachwidth(bar_width,height);
bar_number = '';
bar_fist_number = 0;
first = 2;
%左边编码查出条形码编码
[bar_fist_number,bar_number] = func_leftnumber(bar_sum2,codes,bar_fist_number,Left_bar_number,bar_number,first);
%右边编码查出条形码编码
bar_number = func_rightnumber(codes,bar_number,Right_bar_number);
%从第一位数据编码表中查出第一位数字
bar_number = func_findnumber(bar_fist_number,first_codes,bar_number);
%校验码
check_code = func_codecheck(bar_number);
if check_code==str2num(bar_number(13))
disp('条形码为:');
for i=1:length(bar_number)
final{i} = bar_number(i);
end
end
final
end
A09-14