else
% 提取字符区域
temp = Segmation(imcrop(bw, [1 1 wide m])); [m, n] = size(temp);
tall = sum(temp(:)); % 该字符所有像素之和 % 该字符图像2/3图像区域像素和
two_thirds = sum(sum( temp(round(m/3):2*round(m/3), :) )); rate = two_thirds/tall; % 中间区域像素/全局区域像素 if rate > rateTol flag = 1;
word1 = temp; %提取WORD 1 end
bw(:, 1:wide) = 0; % 抹去已处理的区域 bw = Segmation(bw); end end
% 分割出第二个字符
[word2, bw] = Word_Segmation(bw); % 分割出第三个字符
[word3, bw] = Word_Segmation(bw); % 分割出第四个字符
[word4, bw] = Word_Segmation(bw); % 分割出第五个字符
[word5, bw] = Word_Segmation(bw); % 分割出第六个字符
[word6, bw] = Word_Segmation(bw); % 分割出第七个字符
[word7, bw] = Word_Segmation(bw);
wid = [size(word1, 2) size(word2, 2) size(word3, 2) ...
size(word4, 2) size(word5, 2) size(word6, 2) size(word7, 2)]; [maxwid, indmax] = max(wid); maxwid = maxwid + 10; wordi = word1;
wordi = [zeros(size(wordi, 1), round((maxwid-size(word1, 2))/2)) wordi zeros(size(wordi, 1), round((maxwid-size(word1, 2))/2))]; word1 = wordi;
wordi = word2;
wordi = [zeros(size(wordi, 1), round((maxwid-size(word2, 2))/2)) wordi zeros(size(wordi, 1), round((maxwid-size(word2, 2))/2))]; word2 = wordi;
wordi = word3;
wordi = [zeros(size(wordi, 1), round((maxwid-size(word3, 2))/2)) wordi zeros(size(wordi, 1), round((maxwid-size(word3, 2))/2))]; word3 = wordi;
wordi = word4;
wordi = [zeros(size(wordi, 1), round((maxwid-size(word4, 2))/2)) wordi zeros(size(wordi, 1), round((maxwid-size(word4, 2))/2))]; word4 = wordi;
wordi = word5;
wordi = [zeros(size(wordi, 1), round((maxwid-size(word5, 2))/2)) wordi zeros(size(wordi, 1), round((maxwid-size(word5, 2))/2))]; word5 = wordi;
wordi = word6;
wordi = [zeros(size(wordi, 1), round((maxwid-size(word6, 2))/2)) wordi zeros(size(wordi, 1), round((maxwid-size(word6, 2))/2))]; word6 = wordi;
wordi = word7;
wordi = [zeros(size(wordi, 1), round((maxwid-size(word7, 2))/2)) wordi zeros(size(wordi, 1), round((maxwid-size(word7, 2))/2))]; word7 = wordi; % figure(1);
% subplot(2, 7, 1); imshow(word1); title('字符1'); % subplot(2, 7, 2); imshow(word2); title('字符2'); % subplot(2, 7, 3); imshow(word3); title('字符3'); % subplot(2, 7, 4); imshow(word4); title('字符4'); % subplot(2, 7, 5); imshow(word5); title('字符5'); % subplot(2, 7, 6); imshow(word6); title('字符6'); % subplot(2, 7, 7); imshow(word7); title('字符7'); % 切割出的字符归一化大小为 40*20,此处演示 word11 = imresize(word1, [40 20]); word21 = imresize(word2, [40 20]); word31 = imresize(word3, [40 20]); word41 = imresize(word4, [40 20]); word51 = imresize(word5, [40 20]); word61 = imresize(word6, [40 20]); word71 = imresize(word7, [40 20]);
% subplot(2, 7, 8); imshow(word11); title('字符1'); % subplot(2, 7, 9); imshow(word21); title('字符2'); % subplot(2, 7, 10); imshow(word31); title('字符3'); % subplot(2, 7, 11); imshow(word41); title('字符4');
% subplot(2, 7, 12); imshow(word51); title('字符5'); % subplot(2, 7, 13); imshow(word61); title('字符6'); % subplot(2, 7, 14); imshow(word71); title('字符7'); % 赋值操作
words.word1 = word11; words.word2 = word21; words.word3 = word31; words.word4 = word41; words.word5 = word51; words.word6 = word61; words.word7 = word71;
if flag_display figure;
subplot(2, 7, 1); imshow(word1); title('字符1', 'FontWeight', 'Bold'); subplot(2, 7, 2); imshow(word2); title('字符2', 'FontWeight', 'Bold'); subplot(2, 7, 3); imshow(word3); title('字符3', 'FontWeight', 'Bold'); subplot(2, 7, 4); imshow(word4); title('字符4', 'FontWeight', 'Bold'); subplot(2, 7, 5); imshow(word5); title('字符5', 'FontWeight', 'Bold'); subplot(2, 7, 6); imshow(word6); title('字符6', 'FontWeight', 'Bold'); subplot(2, 7, 7); imshow(word7); title('字符7', 'FontWeight', 'Bold');
subplot(2, 7, 8); imshow(word11); title('字符1', 'FontWeight', 'Bold'); subplot(2, 7, 9); imshow(word21); title('字符2', 'FontWeight', 'Bold'); subplot(2, 7, 10); imshow(word31); title('字符3', 'FontWeight', 'Bold'); subplot(2, 7, 11); imshow(word41); title('字符4', 'FontWeight', 'Bold'); subplot(2, 7, 12); imshow(word51); title('字符5', 'FontWeight', 'Bold'); subplot(2, 7, 13); imshow(word61); title('字符6', 'FontWeight', 'Bold'); subplot(2, 7, 14); imshow(word71); title('字符7', 'FontWeight', 'Bold'); end
function mask = Mask_Process(Img, id) % 图像蒙版处理函数 % 输入参数:
% Img——图像矩阵 % id——图像序号 % 输出参数:
% mask——模板图像
% 如果已经存在模板图像则不再取模板 filename = sprintf('mask\\\\mask%d.jpg', id); if exist(filename, 'file')
mask = imread(filename);
if ~isa(mask, 'logical')
mask = im2bw(mask); end
sz = size(Img);
if ~isequal(sz(1:2), size(mask))
mask = imresize(mask, sz(1:2)); mask = logical(mask); end return; end I = Img; Ptn = []; figure;
subplot(1, 3, 1); imshow(I);
title('取点_{左键取点,右键退出}', 'Color', 'r', ... 'FontWeight', 'Bold'); hold on;
set(gcf, 'units', 'normalized', 'position', [0 0 1 1]); [x, y, button] = ginput(1); % 点击
while button == 1
plot(x, y, 'r+', 'LineWidth', 2); % 绘制节点 Ptn = [Ptn; x y]; % 存储节点 [x, y, button] = ginput(1); % 点击 end
if size(Ptn, 1) < 2 return; end
Ptn = [Ptn; Ptn(1, :)];
plot(Ptn(:, 1), Ptn(:, 2), 'ro-', 'LineWidth', 2, 'MarkerFaceColor', 'k'); title('原图像', 'Color', 'k', ... 'FontWeight', 'Bold');
mask = Get_PointSplineMask(I, Ptn); % 获取蒙板图像 subplot(1, 3, 2); imshow(mask); title('蒙板图像', 'Color', 'k', ... 'FontWeight', 'Bold');
if ndims(I) == 3
I1 = I .* uint8(cat(3, mask, mask, mask)); else
if ~isequal(size(I), size(mask))
mask = imresize(mask, size(I)); mask = logical(mask);
end
I1 = I .* mask; end
subplot(1, 3, 3); imshow(I1);
title('蒙板分割结果', 'Color', 'k', ... 'FontWeight', 'Bold');
imwrite(mask, filename);
function str = Pattern_Recognition(words) % 车牌字符识别 % 输入参数:
% words——车牌字符图像 % 输出参数:
% str——识别结果 pattern = [];
dirpath = './标准库/'; %读取所有样本文件 files = ls([dirpath '*.jpg']); for t = 1 : length(files)
imagedata = imread([dirpath, files(t,:)]); %读取样本文件 imagedata = im2bw(imagedata, 0.5); pattern(t).feature = imagedata; end
% 字符库列表 chepaiword=[...
'沪' 'C' 'E' 'N' '2' '6' '7' '浙' 'B' 'T' '2' '0' '0' '8' ... '皖' 'N' '6' '9' '3' '3' '5' '皖' 'A' 'M' '3' '7' '6' '7' ... '沪' 'B' '9' '1' '2' '1' '3' '沪' 'F' 'S' '2' '6' '9' '5' ... '吉' 'A' 'E' '3' '1' '2' '7' '沪' 'B' 'B' '3' '3' '9' '3' ... '沪' 'J' 'B' '7' '7' '7' '8' '京' 'H' 'Z' '9' '1' '3' '9' ... '晋' 'K' 'C' '3' '8' '4' '1' '苏' 'D' 'V' '0' '0' '0' '1' ... '苏' 'A' 'V' '8' '8' '8' '8' '浙' 'B' '8' '8' '8' '8' '8' ... '沪' 'F' 'Q' '1' '2' '7' '7' '苏' 'E' 'D' '0' '0' '0' '9' ... '沪' 'E' 'C' '1' '2' '8' '8' '苏' 'G' 'B' '0' '5' '7' '8' ... '沪' 'B' 'B' '2' '0' '2' '9' '沪' 'D' '5' '4' '2' '2' '6'];
for m = 1 : 7;
for n = 1 : length(files); switch m