功能:识别指定文件夹里面的图纸上面的指定位置的内容,将此内容给这个图纸文件命名,然后存放在指定文件夹(所谓指定,就是控制台输入路径即可)。
准备工作:
效果图:
完整代码:
///
// File generated by HDevelop for HALCON/C++ Version 12.0
///
//头文件的预处理指令
#ifndef __APPLE__
# include "HalconCpp.h"
# include "HDevThread.h"
# if defined(__linux__) && !defined(NO_EXPORT_APP_MAIN)
# include <X11/Xlib.h>
# endif
#else
# ifndef HC_LARGE_IMAGES
# include <HALCONCpp/HalconCpp.h>
# include <HALCONCpp/HDevThread.h>
# else
# include <HALCONCppxl/HalconCpp.h>
# include <HALCONCppxl/HDevThread.h>
# endif
# include <stdio.h>
# include <HALCON/HpThread.h>
# include <CoreFoundation/CFRunLoop.h>
#endif
#include<Halcon.h>
#include<HalconCpp.h>
using namespace std;
using namespace HalconCpp;
//函数声明
void dev_update_off();
void dev_update_on();
void disp_message(HTuple hv_WindowHandle, HTuple hv_String, HTuple hv_CoordSystem,
HTuple hv_Row, HTuple hv_Column, HTuple hv_Color, HTuple hv_Box);
//函数定义
void dev_update_off()
{
return;
}
void dev_update_on()
{
return;
}
void disp_message(HTuple hv_WindowHandle, HTuple hv_String, HTuple hv_CoordSystem,
HTuple hv_Row, HTuple hv_Column, HTuple hv_Color, HTuple hv_Box)
{
//定义一些halcon里面用到的变量
HTuple hv_Red, hv_Green, hv_Blue, hv_Row1Part;
HTuple hv_Column1Part, hv_Row2Part, hv_Column2Part, hv_RowWin;
HTuple hv_ColumnWin, hv_WidthWin, hv_HeightWin, hv_MaxAscent;
HTuple hv_MaxDescent, hv_MaxWidth, hv_MaxHeight, hv_R1;
HTuple hv_C1, hv_FactorRow, hv_FactorColumn, hv_UseShadow;
HTuple hv_ShadowColor, hv_Exception, hv_Width, hv_Index;
HTuple hv_Ascent, hv_Descent, hv_W, hv_H, hv_FrameHeight;
HTuple hv_FrameWidth, hv_R2, hv_C2, hv_DrawMode, hv_CurrentColor;
GetRgb(hv_WindowHandle, &hv_Red, &hv_Green, &hv_Blue);
//获取窗口中感兴趣区域的位置信息
GetPart(hv_WindowHandle, &hv_Row1Part, &hv_Column1Part, &hv_Row2Part, &hv_Column2Part);
//获取窗口的尺寸和位置信息
GetWindowExtents(hv_WindowHandle, &hv_RowWin, &hv_ColumnWin, &hv_WidthWin, &hv_HeightWin);
//设置窗口显示的感兴趣区域
SetPart(hv_WindowHandle, 0, 0, hv_HeightWin - 1, hv_WidthWin - 1);
//default settings
if (0 != (hv_Row == -1))
{
hv_Row = 12;
}
if (0 != (hv_Column == -1))
{
hv_Column = 12;
}
if (0 != (hv_Color == HTuple()))
{
hv_Color = "";
}
//将变量hv_String转换为字符串,并按照换行符进行分割,将分割后的子字符串存储为元组
hv_String = (("" + hv_String) + "").TupleSplit("\n");
//
//获取字体大小
GetFontExtents(hv_WindowHandle, &hv_MaxAscent, &hv_MaxDescent, &hv_MaxWidth, &hv_MaxHeight);
if (0 != (hv_CoordSystem == HTuple("window")))
{
hv_R1 = hv_Row;
hv_C1 = hv_Column;
}
else
{
//图像坐标转换为窗口坐标
//计算行和列的缩放因子
hv_FactorRow = (1. * hv_HeightWin) / ((hv_Row2Part - hv_Row1Part) + 1);//窗口高度与行的差值的比例
hv_FactorColumn = (1. * hv_WidthWin) / ((hv_Column2Part - hv_Column1Part) + 1);//宽度
//使用计算出来的缩放因子将图像坐标(hv_Row和hv_Column)转换为窗口坐标(hv_R1和hv_C1)
hv_R1 = ((hv_Row - hv_Row1Part) + 0.5) * hv_FactorRow;
hv_C1 = ((hv_Column - hv_Column1Part) + 0.5) * hv_FactorColumn;
}
//根据文本大小显示文本框
hv_UseShadow = 1;//启用阴影效果
hv_ShadowColor = "gray";//设置阴影颜色
if (0 != (HTuple(hv_Box[0]) == HTuple("true")))//如果hv_Box[0]的值等于字符串“true”
{
hv_Box[0] = "#fce9d4";//设置方框颜色
hv_ShadowColor = "#f28d26";//设置阴影颜色
}
if (0 != ((hv_Box.TupleLength()) > 1))
{
if (0 != (HTuple(hv_Box[1]) == HTuple("true")))
{
//Use default ShadowColor set above
}
else if (0 != (HTuple(hv_Box[1]) == HTuple("false")))
{
hv_UseShadow = 0;//禁止使用阴影颜色
}
else
{
hv_ShadowColor = ((const HTuple&)hv_Box)[1];
//Valid color?
try
{
SetColor(hv_WindowHandle, HTuple(hv_Box[1]));
}
// catch (Exception)
catch (HalconCpp::HException& HDevExpDefaultException)
{
HDevExpDefaultException.ToHTuple(&hv_Exception);
hv_Exception = "Wrong value of control parameter Box[1] (must be a 'true', 'false', or a valid color string)";
throw HalconCpp::HException(hv_Exception);
}
}
}
if (0 != (HTuple(hv_Box[0]) != HTuple("false")))
{
//Valid color?
try
{
SetColor(hv_WindowHandle, HTuple(hv_Box[0]));
}
// catch (Exception)
catch (HalconCpp::HException& HDevExpDefaultException)
{
HDevExpDefaultException.ToHTuple(&hv_Exception);
hv_Exception = "Wrong value of control parameter Box[0] (must be a 'true', 'false', or a valid color string)";
throw HalconCpp::HException(hv_Exception);
}
//计算框的范围
hv_String = (" " + hv_String) + " ";
hv_Width = HTuple();
{
HTuple end_val93 = (hv_String.TupleLength()) - 1;
HTuple step_val93 = 1;
for (hv_Index = 0; hv_Index.Continue(end_val93, step_val93); hv_Index += step_val93)
{
GetStringExtents(hv_WindowHandle, HTuple(hv_String[hv_Index]), &hv_Ascent,
&hv_Descent, &hv_W, &hv_H);
hv_Width = hv_Width.TupleConcat(hv_W);
}
}
hv_FrameHeight = hv_MaxHeight * (hv_String.TupleLength());
hv_FrameWidth = (HTuple(0).TupleConcat(hv_Width)).TupleMax();
hv_R2 = hv_R1 + hv_FrameHeight;
hv_C2 = hv_C1 + hv_FrameWidth;
//Display rectangles
GetDraw(hv_WindowHandle, &hv_DrawMode);
SetDraw(hv_WindowHandle, "fill");
//Set shadow color
SetColor(hv_WindowHandle, hv_ShadowColor);
if (0 != hv_UseShadow)
{
DispRectangle1(hv_WindowHandle, hv_R1 + 1, hv_C1 + 1, hv_R2 + 1, hv_C2 + 1);
}
//Set box color
SetColor(hv_WindowHandle, HTuple(hv_Box[0]));
DispRectangle1(hv_WindowHandle, hv_R1, hv_C1, hv_R2, hv_C2);
SetDraw(hv_WindowHandle, hv_DrawMode);
}
//Write text.
{
//循环的结束点 每次循环 长度减一
HTuple end_val115 = (hv_String.TupleLength()) - 1;
//循环步长为1
HTuple step_val115 = 1;
for (hv_Index = 0; hv_Index.Continue(end_val115, step_val115); hv_Index += step_val115)
{
hv_CurrentColor = ((const HTuple&)hv_Color)[hv_Index % (hv_Color.TupleLength())];
if (0 != (HTuple(hv_CurrentColor != HTuple("")).TupleAnd(hv_CurrentColor != HTuple("auto"))))
{
SetColor(hv_WindowHandle, hv_CurrentColor);
}
else
{
SetRgb(hv_WindowHandle, hv_Red, hv_Green, hv_Blue);
}
hv_Row = hv_R1 + (hv_MaxHeight * hv_Index);
SetTposition(hv_WindowHandle, hv_Row, hv_C1);
//把识别到的字符写到窗口中
WriteString(hv_WindowHandle, HTuple(hv_String[hv_Index]));
}
}
//Reset changed window settings
SetRgb(hv_WindowHandle, hv_Red, hv_Green, hv_Blue);
SetPart(hv_WindowHandle, hv_Row1Part, hv_Column1Part, hv_Row2Part, hv_Column2Part);
return;
}
#ifndef NO_EXPORT_MAIN
// Main procedure
void action()
{
// Local iconic variables
HObject ho_Image, ho_ImageGray, ho_ROI_0, ho_ImageReduced;
HObject ho_Regions, ho_ConnectedRegions, ho_SelectedRegions;
HObject ho_SortedRegions;
// Local control variables
HTuple hv_WindowHandle, hv_ImageFiles, hv_Index;
HTuple hv_Width, hv_Height, hv_OCRHandle, hv_Class, hv_Confidence;
HTuple hv_result, hv_Index1, hv_Filename, hv_text, hv_Path, hv_savePath;
dev_update_off();
dev_update_on();
if (HDevWindowStack::IsOpen())
CloseWindow(HDevWindowStack::Pop());
SetWindowAttr("background_color", "black");
OpenWindow(0, 0, 800, 500, 0, "", "", &hv_WindowHandle);
HDevWindowStack::Push(hv_WindowHandle);
//Image Acquisition 01: Code generated by Image Acquisition 01
//"D:/halcon51/image"
cout << "请输入图片所在路径" << endl;
string path;
cin >> path;
hv_Path = path.c_str();
string str = (char*)(hv_Path.ToString().Text());
cout << "路径是" + str << endl;
string savePath;
cout << "请输入图片要保存的路径" << endl;
cin >> savePath;
cout << "保存成功!" << endl;
//将输入的字符串转换成char型
hv_savePath = savePath.c_str();
ListFiles(path.c_str(), (HTuple("files").Append("follow_links")), &hv_ImageFiles);
TupleRegexpSelect(hv_ImageFiles, (HTuple("\\.(tif|tiff|gif|bmp|jpg|jpeg|jp2|png|pcx|pgm|ppm|pbm|xwd|ima|hobj)$").Append("ignore_case")),
&hv_ImageFiles);
{
HTuple end_val7 = (hv_ImageFiles.TupleLength()) - 1;
HTuple step_val7 = 1;
for (hv_Index = 0; hv_Index.Continue(end_val7, step_val7); hv_Index += step_val7)
{
ReadImage(&ho_Image, HTuple(hv_ImageFiles[hv_Index]));
//Image Acquisition 01: Do something
GetImageSize(ho_Image, &hv_Width, &hv_Height);
Rgb3ToGray(ho_Image, ho_Image, ho_Image, &ho_ImageGray);
GenRectangle1(&ho_ROI_0, 4662.84, 5673.69, 4791.83, 6533.15);
ReduceDomain(ho_ImageGray, ho_ROI_0, &ho_ImageReduced);
Threshold(ho_ImageReduced, &ho_Regions, 0, 128);
Connection(ho_Regions, &ho_ConnectedRegions);
SelectShape(ho_ConnectedRegions, &ho_SelectedRegions, "area", "and", 100, 500);
SortRegion(ho_SelectedRegions, &ho_SortedRegions, "first_point", "true", "column");
ReadOcrClassMlp("Industrial_0-9A-Z_NoRej.omc", &hv_OCRHandle);
DoOcrMultiClassMlp(ho_SortedRegions, ho_ImageReduced, hv_OCRHandle, &hv_Class,
&hv_Confidence);
disp_message(hv_WindowHandle, hv_Class, "window", 12, 12, "black", "true");
hv_result = "";
{
HTuple end_val22 = (hv_Class.TupleLength()) - 1;
HTuple step_val22 = 1;
for (hv_Index1 = 0; hv_Index1.Continue(end_val22, step_val22); hv_Index1 += step_val22)
{
hv_result += HTuple(hv_Class[hv_Index1]);
}
}
hv_Filename = savePath.c_str() + hv_result;
WriteImage(ho_Image, "png", 0, hv_Filename);
}
}
// stop(); only in hdevelop
if (HDevWindowStack::IsOpen())
ClearWindow(HDevWindowStack::GetActive());
}
#ifndef NO_EXPORT_APP_MAIN
#ifdef __APPLE__
// On OS X systems, we must have a CFRunLoop running on the main thread in
// order for the HALCON graphics operators to work correctly, and run the
// action function in a separate thread. A CFRunLoopTimer is used to make sure
// the action function is not called before the CFRunLoop is running.
HTuple gStartMutex;
H_pthread_t gActionThread;
static void timer_callback(CFRunLoopTimerRef timer, void* info)
{
UnlockMutex(gStartMutex);
}
static Herror apple_action(void** parameters)
{
LockMutex(gStartMutex);
action();
CFRunLoopStop(CFRunLoopGetMain());
return H_MSG_OK;
}
static int apple_main(int argc, char* argv[])
{
Herror error;
CFRunLoopTimerRef Timer;
CFRunLoopTimerContext TimerContext = { 0, 0, 0, 0, 0 };
CreateMutex("type", "sleep", &gStartMutex);
LockMutex(gStartMutex);
error = HpThreadHandleAlloc(&gActionThread);
if (H_MSG_OK != error)
{
fprintf(stderr, "HpThreadHandleAlloc failed: %d\n", error);
exit(1);
}
error = HpThreadCreate(gActionThread, 0, apple_action);
if (H_MSG_OK != error)
{
fprintf(stderr, "HpThreadCreate failed: %d\n", error);
exit(1);
}
Timer = CFRunLoopTimerCreate(kCFAllocatorDefault,
CFAbsoluteTimeGetCurrent(), 0, 0, 0,
timer_callback, &TimerContext);
if (!Timer)
{
fprintf(stderr, "CFRunLoopTimerCreate failed\n");
exit(1);
}
CFRunLoopAddTimer(CFRunLoopGetCurrent(), Timer, kCFRunLoopCommonModes);
CFRunLoopRun();
CFRunLoopRemoveTimer(CFRunLoopGetCurrent(), Timer, kCFRunLoopCommonModes);
CFRelease(Timer);
error = HpThreadHandleFree(gActionThread);
if (H_MSG_OK != error)
{
fprintf(stderr, "HpThreadHandleFree failed: %d\n", error);
exit(1);
}
ClearMutex(gStartMutex);
return 0;
}
#endif
int main(int argc, char* argv[])
{
// Default settings used in HDevelop (can be omitted)
int ret = 0;
SetSystem("width", 512);
SetSystem("height", 512);
#if defined(_WIN32)
SetSystem("use_window_thread", "true");
#elif defined(__linux__)
XInitThreads();
#endif
#ifndef __APPLE__
action();
#else
ret = apple_main(argc, argv);
#endif
return ret;
}
#endif
#endif
运行效果:
根据提示手动输入路径
运行之后
此方法可以批量管理图纸文件,也可以批量命名文件。