QT5.8配置halcon环境

网友投稿 1028 2022-11-30

QT5.8配置halcon环境

QT5.8配置halcon环境

首先是新建一个qt项目halcon,这里我就不用多说了吧

第二,为了方便:我们将halcon安装目录下的include,lib文件夹拷贝到

这个halcon工程目录下:

第三,接着就是在.pro文件中添加包含目录和库目录。因为我们直接拷贝过来的所以目录比较简单

然后是cpp中的源码:

#include "mainwindow.h"#include "ui_mainwindow.h"#include #include "halconCpp.h"using namespace HalconCpp;void disp_message (HTuple hv_WindowHandle, HTuple hv_String, HTuple hv_CoordSystem, HTuple hv_Row, HTuple hv_Column, HTuple hv_Color, HTuple hv_Box){ // Local iconic variables // Local control variables 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; //This procedure displays text in a graphics window. // //Input parameters: //WindowHandle: The WindowHandle of the graphics window, where // the message should be displayed //String: A tuple of strings containing the text message to be displayed //CoordSystem: If set to 'window', the text position is given // with respect to the window coordinate system. // If set to 'image', image coordinates are used. // (This may be useful in zoomed images.) //Row: The row coordinate of the desired text position // If set to -1, a default value of 12 is used. //Column: The column coordinate of the desired text position // If set to -1, a default value of 12 is used. //Color: defines the color of the text as string. // If set to [], '' or 'auto' the currently set color is used. // If a tuple of strings is passed, the colors are used cyclically // for each new textline. //Box: If Box[0] is set to 'true', the text is written within an orange box. // If set to' false', no box is displayed. // If set to a color string (e.g. 'white', '#FF00CC', etc.), // the text is written in a box of that color. // An optional second value for Box (Box[1]) controls if a shadow is displayed: // 'true' -> display a shadow in a default color // 'false' -> display no shadow (same as if no second value is given) // otherwise -> use given string as color string for the shadow color // //Prepare window 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)+"").TupleSplit("\n"); // //Estimate extentions of text depending on font size. 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 { //Transform image to window coordinates hv_FactorRow = (1.*hv_HeightWin)/((hv_Row2Part-hv_Row1Part)+1); hv_FactorColumn = (1.*hv_WidthWin)/((hv_Column2Part-hv_Column1Part)+1); hv_R1 = ((hv_Row-hv_Row1Part)+0.5)*hv_FactorRow; hv_C1 = ((hv_Column-hv_Column1Part)+0.5)*hv_FactorColumn; } // //Display text box depending on text size hv_UseShadow = 1; hv_ShadowColor = "gray"; if (0 != (HTuple(hv_Box[0])==HTuple("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); } //Calculate box extents 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; 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;}MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){ ui->setupUi(this);}MainWindow::~MainWindow(){ delete ui;}void MainWindow::on_pushButton_clicked(){ // Local iconic variables QString text; HObject ho_Image, ho_Rectangle1, ho_Rotated; HObject ho_DotImage, ho_ROI_0, ho_ImageePart, ho_Region; HObject ho_ConnectedRegions1, ho_SelectedRegions1, ho_RegionUnion; HObject ho_RegionClosing1, ho_RegionClosing2, ho_Rectangle; HObject ho_RegionClosing3, ho_RegionClosing4, ho_RegionDilation; HObject ho_ConnectedRegions, ho_SelectedRegions2, ho_Partitioned; HObject ho_FinalCharacters, ho_Char, ho_Character, ho_Characters; // Local control variables HTuple hv_WindowHandle, hv_Width, hv_Height; HTuple hv_Classes, hv_Row1, hv_Column1, hv_Row2, hv_Column2; HTuple hv_Px, hv_Py, hv_OrientationAngle, hv_HomMat2DIdentity; HTuple hv_HomMat2DRotate, hv_NumIntermediate, hv_i, hv_CharacterNames; HTuple hv_CharacterCount, hv_OCRHandle, hv_Class, hv_Row11; HTuple hv_Column21; // if (HDevWindowStack::IsOpen()) // CloseWindow(HDevWindowStack::Pop()); ReadImage(&ho_Image, "D:/1.bmp"); GetImageSize(ho_Image, &hv_Width, &hv_Height); Hlong lwindowID = (Hlong)this->winId(); SetWindowAttr("background_color","black"); OpenWindow(0,0,hv_Width/4,hv_Height/4,lwindowID,"","",&hv_WindowHandle); HDevWindowStack::Push(hv_WindowHandle); hv_Classes.Clear(); hv_Classes[0] = "2"; hv_Classes[1] = "0"; hv_Classes[2] = "1"; hv_Classes[3] = "6"; hv_Classes[4] = "0"; hv_Classes[5] = "6"; hv_Classes[6] = "1"; hv_Classes[7] = "6"; hv_Classes[8] = "6"; hv_Classes[9] = "F"; hv_Classes[10] = "1"; hv_Classes[11] = "6"; hv_Classes[12] = "C"; hv_Classes[13] = "1"; hv_Classes[14] = "8"; hv_Classes[15] = "3"; hv_Classes[16] = "8"; hv_Classes[17] = "C"; hv_Row1 = 260.5; hv_Column1 = 362.5; hv_Row2 = 605.5; hv_Column2 = 1532.5; hv_Px = hv_Column1+((hv_Column2-hv_Column1)/2); hv_Py = hv_Row1+((hv_Row2-hv_Row1)/2); GenRectangle1(&ho_Rectangle1, hv_Row1, hv_Column1, hv_Row2, hv_Column2); TextLineOrientation(ho_Rectangle1, ho_Image, 35, -0.523599, 0.523599, &hv_OrientationAngle); HomMat2dIdentity(&hv_HomMat2DIdentity); HomMat2dRotate(hv_HomMat2DIdentity, -hv_OrientationAngle, hv_Px, hv_Py, &hv_HomMat2DRotate); AffineTransImage(ho_Image, &ho_Rotated, hv_HomMat2DRotate, "constant", "false"); DotsImage(ho_Rotated, &ho_DotImage, 11, "dark", 2); GenRectangle1(&ho_ROI_0, 260.5, 362.5, 605.5, 1532.5); ReduceDomain(ho_DotImage, ho_ROI_0, &ho_ImageePart); Threshold(ho_ImageePart, &ho_Region, 75, 255); Connection(ho_Region, &ho_ConnectedRegions1); SelectShape(ho_ConnectedRegions1, &ho_SelectedRegions1, "area", "and", 10.55, 500); Union1(ho_SelectedRegions1, &ho_RegionUnion); ClosingRectangle1(ho_RegionUnion, &ho_RegionClosing1, 1, 10); ClosingRectangle1(ho_RegionClosing1, &ho_RegionClosing2, 10, 1); GenRectangle2(&ho_Rectangle, 16, 16, HTuple(45).TupleRad(), 6, 0); Closing(ho_RegionClosing2, ho_Rectangle, &ho_RegionClosing3); GenRectangle2(&ho_Rectangle, 2, 2, HTuple(145).TupleRad(), 8, 0); Closing(ho_RegionClosing3, ho_Rectangle, &ho_RegionClosing4); DilationCircle(ho_RegionClosing4, &ho_RegionDilation, 2); Connection(ho_RegionDilation, &ho_ConnectedRegions); SelectShape(ho_ConnectedRegions, &ho_SelectedRegions2, "area", "and", 527.52, 5000); PartitionDynamic(ho_SelectedRegions2, &ho_Partitioned, 60, 20); SortRegion(ho_Partitioned, &ho_FinalCharacters, "character", "true", "row"); CountObj(ho_FinalCharacters, &hv_NumIntermediate); if (HDevWindowStack::IsOpen()) DispObj(ho_Rotated, HDevWindowStack::GetActive()); { HTuple end_val41 = hv_NumIntermediate; HTuple step_val41 = 1; for (hv_i=1; hv_i.Continue(end_val41, step_val41); hv_i += step_val41) { if (HDevWindowStack::IsOpen()) SetColor(HDevWindowStack::GetActive(),"red"); SelectObj(ho_FinalCharacters, &ho_Char, hv_i); if (HDevWindowStack::IsOpen()) DispObj(ho_Char, HDevWindowStack::GetActive()); Intersection(ho_Char, ho_Region, &ho_Character); AppendOcrTrainf(ho_Character, ho_Rotated, HTuple(hv_Classes[hv_i-1]), "D:/svm.trf"); } } Intersection(ho_FinalCharacters, ho_Region, &ho_Characters); if (HDevWindowStack::IsOpen()) DispObj(ho_Rotated, HDevWindowStack::GetActive()); if (HDevWindowStack::IsOpen()) SetColored(HDevWindowStack::GetActive(),6); if (HDevWindowStack::IsOpen()) DispObj(ho_Characters, HDevWindowStack::GetActive()); ReadOcrTrainfNames("D:/svm.trf", &hv_CharacterNames, &hv_CharacterCount); //创建分裂器 CreateOcrClassSvm(8, 10, "constant", "default", hv_CharacterNames, "rbf", 0.02, 0.001, "one-versus-one", "normalization", 0, &hv_OCRHandle); TrainfOcrClassSvm(hv_OCRHandle, "D:/svm.trf", 0.001, "default"); DoOcrMultiClassSvm(ho_Characters, ho_Rotated, hv_OCRHandle, &hv_Class); SmallestRectangle1(ho_Characters, &hv_Row11, &hv_Column1, &hv_Row2, &hv_Column21); { HTuple end_val60 = hv_NumIntermediate; HTuple step_val60 = 1; for (hv_i=1; hv_i.Continue(end_val60, step_val60); hv_i += step_val60) { disp_message(3600, HTuple(hv_Class[hv_i-1]), "image", HTuple(hv_Row2[hv_i-1])+100, HTuple(hv_Column1[hv_i-1]), "red", "false"); QString str = (QString)hv_Class[hv_i - 1].S (); text.append (str); } } ui->lineEdit->setText (text); ClearOcrClassSvm(hv_OCRHandle); // stop(); only in hdevelop}

ui文件中添加一个按钮,一个lineedit就行了,编译运行。

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:我劝你谨慎使用Spring中的@Scheduled注解
下一篇:opencv之WarpPerspective透视变化
相关文章

 发表评论

暂时没有评论,来抢沙发吧~