halcon第九讲:halcon联合vc实现ocr识别

一、先在halcon中实现ocr识别

所要识别的图如下:

*联合vc时要用绝对路径
read_image (Image, 'C://Users//Administrator//Desktop/halcon//第九讲、halcon联合vc实现ocr识别//1.png')
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
dev_display (Image)

*定位校正
words:=['S','K','2','1','3','0','A','0','4','0','6','2','0']
gen_rectangle1 (ROI_0, 168.5, 16.5, 379.5, 608.5)
text_line_orientation (ROI_0, Image, 25, -0.523599, 0.523599, OrientationAngle)
area_center (ROI_0, Area, Row, Column)
vector_angle_to_rigid (Row, Column, OrientationAngle, Row, Column, 0, HomMat2D)
affine_trans_image (Image, ImageAffinTrans, HomMat2D, 'constant', 'false')
*获得点图,点的半径为5,偏移量为2,感兴趣点为黑色点
dots_image (ImageAffinTrans, DotImage,5, 'dark',2)

gen_rectangle1 (ROI_0, 195.5, 14.5, 355.5, 612.5)
reduce_domain (DotImage, ROI_0, ImageReduced)
threshold (ImageReduced, Regions, 20, 255)

*形态学运算
gen_rectangle2 (Rectangle1, 0, 0, rad(45), 8, 4)
closing (Regions, Rectangle1, RegionClosing)
gen_rectangle2 (Rectangle2, 0, 0, rad(145), 8, 4)
closing (RegionClosing, Rectangle2, RegionClosing)
dilation_circle (RegionClosing, RegionDilation1, 4.5)
connection (RegionDilation1, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 985.32, 2000.01)

*动态分割
partition_dynamic (SelectedRegions, Partitioned, 60, 20)
sort_region (Partitioned, SortedRegions, 'character', 'true', 'row')
count_obj (SortedRegions, Number)

*创建trf训练文件
for i:=1 to Number by 1
    select_obj (SortedRegions, ObjectSelected, i)
    *区域和区域作交集
    intersection (ObjectSelected, Regions, RegionIntersection)
    append_ocr_trainf (RegionIntersection, ImageAffinTrans, words[i-1], 'C://Users//Administrator//Desktop/halcon//第九讲、halcon联合vc实现ocr识别//train_ocr')
    *disp_continue_message (WindowHandle, 'black', 'true')
    *stop()
endfor

read_ocr_trainf_names ('C://Users//Administrator//Desktop/halcon//第九讲、halcon联合vc实现ocr识别//train_ocr', CharacterNames, CharacterCount)
create_ocr_class_mlp (8, 10, 'constant', 'default', CharacterNames, 80, 'none', 10, 42, OCRHandle)
trainf_ocr_class_mlp (OCRHandle, 'C://Users//Administrator//Desktop/halcon//第九讲、halcon联合vc实现ocr识别//train_ocr.trf', 200, 1, 0.01, Error, ErrorLog)
*也可直接读入halcon训练好的模型文件,则上面的创建训练文件、分类器以及训练都可以不要了
* read_ocr_class_mlp ('Industrial_0-9A-Z_NoRej.omc', OCRHandle)
intersection (SortedRegions, Regions, RegionIntersection1)
do_ocr_multi_class_mlp (RegionIntersection1, ImageAffinTrans, OCRHandle, Class, Confidence)

smallest_rectangle1 (RegionIntersection1, Row1, Column1, Row2, Column2)
set_display_font (WindowHandle, 26, 'mono', 'true', 'false')
dev_display (ImageAffinTrans)
for j:=0 to Number-1 by 1
   disp_message (WindowHandle, Class[j], 'image', Row1[j]+120, Column1[j], 'white', 'false')
endfor

用自己训练的模型识别结果如下 :

用halcon自带的模型识别结果如下:

二、halcon联合vc实现ocr识别

1、新建MFC工程;

2、进行配置,halcon在vc中的配置在我的上一篇博客有所讲解;

3、删除默认控件,添加自己需要的控件;

4、为button按钮添加消息响应函数,将action里的代码拷贝到里面,因为需要显示所以还需要拷贝显示函数;

5、最后我们需要把结果显示到编辑框中,事件要有行为,需要由变量来实现,所以还需要为编辑控件添加一个变量。

void CMFCApplication2Dlg::OnBnClickedButton1()
{
	// Local iconic variables
	HObject  ho_Image, ho_ROI_0, ho_ImageAffinTrans;
	HObject  ho_DotImage, ho_ImageReduced, ho_Regions, ho_Rectangle1;
	HObject  ho_RegionClosing, ho_Rectangle2, ho_RegionDilation1;
	HObject  ho_ConnectedRegions, ho_SelectedRegions, ho_Partitioned;
	HObject  ho_SortedRegions, ho_ObjectSelected, ho_RegionIntersection;
	HObject  ho_RegionIntersection1;

	// Local control variables
	HTuple  hv_Width, hv_Height, hv_WindowHandle;
	HTuple  hv_words, hv_OrientationAngle, hv_Area, hv_Row;
	HTuple  hv_Column, hv_HomMat2D, hv_Number, hv_i, hv_CharacterNames;
	HTuple  hv_CharacterCount, hv_OCRHandle, hv_Error, hv_ErrorLog;
	HTuple  hv_Class, hv_Confidence, hv_Row1, hv_Column1, hv_Row2;
	HTuple  hv_Column2, hv_j;
	HTuple kuan, gao;
	ReadImage(&ho_Image, "C://Users//Administrator//Desktop/halcon//第九讲、halcon联合vc实现ocr识别//1.png");
	GetImageSize(ho_Image, &hv_Width, &hv_Height);
	SetWindowAttr("background_color", "black");

   //【***】关联显示窗口
	HWND hwnd;
	hwnd = GetDlgItem(PIC)->m_hWnd;
	LONG windowID = (LONG)hwnd;
	CRect rect;
	GetDlgItem(PIC)->GetWindowRect(&rect);
	kuan[0] = rect.Width();
	gao[0] = rect.Height();

	OpenWindow(0, 0, kuan[0], gao[0], windowID, "", "", &hv_WindowHandle);
	HDevWindowStack::Push(hv_WindowHandle);
	if (HDevWindowStack::IsOpen())
		DispObj(ho_Image, HDevWindowStack::GetActive());

	//定位校正
	hv_words.Clear();
	hv_words[0] = "S";
	hv_words[1] = "K";
	hv_words[2] = "2";
	hv_words[3] = "1";
	hv_words[4] = "3";
	hv_words[5] = "0";
	hv_words[6] = "A";
	hv_words[7] = "0";
	hv_words[8] = "4";
	hv_words[9] = "0";
	hv_words[10] = "6";
	hv_words[11] = "2";
	hv_words[12] = "0";
	GenRectangle1(&ho_ROI_0, 168.5, 16.5, 379.5, 608.5);
	TextLineOrientation(ho_ROI_0, ho_Image, 25, -0.523599, 0.523599, &hv_OrientationAngle);
	AreaCenter(ho_ROI_0, &hv_Area, &hv_Row, &hv_Column);
	VectorAngleToRigid(hv_Row, hv_Column, hv_OrientationAngle, hv_Row, hv_Column, 0,
		&hv_HomMat2D);
	AffineTransImage(ho_Image, &ho_ImageAffinTrans, hv_HomMat2D, "constant", "false");
	//获得点图,点的半径为5,偏移量为2,感兴趣点为黑色点
	DotsImage(ho_ImageAffinTrans, &ho_DotImage, 5, "dark", 2);

	GenRectangle1(&ho_ROI_0, 195.5, 14.5, 355.5, 612.5);
	ReduceDomain(ho_DotImage, ho_ROI_0, &ho_ImageReduced);
	Threshold(ho_ImageReduced, &ho_Regions, 20, 255);

	//形态学运算
	GenRectangle2(&ho_Rectangle1, 0, 0, HTuple(45).TupleRad(), 8, 4);
	Closing(ho_Regions, ho_Rectangle1, &ho_RegionClosing);
	GenRectangle2(&ho_Rectangle2, 0, 0, HTuple(145).TupleRad(), 8, 4);
	Closing(ho_RegionClosing, ho_Rectangle2, &ho_RegionClosing);
	DilationCircle(ho_RegionClosing, &ho_RegionDilation1, 4.5);
	Connection(ho_RegionDilation1, &ho_ConnectedRegions);
	SelectShape(ho_ConnectedRegions, &ho_SelectedRegions, "area", "and", 985.32, 2000.01);

	//动态分割
	PartitionDynamic(ho_SelectedRegions, &ho_Partitioned, 60, 20);
	SortRegion(ho_Partitioned, &ho_SortedRegions, "character", "true", "row");
	CountObj(ho_SortedRegions, &hv_Number);

	//创建trf训练文件
	{
		HTuple end_val34 = hv_Number;
		HTuple step_val34 = 1;
		for (hv_i = 1; hv_i.Continue(end_val34, step_val34); hv_i += step_val34)
		{
			SelectObj(ho_SortedRegions, &ho_ObjectSelected, hv_i);
			//区域和区域作交集
			Intersection(ho_ObjectSelected, ho_Regions, &ho_RegionIntersection);
			AppendOcrTrainf(ho_RegionIntersection, ho_ImageAffinTrans, HTuple(hv_words[hv_i - 1]),
				"C://Users//Administrator//Desktop/halcon//第九讲、halcon联合vc实现ocr识别//train_ocr");
			//disp_continue_message (WindowHandle, 'black', 'true')
			//stop ()
		}
	}

	ReadOcrTrainfNames("C://Users//Administrator//Desktop/halcon//第九讲、halcon联合vc实现ocr识别//train_ocr",
		&hv_CharacterNames, &hv_CharacterCount);
	CreateOcrClassMlp(8, 10, "constant", "default", hv_CharacterNames, 80, "none",
		10, 42, &hv_OCRHandle);
	TrainfOcrClassMlp(hv_OCRHandle, "C://Users//Administrator//Desktop/halcon//第九讲、halcon联合vc实现ocr识别//train_ocr.trf",
		200, 1, 0.01, &hv_Error, &hv_ErrorLog);
	//也可直接读入halcon训练好的模型文件,则上面的创建训练文件、分类器以及训练都可以不要了
	//read_ocr_class_mlp ('Industrial_0-9A-Z_NoRej.omc', OCRHandle)
	Intersection(ho_SortedRegions, ho_Regions, &ho_RegionIntersection1);
	DoOcrMultiClassMlp(ho_RegionIntersection1, ho_ImageAffinTrans, hv_OCRHandle, &hv_Class,
		&hv_Confidence);

	SmallestRectangle1(ho_RegionIntersection1, &hv_Row1, &hv_Column1, &hv_Row2, &hv_Column2);
	set_display_font(hv_WindowHandle, 26, "mono", "true", "false");
	if (HDevWindowStack::IsOpen())
		DispObj(ho_ImageAffinTrans, HDevWindowStack::GetActive());
	{
		HTuple end_val54 = hv_Number - 1;
		HTuple step_val54 = 1;
		for (hv_j = 0; hv_j.Continue(end_val54, step_val54); hv_j += step_val54)
		{
			disp_message(hv_WindowHandle, HTuple(hv_Class[hv_j]), "image", HTuple(hv_Row1[hv_j]) + 120,
				HTuple(hv_Column1[hv_j]), "white", "false");
			//【***】强制转换成字符串类型
			CString str = (CString)hv_Class[hv_j].S();
			result = result + str;
		}
	}
    //【***】将结果呈现出来
	UpdateData(FALSE);
}

运行结果如下:

 点击ocr采集识别按钮后结果如下:

猜你喜欢

转载自blog.csdn.net/qq_24946843/article/details/82114589