Qt 联合Halcon视觉框架(1)
匹配类
# if QT_VERSION >= 0x050000
# include <QtWidgets/qlabel.h>
# include <QtWidgets/qpushbutton.h>
# else
# include <qlabel.h>
# include <qpushbutton.h>
# endif
# include <QScopedPointer>
# include "qhalconwindow.h"
class Matching : public QWidget
{
Q_OBJECT
public :
Matching ( QWidget * parent = nullptr ) ;
virtual ~ Matching ( void ) ;
void InitFg ( void ) ;
protected slots:
void Create ( void ) ;
void Start ( void ) ;
void Stop ( void ) ;
protected :
void timerEvent ( QTimerEvent* ) ;
void StartMatching ( void ) ;
private :
Hlong ImageWidth, ImageHeight;
HalconCpp:: HTuple FGHandle;
HalconCpp:: HImage Image;
QScopedPointer< HalconCpp:: HShapeModel> ShapeModel;
HalconCpp:: HTuple ModelRow, ModelColumn;
HalconCpp:: HTuple Rect1Row, Rect1Col, Rect2Row, Rect2Col;
HalconCpp:: HTuple RectLength1, RectLength2;
HalconCpp:: HObject ShapeModelRegion;
QLabel * MatchTimeLabel;
QLabel * MatchScoreLabel;
QLabel * MeasTimeLabel;
QLabel * NumLeadsLabel;
QLabel * MinLeadsDistLabel;
QPushButton * CreateButton;
QPushButton * StartButton;
QPushButton * StopButton;
QHalconWindow * Disp;
long Timer;
} ;
构造函数初始化布局变量
Matching :: Matching ( QWidget * parent)
: QWidget ( parent) , Timer ( - 1 )
{
CreateButton = new QPushButton ( tr ( "Create Model" ) , this ) ;
connect ( CreateButton, SIGNAL ( clicked ( ) ) , SLOT ( Create ( ) ) ) ;
StartButton = new QPushButton ( tr ( "Start" ) , this ) ;
StartButton-> setEnabled ( false ) ;
connect ( StartButton, SIGNAL ( clicked ( ) ) , SLOT ( Start ( ) ) ) ;
StopButton = new QPushButton ( tr ( "Stop" ) , this ) ;
StopButton-> setEnabled ( false ) ;
connect ( StopButton, SIGNAL ( clicked ( ) ) , SLOT ( Stop ( ) ) ) ;
QLabel * match_time = new QLabel ( tr ( "Matching:" ) , this ) ;
QLabel * match_time2 = new QLabel ( tr ( "Time:" ) , this ) ;
MatchTimeLabel = new QLabel ( " " , this ) ;
QLabel * match_score = new QLabel ( tr ( "Score: " ) , this ) ;
MatchScoreLabel = new QLabel ( " " , this ) ;
QLabel * meas_time = new QLabel ( tr ( "Measure:" ) , this ) ;
QLabel * meas_time2 = new QLabel ( tr ( "Time:" ) , this ) ;
MeasTimeLabel = new QLabel ( " " , this ) ;
QLabel * num_leads = new QLabel ( tr ( "Number of leads: " ) , this ) ;
NumLeadsLabel = new QLabel ( " " , this ) ;
QLabel * min_lead_dist = new QLabel ( tr ( "Minimum Lead Distance: " ) , this ) ;
MinLeadsDistLabel = new QLabel ( " " , this ) ;
QLabel * MvtecLabel = new QLabel ( "\xa9 2004-2017 MVTec Software GmbH" , this ) ;
MvtecLabel-> setFont ( QFont ( nullptr , 10 , QFont:: Bold) ) ;
QLabel * DispHintLabel = new QLabel (
"Zoom: mouse wheel; Move: left mouse button; Reset: double click" , this ) ;
QVBoxLayout * TopBox = new QVBoxLayout ( this ) ;
QHBoxLayout * Mvtec = new QHBoxLayout;
Mvtec-> addStretch ( 1 ) ;
Mvtec-> addWidget ( MvtecLabel) ;
Mvtec-> addStretch ( 1 ) ;
QVBoxLayout * TopVBox = new QVBoxLayout;
QHBoxLayout * HBoxDispAndButtons = new QHBoxLayout;
Disp = new QHalconWindow ( this ) ;
Disp-> setMinimumSize ( 50 , 50 ) ;
QVBoxLayout * DispVBox = new QVBoxLayout;
DispVBox-> addWidget ( Disp, 1 ) ;
DispVBox-> addSpacing ( 8 ) ;
DispVBox-> addWidget ( DispHintLabel) ;
QVBoxLayout * Buttons = new QVBoxLayout;
Buttons-> addWidget ( CreateButton) ;
Buttons-> addSpacing ( 8 ) ;
Buttons-> addWidget ( StartButton) ;
Buttons-> addSpacing ( 8 ) ;
Buttons-> addWidget ( StopButton) ;
Buttons-> addStretch ( 1 ) ;
HBoxDispAndButtons-> addSpacing ( 15 ) ;
HBoxDispAndButtons-> addLayout ( DispVBox, 1 ) ;
HBoxDispAndButtons-> addSpacing ( 15 ) ;
HBoxDispAndButtons-> addLayout ( Buttons) ;
HBoxDispAndButtons-> addSpacing ( 15 ) ;
QHBoxLayout * HBoxLabels = new QHBoxLayout;
QGridLayout * Labels = new QGridLayout ( ) ;
Labels-> addWidget ( match_time, 0 , 0 ) ;
Labels-> addWidget ( match_time2, 0 , 1 ) ;
Labels-> addWidget ( MatchTimeLabel, 0 , 2 ) ;
Labels-> addWidget ( match_score, 0 , 3 ) ;
Labels-> addWidget ( MatchScoreLabel, 0 , 4 ) ;
Labels-> addWidget ( meas_time, 1 , 0 ) ;
Labels-> addWidget ( meas_time2, 1 , 1 ) ;
Labels-> addWidget ( MeasTimeLabel, 1 , 2 ) ;
Labels-> addWidget ( num_leads, 1 , 3 ) ;
Labels-> addWidget ( NumLeadsLabel, 1 , 4 ) ;
Labels-> addWidget ( min_lead_dist, 1 , 5 ) ;
Labels-> addWidget ( MinLeadsDistLabel, 1 , 6 ) ;
HBoxLabels-> addSpacing ( 15 ) ;
HBoxLabels-> addLayout ( Labels) ;
HBoxLabels-> addSpacing ( 130 ) ;
TopVBox-> addLayout ( HBoxDispAndButtons) ;
TopVBox-> addSpacing ( 15 ) ;
TopVBox-> addLayout ( HBoxLabels) ;
TopBox-> addSpacing ( 15 ) ;
TopBox-> addLayout ( TopVBox) ;
TopBox-> addSpacing ( 15 ) ;
TopBox-> addLayout ( Mvtec) ;
TopBox-> addSpacing ( 10 ) ;
}
初始化图片
void Matching :: InitFg ( void )
{
using namespace HalconCpp;
OpenFramegrabber ( "File" , 1 , 1 , 0 , 0 , 0 , 0 , "default" , - 1 , "default" , - 1 , "default" ,
"board/board.seq" , "default" , - 1 , 1 , & FGHandle) ;
GrabImage ( & Image, FGHandle) ;
Image. GetImageSize ( & ImageWidth, & ImageHeight) ;
Disp-> GetHalconBuffer ( ) -> SetPart ( 0 , 0 , ImageHeight - 1 , ImageWidth - 1 ) ;
Disp-> GetHalconBuffer ( ) -> SetLineWidth ( 3 ) ;
Disp-> GetHalconBuffer ( ) -> DispObj ( Image) ;
Disp-> GetHalconBuffer ( ) -> FlushBuffer ( ) ;
}
匹配
void Matching :: StartMatching ( void )
{
using namespace HalconCpp;
double S1, S2;
HTuple Rect1RowCheck, Rect1ColCheck, Rect2RowCheck, Rect2ColCheck;
HTuple MeasureHandle1, MeasureHandle2, NumLeads;
HTuple RowCheck, ColumnCheck, AngleCheck, Score, HomMat2D, MinDistance;
HTuple RowEdgeFirst1, ColumnEdgeFirst1, AmplitudeFirst1;
HTuple RowEdgeSecond1, ColumnEdgeSecond1, AmplitudeSecond1;
HTuple IntraDistance1, InterDistance1;
HTuple RowEdgeFirst2, ColumnEdgeFirst2, AmplitudeFirst2;
HTuple RowEdgeSecond2, ColumnEdgeSecond2, AmplitudeSecond2;
HTuple IntraDistance2, InterDistance2;
HObject ShapeModelTrans;
HObject Rectangle1, Rectangle2;
HImage Image;
char buf[ MAX_STRING] ;
QString string;
GrabImage ( & Image, FGHandle) ;
Disp-> GetHalconBuffer ( ) -> DispObj ( Image) ;
S1 = HSystem :: CountSeconds ( ) ;
ShapeModel-> FindShapeModel ( Image, 0 , 2 * PI, 0.7 , 1 , 0.5 , "least_squares" , 4 , 0.7 ,
& RowCheck, & ColumnCheck, & AngleCheck, & Score) ;
S2 = HSystem :: CountSeconds ( ) ;
sprintf ( buf, "%5.2f" , ( S2 - S1) * 1000 ) ;
string = buf;
MatchTimeLabel-> setText ( string) ;
if ( Score. Length ( ) == 1 )
{
sprintf ( buf, "%7.5f" , ( double ) Score[ 0 ] ) ;
string = buf;
MatchScoreLabel-> setText ( string) ;
VectorAngleToRigid ( ModelRow, ModelColumn, 0 , RowCheck, ColumnCheck, AngleCheck, & HomMat2D) ;
AffineTransRegion ( ShapeModelRegion, & ShapeModelTrans, HomMat2D, "false" ) ;
Disp-> GetHalconBuffer ( ) -> SetColor ( "green" ) ;
Disp-> GetHalconBuffer ( ) -> DispObj ( ShapeModelTrans) ;
AffineTransPixel ( HomMat2D, Rect1Row, Rect1Col, & Rect1RowCheck, & Rect1ColCheck) ;
AffineTransPixel ( HomMat2D, Rect2Row, Rect2Col, & Rect2RowCheck, & Rect2ColCheck) ;
GenRectangle2 ( & Rectangle1, Rect1RowCheck, Rect1ColCheck, AngleCheck, RectLength1, RectLength2) ;
GenRectangle2 ( & Rectangle2, Rect2RowCheck, Rect2ColCheck, AngleCheck, RectLength1, RectLength2) ;
Disp-> GetHalconBuffer ( ) -> SetColor ( "blue" ) ;
Disp-> GetHalconBuffer ( ) -> SetDraw ( "margin" ) ;
Disp-> GetHalconBuffer ( ) -> DispObj ( Rectangle1) ;
Disp-> GetHalconBuffer ( ) -> DispObj ( Rectangle2) ;
Disp-> GetHalconBuffer ( ) -> SetDraw ( "fill" ) ;
S1 = HSystem :: CountSeconds ( ) ;
GenMeasureRectangle2 ( Rect1RowCheck, Rect1ColCheck, AngleCheck, RectLength1, RectLength2,
ImageWidth, ImageHeight, "bilinear" , & MeasureHandle1) ;
GenMeasureRectangle2 ( Rect2RowCheck, Rect2ColCheck, AngleCheck, RectLength1, RectLength2,
ImageWidth, ImageHeight, "bilinear" , & MeasureHandle2) ;
MeasurePairs ( Image, MeasureHandle1, 2 , 90 , "positive" , "all" ,
& RowEdgeFirst1, & ColumnEdgeFirst1, & AmplitudeFirst1,
& RowEdgeSecond1, & ColumnEdgeSecond1, & AmplitudeSecond1,
& IntraDistance1, & InterDistance1) ;
MeasurePairs ( Image, MeasureHandle2, 2 , 90 , "positive" , "all" ,
& RowEdgeFirst2, & ColumnEdgeFirst2, & AmplitudeFirst2,
& RowEdgeSecond2, & ColumnEdgeSecond2, & AmplitudeSecond2,
& IntraDistance2, & InterDistance2) ;
CloseMeasure ( MeasureHandle1) ;
CloseMeasure ( MeasureHandle2) ;
S2 = HSystem :: CountSeconds ( ) ;
Disp-> GetHalconBuffer ( ) -> SetColor ( "red" ) ;
Disp-> GetHalconBuffer ( ) -> DispLine (
RowEdgeFirst1 - RectLength2 * AngleCheck. TupleCos ( ) ,
ColumnEdgeFirst1 - RectLength2 * AngleCheck. TupleSin ( ) ,
RowEdgeFirst1 + RectLength2 * AngleCheck. TupleCos ( ) ,
ColumnEdgeFirst1 + RectLength2 * AngleCheck. TupleSin ( ) ) ;
Disp-> GetHalconBuffer ( ) -> DispLine (
RowEdgeSecond1 - RectLength2 * AngleCheck. TupleCos ( ) ,
ColumnEdgeSecond1 - RectLength2 * AngleCheck. TupleSin ( ) ,
RowEdgeSecond1 + RectLength2 * AngleCheck. TupleCos ( ) ,
ColumnEdgeSecond1 + RectLength2 * AngleCheck. TupleSin ( ) ) ;
Disp-> GetHalconBuffer ( ) -> DispLine (
RowEdgeFirst2 - RectLength2 * AngleCheck. TupleCos ( ) ,
ColumnEdgeFirst2 - RectLength2 * AngleCheck. TupleSin ( ) ,
RowEdgeFirst2 + RectLength2 * AngleCheck. TupleCos ( ) ,
ColumnEdgeFirst2 + RectLength2 * AngleCheck. TupleSin ( ) ) ;
Disp-> GetHalconBuffer ( ) -> DispLine (
RowEdgeSecond2 - RectLength2 * AngleCheck. TupleCos ( ) ,
ColumnEdgeSecond2 - RectLength2 * AngleCheck. TupleSin ( ) ,
RowEdgeSecond2 + RectLength2 * AngleCheck. TupleCos ( ) ,
ColumnEdgeSecond2 + RectLength2 * AngleCheck. TupleSin ( ) ) ;
sprintf ( buf, "%5.2f" , ( S2 - S1) * 1000 ) ;
string = buf;
MeasTimeLabel-> setText ( string) ;
NumLeads = IntraDistance1. Length ( ) + IntraDistance2. Length ( ) ;
sprintf ( buf, "%2ld" , ( Hlong) NumLeads[ 0 ] ) ;
string = buf;
NumLeadsLabel-> setText ( string) ;
MinDistance = ( InterDistance1. TupleConcat ( InterDistance2) ) . TupleMin ( ) ;
sprintf ( buf, "%6.3f" , ( double ) MinDistance[ 0 ] ) ;
string = buf;
MinLeadsDistLabel-> setText ( string) ;
}
Disp-> GetHalconBuffer ( ) -> FlushBuffer ( ) ;
}
匹配计时器事件
void Matching :: timerEvent ( QTimerEvent* )
{
StartMatching ( ) ;
}
开启计时器
void Matching :: Start ( void )
{
StartButton-> setEnabled ( false ) ;
StopButton-> setEnabled ( true ) ;
Timer = startTimer ( 20 ) ;
}
停止
void Matching :: Stop ( void )
{
StartButton-> setEnabled ( true ) ;
StopButton-> setEnabled ( false ) ;
if ( Timer != - 1 )
{
killTimer ( Timer) ;
Timer = - 1 ;
}
}
析构函数
Matching :: ~ Matching ( void )
{
using namespace HalconCpp;
CloseFramegrabber ( FGHandle) ;
if ( Timer != - 1 )
{
killTimer ( Timer) ;
Timer = - 1 ;
}
}