matlab图片汉字识别详细代码,包括灰度处理,中值滤波图像增强、sobel边缘检测、二值化处理、VGH直方图局部分割、基于字符投影的倾斜校正技术、文字切分、归一化处理、模板匹配法文字识别等
时间: 2023-11-29 09:35:55 浏览: 183
以下是一个简单的 MATLAB 图片汉字识别代码,其中包括了灰度处理、中值滤波图像增强、sobel 边缘检测、二值化处理、VGH 直方图局部分割、基于字符投影的倾斜校正技术、文字切分、归一化处理、模板匹配法文字识别等步骤。代码中的图片是一个黑底白字的汉字图片,可以根据需要进行修改。
```
clear all;
clc;
% 读取图片
img = imread('chinese.jpg');
figure(1);
imshow(img);
title('Original Image');
% 灰度处理
gray = rgb2gray(img);
figure(2);
imshow(gray);
title('Gray Scale Image');
% 中值滤波图像增强
med = medfilt2(gray,[3 3]);
figure(3);
imshow(med);
title('Median Filter Image');
% Sobel 边缘检测
sobel = edge(med,'sobel');
figure(4);
imshow(sobel);
title('Sobel Edge Detection Image');
% 二值化处理
bw = imbinarize(med);
figure(5);
imshow(bw);
title('Binary Image');
% VGH 直方图局部分割
vgh = VGH(bw);
figure(6);
imshow(vgh);
title('VGH Segmentation Image');
% 基于字符投影的倾斜校正技术
theta = skewCorrect(vgh);
rot = imrotate(vgh,-theta,'bilinear','crop');
figure(7);
imshow(rot);
title('Skew Correct Image');
% 文字切分
[chars, num_chars] = charSegment(rot);
for i = 1:num_chars
figure(i+7);
imshow(chars{i});
title(['Character ', num2str(i)]);
end
% 归一化处理
norm_chars = normalizeChar(chars, num_chars);
for i = 1:num_chars
figure(i+num_chars+7);
imshow(norm_chars{i});
title(['Normalized Character ', num2str(i)]);
end
% 模板匹配法文字识别
templates = {'一','二','三','四','五','六','七','八','九','十'};
result = '';
for i = 1:num_chars
char = norm_chars{i};
[match, letter] = matchTemplate(char, templates);
if match
result = [result, letter];
end
end
disp(['The recognized character is ', result]);
```
其中,`VGH` 函数用于进行直方图局部分割,`skewCorrect` 函数用于进行基于字符投影的倾斜校正技术,`charSegment` 函数用于进行文字切分,`normalizeChar` 函数用于进行归一化处理,`matchTemplate` 函数用于进行模板匹配法文字识别。这些函数的具体实现可以参考以下代码:
```
function vgh = VGH(img)
% VGH 直方图局部分割
[m,n] = size(img);
vgh = zeros(m,n);
for i = 1:m
for j = 1:n
if img(i,j) == 0
vgh(i,j) = 255;
end
end
end
for i = 1:m
for j = 1:n
if vgh(i,j) == 255
for k = 1:n
if vgh(i,k) == 0
break;
end
vgh(i,k) = 0;
end
for k = n:-1:1
if vgh(i,k) == 0
break;
end
vgh(i,k) = 0;
end
end
end
end
```
```
function theta = skewCorrect(img)
% 基于字符投影的倾斜校正技术
[m,n] = size(img);
theta = 0;
cnt = 0;
for i = -30:30
rot = imrotate(img,i,'bilinear','crop');
proj = sum(rot,2);
idx = find(proj>0);
if length(idx) > cnt
cnt = length(idx);
theta = i;
end
end
```
```
function [chars, num_chars] = charSegment(img)
% 文字切分
[m,n] = size(img);
chars = {};
num_chars = 0;
for i = 1:n
col = img(:,i);
if sum(col) == 0
continue;
end
if i == 1
num_chars = num_chars + 1;
chars{num_chars} = img(:,1:i);
elseif i == n
num_chars = num_chars + 1;
chars{num_chars} = img(:,i:end);
else
col_prev = img(:,i-1);
col_next = img(:,i+1);
if sum(col_prev) == 0 && sum(col_next) > 0
num_chars = num_chars + 1;
chars{num_chars} = img(:,1:i);
elseif sum(col_prev) > 0 && sum(col_next) == 0
num_chars = num_chars + 1;
chars{num_chars} = img(:,i:end);
end
end
end
```
```
function norm_chars = normalizeChar(chars, num_chars)
% 归一化处理
norm_chars = {};
for i = 1:num_chars
char = chars{i};
[m,n] = size(char);
if m > n
scale = 40/m;
char = imresize(char,scale);
[m,n] = size(char);
pad = floor((m-n)/2);
char = padarray(char,[0 pad],'both');
else
scale = 40/n;
char = imresize(char,scale);
[m,n] = size(char);
pad = floor((n-m)/2);
char = padarray(char,[pad 0],'both');
end
char = imresize(char,[64 64]);
norm_chars{i} = char;
end
```
```
function [match, letter] = matchTemplate(char, templates)
% 模板匹配法文字识别
[m,n] = size(char);
max_corr = 0;
match = false;
letter = '';
for i = 1:length(templates)
template = imread([templates{i}, '.bmp']);
[tm,tn] = size(template);
if tm > m || tn > n
continue;
end
corr = corr2(template, char(1:tm,1:tn));
if corr > max_corr
max_corr = corr;
letter = templates{i};
end
end
if max_corr > 0.7
match = true;
end
```
这个代码仅供参考,实际应用中可能需要进行更多的优化和调整。
阅读全文