news 2026/8/2 23:56:12

【图像识别】基于模板匹配算法实现英文印刷字符识别matlab代码

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
【图像识别】基于模板匹配算法实现英文印刷字符识别matlab代码

1 简介

2 部分代码

function MainForm global bw; global bl; global bll; global s; global fontSize; global charpic; global hMainFig; global pic; global hText; clc; close all; warning off all; if ~exist(fullfile(pwd, 'pic'), 'dir') mkdir(fullfile(pwd, 'pic')); end picname = fullfile(pwd, 'image.jpg'); pic = imread(picname); s = size(pic); if length(s) == 3 pic = rgb2gray(pic); end bw = im2bw(pic, 0.7); bw = ~bw; for i = 1 : s(1) if sum(bw(i,:) ~=0) > 0 FontSize_s = i; break; end end for i = FontSize_s : s(1) if sum(bw(i,:) ~=0) == 0 FontSize_e = i; break; end end FontSizeT = FontSize_e - FontSize_s; fontName = '宋体'; fontSize = FontSizeT; bw1 = imclose(bw, strel('line', 4, 90)); bw2 = bwareaopen(bw1, 20); bwi2 = bwselect(bw2, 368, 483, 4); bw2(bwi2) = 0; bw3 = bw .* bw2; bw4 = imclose(bw3, strel('square', 4)); [Lbw4, numbw4] = bwlabel(bw4); stats = regionprops(Lbw4); for i = 1 : numbw4 tempBound = stats(i).BoundingBox; tempPic = imcrop(pic, tempBound); tempStr = fullfile(pwd, sprintf('pic\\%03d.jpg', i)); imwrite(tempPic, tempStr); end [bl, num] = bwlabel(bw1, 4); chars = [char(uint8('A'):uint8('Z')), uint8('a'):uint8('z'), uint8('0'):uint8('9')]; eleLen = length(chars); charpic = cell(1,eleLen); hf1 = figure('Visible', 'Off'); imshow(zeros(32,32)); h = text(15, 15, 'a', 'Color', 'w', 'Fontname', fontName, 'FontSize', fontSize); for p = 1 : eleLen set(h, 'String', chars(p)); fh = getframe(hf1, [85, 58, 30, 30]); temp = fh.cdata; temp = im2bw(temp, 0.2); [r, c] = find(temp == 0); rect = [min(c) min(r) max(c)-min(c) max(r)-min(r)]; temp = imcrop(temp, rect); [f1, f2] = find(temp == 1); sz = size(temp); temp = temp(max(1, min(f1)-1):min(max(f1)+1, sz(1)),max(1, min(f2)-1):min(max(f2)+1, sz(2))); charpic{p} = temp; end delete(hf1); bll = zeros(size(bl)); for i = 1:num [f1, f2] = find(bl == i); bll(min(f1):max(f1), min(f2):max(f2)) = i; end hMainFig = figure(1); imshow(picname, 'Border', 'loose'); hold on; for i = 1 : numbw4 tempBound = stats(i).BoundingBox; rectangle('Position', tempBound, 'EdgeColor', 'r'); end hText = axes('Units', 'Normalized', 'Position', [0 0 0.1 0.1]); axis off; set(hMainFig, 'WindowButtonMotionFcn', @ShowPointData); end function ShowPointData(hObject, eventdata, handles) global bw; global bl; global bll; global s; global charpic; global hMainFig; global pic; global hText; p = get(gca,'currentpoint'); x = p(3); y = p(1); if x<1 || x>s(1) || y<1 || y>s(2) return; end curlabel = bll(uint32(x), uint32(y)); if curlabel ~= 0 [f1, f2] = find(bl == curlabel); minx = min(f1); maxx = max(f1); miny = min(f2); maxy = max(f2); tempic = pic(minx:maxx, miny:maxy); temp = bw(minx:maxx, miny:maxy); tempIm = zeros(round(size(temp)*2)); tempIm = logical(tempIm); tempIm(round((size(tempIm, 1)-size(temp, 1))/2):round((size(tempIm, 1)-size(temp, 1))/2)+size(temp, 1)-1, ... round((size(tempIm, 2)-size(temp, 2))/2):round((size(tempIm, 2)-size(temp, 2))/2)+size(temp, 2)-1) = temp; set(0, 'CurrentFigure', hMainFig); imshow(tempIm, [], 'Parent', hText); mincost = 100000; mark = 1; for i = 1 : length(charpic) temp1 = charpic{i}; ss = size(temp); temp1 = imresize(temp1, ss); % tempcost = sum(sum(abs(temp - temp1))); tempcost = norm(temp - temp1); if tempcost < mincost mincost = tempcost; mark = i; end end end end

3 仿真结果

4 参考文献

[1]赖特. "基于模板匹配及人工神经网络算法的图像识别应用——MATLAB实现机动车牌号码辨识." 智能建筑与智慧城市 11(2017):5.

部分理论引用网络文献,若有侵权联系博主删除。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/8/2 23:55:25

爬虫转大模型:你的采集能力还能打吗?

聊《大模型岗位变了&#xff0c;爬虫工程师该补的还是算法吗&#xff1f;》之前&#xff0c;先说一句实在的&#xff1a;别急着背概念&#xff0c;先看它在真实项目里到底解决什么问题。 摘要 摘要&#xff1a;从爬虫到 AI 工程师&#xff0c;中间隔着的不是算法&#xff0c;…

作者头像 李华
网站建设 2026/8/2 23:53:01

Excel数据透视表:从核心概念到实战应用,快速掌握数据分析利器

1. 从数据泥潭到清晰洞察&#xff1a;透视表为何是Excel的灵魂如果你经常和Excel打交道&#xff0c;处理过成百上千行的销售记录、库存清单或者项目报表&#xff0c;那你一定经历过这种痛苦&#xff1a;面对密密麻麻的数字&#xff0c;老板却要你“快速分析一下这个季度的区域销…

作者头像 李华
网站建设 2026/8/2 23:52:58

gh_mirrors/ex/exporters贡献指南:为新模型架构添加Core ML支持

gh_mirrors/ex/exporters贡献指南&#xff1a;为新模型架构添加Core ML支持 【免费下载链接】exporters Export Hugging Face models to Core ML and TensorFlow Lite 项目地址: https://gitcode.com/gh_mirrors/ex/exporters 欢迎参与GitHub加速计划的ex/exporters项目…

作者头像 李华
网站建设 2026/8/2 23:51:33

结构光照明显微镜(SIM)原理、搭建与超分辨成像实战指南

1. 项目概述&#xff1a;从“看见”到“看清”的显微革命在生命科学和材料研究的微观世界里&#xff0c;分辨率就是一切。传统宽场荧光显微镜&#xff0c;就像在雾霾天里看远处的灯光&#xff0c;虽然能看到光点&#xff0c;但光点之间相互重叠、模糊不清&#xff0c;我们称之为…

作者头像 李华