/****************************************************************************
* @file Template matching.c
* @date 2021-06-08
* @author Xiaoxiao
* @version CVI2012 + IMAQ vision6.0
* @brief 数图大作业:基于模板匹配的文字识别
* @blog https://blog.csdn.net/weixin_43470383/article/details/117478961
https://blog.csdn.net/weixin_43470383/article/details/117486707
https://blog.csdn.net/weixin_43470383/article/details/117518445
*****************************************************************************/
#include <utility.h>
#include <ansi_c.h>
#include <cvirte.h>
#include <userint.h>
#include "nivision.h"
#include "Template matching.h"
#define num_word 362 // 字的个数
static int panelHandle;
static Image *srcImage; // 输入图像
static Image *dstImage; // 输出图像
static Image *binImage; // 二值化图像
static Image *rowSplitImage; // 行分割图像
static Image *colSplitImage; // 列分割图像
static Image *splitImage; // 最终分割图像
static Image *fontsImage[num_word]; // 字库汉字图像
static int width,height; // 原图像的宽、高
static int _height[1024]={0}; // 行分割后每行文字的高度
static int _width[100][50]={0}; // 列分割后每行文字中每个字的宽度
static int topRow[100]={0}; // 每行的上边界
static int bottomRow[100]={0}; // 每行的下边界
int canvasImg; // 幕布显示存储文件
int num_row=0; // 文本的行数
int leftMargin[100][50]={0}; // 每个字符左边界的位置
int rightMargin[100][50]={0}; // 每个字符右边界的位置
int num_col[1024]={0}; // 每行字数
int maxw[1024]={0}; // 每行单个汉字最大的宽度
int number_w[200]={0}; // 每个字每行的白点数目
int topMargin[50][50]={0}; // 每个字的上边界
int bottomMargin[50][50]={0}; // 每个汉字的下边界
Rect Extraction; // 分割后的区域
Point startPoint, endPoint, startPoint1, endPoint1; // 分割划线的起点和终点
char word[num_word][3]={"。",",","?","“",":",";","”","!","、","...",
"伟","主","被","路","或","田","民","里","才","残",
"都","恶","先","出","次","难","电","久","害","白",
"前","选","靠","很","课","两","控","收","快","亏",
"只","来","拦","蓝","老","了","离","连","练","凉",
"空","吗","满","没","块","幕","拿","那","奶","从",
"做","但","弄","屏","去","下","吃","想","小","新",
"抗","再","在","这","拉","中","周","吧","子","自",
"脑","牛","到","等","点","掉","差","对","额","场",
"而","该","敢","杠","高","工","观","国","过","还",
"卡","好","和","可","后","会","别","家","就","口",
"跨","不","比","拾","补","明","漠","派","盘","平",
"看","钱","却","热","日","如","若","沙","时","说",
"死","踏","太","并","条","同","投","图","哇","外",
"玩","王","围","问","线","像","样","要","已","用",
"有","知","左","阿","八","开","本","必","步","曾",
"力","当","动","饿","系","乏","转","舞","骨","朝",
"机","加","将","讲","他","接","今","青","一","旧",
"苦","劳","零","六","度","乱","能","河","怕","拍",
"百","跑","配","珊","月","普","七","其","人","暗",
"任","三","杀","身","十","是","四","所","体","天",
"为","把","我","五","心","性","也","以","益","统",
"于","域","张","志","走","个","无","分","赞","析",
"继","术","保","护","制","技","坐","独","川","林",
"照","幽","篁","弹","琴","复","长","啸","深","阙",
"江","红","岳","飞","怒","发","冲","冠","凭","栏",
"处","潇","雨","歇","抬","望","眼","仰","啸","壮",
"怀","激","烈","功","名","尘","与","土","千","云",
"和","莫","闲","少","年","头","空","悲","切","靖",
"康","耻","犹","未","雪","臣","恨","何","灭","驾",
"车","破","贺","兰","山","缺","饥","餐","胡","虏",
"肉","笑","谈","渴","饮","匈","奴","血","待","玉",
"案","元","夕","辛","弃","疾","东","风","夜","放",
"花","树","更","吹","落","星","宝","马","雕","香",
"凤","箫","声","壶","光","鱼","龙","蛾","儿","柳",
"黄","金","缕","语","盈","众","里","寻","蓦","然",
"回","首"};
int main (int argc, char *argv[])
{
if (InitCVIRTE (0, argv, 0) == 0)
return -1;
if ((panelHandle = LoadPanel(0, "Template matching.uir", PANEL)) < 0)
return -1;
DisplayPanel(panelHandle);
srcImage = imaqCreateImage(IMAQ_IMAGE_U8, 2);
dstImage = imaqCreateImage(IMAQ_IMAGE_U8, 2);
binImage = imaqCreateImage(IMAQ_IMAGE_U8, 2);
rowSplitImage = imaqCreateImage(IMAQ_IMAGE_U8, 2);
splitImage = imaqCreateImage(IMAQ_IMAGE_U8, 2);
colSplitImage = imaqCreateImage(IMAQ_IMAGE_U8, 2);
char AllImagePath[num_word][260], location[20];
// 获取路径,读入字库中所有图片,建立模板库
for(int i = 0; i < num_word; i++)
{
fontsImage[i] = imaqCreateImage (IMAQ_IMAGE_U8, 3);
GetProjectDir(AllImagePath[i]);
sprintf(location,"\\train\\%d.bmp",i);
strcat(AllImagePath[i], location);
imaqReadFile(fontsImage[i], AllImagePath[i], NULL, NULL);
// 样本图片二值化处理,构成标准汉字模板库
imaqThreshold(fontsImage[i], fontsImage[i], 100, 255, TRUE, 255);
}
RunUserInterface();
DiscardPanel(panelHandle);
return 0;
}
/*****************************加载图片**********************************/
int CVICALLBACK Load_image (int panel, int control, int event, void *callbackData, int eventData1, int eventData2)
{
char fileName[512];
int status;
switch (event)
{
case EVENT_COMMIT:
status = FileSelectPopup ("", "*.bmp*", "", "select an image file", VAL_LOAD_BUTTON, 0, 0, 1, 0, fileName);
if(status = 1)
{
// 显示输入的测试图片
imaqReadFile(srcImage, fileName, NULL, NULL);
imaqCloseWindow(IMAQ_ALL_WINDOWS);
CanvasClear(panelHandle, PANEL_CANVAS_IN, VAL_ENTIRE_OBJECT);
GetBitmapFromFile(fileName, &canvasImg);
CanvasDrawBitmap(panelHandle, PANEL_CANVAS_IN, canvasImg, VAL_ENTIRE_OBJECT, VAL_ENTIRE_OBJECT);
DiscardBitmap(canvasImg);
// 测试图片二值化和反色
imaqThreshold(binImage, srcImage, 100, 255, TRUE, 255);
imaqNor(binImage, binImage, binImage);
}
break;
}
return 0;
}
/*****************************行分割**********************************/
int CVICALLBACK rowSplit (int panel, int control, int event, void *callbackData, int eventData1, int eventData2)
{
Point point;
PixelValue grayLevel;
// 记录每行文字上界和下界的数目
int topm=0, botm=0;
// 获取输入图片的宽和高
imaqGetImageSize (srcImage, &width, &height);
// 每行(反色后)白点的数目(注意必须在获取图片的高度的前提下)
int *pt = calloc(height, sizeof(int));
switch (event)
{
case EVENT_COMMIT:
// 出现(反色后)白色像素则确定边界
for(int h = 0; h < height; h++)
{ for(int w = 0; w < width; w++)
{ point.x = w;
point.y = h;
imaqGetPixel(binImage, point, &grayLevel);
if(grayLevel.grayscale == 255.0)
*(pt + h) = *(pt + h) + 1;
}
}
// 记录每行文字上界和文字下界
for(int h = 0; h < height - 1; h++)
{
if(*(pt + h) == 0 & *(pt + h + 1) > 0)
{
topRow[topm++] = h;
num_row = num_row + 1;
// 划线显示行分割效果
startPoint.x = 0;
startPoint.y = h;
endPoint.x = width;
endPoint.y = h;
imaqDrawLineOnImage(srcImage, srcImage, IMAQ_DRAW_VALUE, startPoint, endPoint, 0.0);
}
if(*(pt + h) > 0 & *(pt + h + 1) == 0)
{
bottomRow[botm++] = h;
startPoint.x = 0;
startPoint.y = h;
endPoint.x = width;
endPoint.y = h;
imaqDrawLineOnImage(srcImage, srcImage, IMAQ_DRAW_VALUE, startPoint, endPoint, 0.0);
}
}
// 记录行分割后每行文字的高度
for(int topm = 0; topm < num_row; topm++)
_height[topm] = bottomRow[topm] - topRow[topm];
imaqWri
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
不建议下载,建议自己亲手做一遍! 平台:CVI2012 + IMAQ vision6.0 实现过程: https://blog.csdn.net/weixin_43470383/article/details/117478961 https://blog.csdn.net/weixin_43470383/article/details/117486707 https://blog.csdn.net/weixin_43470383/article/details/117518445
资源推荐
资源详情
资源评论
收起资源包目录
数图-基于模板匹配的文字识别源码 (373个子文件)
满江红.bmp 9.27MB
35.bmp 5KB
38.bmp 5KB
259.bmp 5KB
220.bmp 5KB
217.bmp 5KB
188.bmp 5KB
56.bmp 5KB
184.bmp 5KB
249.bmp 5KB
280.bmp 5KB
226.bmp 5KB
256.bmp 5KB
145.bmp 5KB
224.bmp 5KB
298.bmp 5KB
230.bmp 5KB
63.bmp 5KB
192.bmp 5KB
180.bmp 5KB
250.bmp 5KB
317.bmp 5KB
254.bmp 5KB
103.bmp 5KB
228.bmp 5KB
124.bmp 5KB
8.bmp 5KB
305.bmp 5KB
219.bmp 5KB
130.bmp 5KB
65.bmp 5KB
152.bmp 5KB
356.bmp 5KB
110.bmp 5KB
162.bmp 5KB
81.bmp 5KB
19.bmp 5KB
186.bmp 5KB
114.bmp 5KB
283.bmp 5KB
142.bmp 5KB
310.bmp 5KB
257.bmp 5KB
352.bmp 5KB
331.bmp 5KB
11.bmp 5KB
16.bmp 5KB
159.bmp 5KB
112.bmp 5KB
85.bmp 5KB
105.bmp 5KB
193.bmp 5KB
82.bmp 5KB
31.bmp 5KB
99.bmp 5KB
252.bmp 5KB
44.bmp 5KB
69.bmp 5KB
318.bmp 5KB
321.bmp 5KB
173.bmp 5KB
3.bmp 5KB
197.bmp 5KB
75.bmp 5KB
41.bmp 5KB
2.bmp 5KB
100.bmp 5KB
22.bmp 5KB
316.bmp 5KB
9.bmp 5KB
30.bmp 5KB
79.bmp 5KB
210.bmp 5KB
238.bmp 5KB
343.bmp 5KB
357.bmp 5KB
149.bmp 5KB
209.bmp 5KB
25.bmp 5KB
106.bmp 5KB
97.bmp 5KB
234.bmp 5KB
336.bmp 5KB
108.bmp 5KB
332.bmp 5KB
123.bmp 5KB
344.bmp 5KB
93.bmp 5KB
291.bmp 5KB
221.bmp 5KB
119.bmp 5KB
132.bmp 5KB
165.bmp 5KB
60.bmp 5KB
334.bmp 5KB
89.bmp 5KB
144.bmp 5KB
191.bmp 5KB
358.bmp 5KB
155.bmp 5KB
共 373 条
- 1
- 2
- 3
- 4
不雨_亦潇潇
- 粉丝: 2w+
- 资源: 27
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
前往页