/****************************************************************************
* @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
不雨_亦潇潇
- 粉丝: 2w+
- 资源: 27
最新资源
- 白色大气风格的全球旅游公司模板下载.zip
- 白色大气风格的三维设计网页CSS模板下载.zip
- 白色大气风格的色彩管理网站模板下载.zip
- 白色大气风格的商务公司官网模板下载.zip
- 白色大气风格的商务公司企业网站模板.zip
- 白色大气风格的时尚服装品牌模板下载.zip
- 白色大气风格的时间轴房地产模板下载.zip
- 白色大气风格的时尚服装商城模板下载.zip
- 白色大气风格的时装网站模板下载.zip
- 白色大气风格的时装设计公司模板下载.zip
- 白色大气风格的时装在线购物商城模板.zip
- 白色大气风格的世界名表网站模板下载.zip
- 白色大气风格的室内设计企业网站模板.zip
- 白色大气风格的视察滚动房地产模板下载.zip
- 白色大气风格的室内装修设计企业网站模板.zip
- 白色大气风格的室内装修模板下载.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
- 3
- 4
前往页