1、简介
halcon是一款和图像处理库,里面的算法很多,效率很高。由于项目中要识别二维码,然后选择了halcon库,因为halcon算法丰富,开发起来方便省时。
2、halcon识别二维码用到的类。
(1)、HImage类:halcon的图像数据存储类。
(2)、HDataCode2D类:halcon识别二维码的算法类。
(3)、HTuple类:halcon存储基本数据的类。类似C++的int。
(4)、HRegion类:halcon用来表示区域的类,类似QT的Item类。
3、源码实现halcon识别二维码。
(1)、MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <qgraphicsscene.h>
#include <qmath.h>
#include "HalconCpp.h"
#include "HDevThread.h"
#include <iostream>
using namespace std;
using namespace HalconCpp;
typedef HalconCpp::HObject HObject;
typedef HalconCpp::HImage HImage;
typedef HalconCpp::HRegion HRegion;
typedef HalconCpp::HXLD HXLD;
typedef HalconCpp::HXLDCont HXLDCont;
typedef HalconCpp::HHomMat2D HHomMat2D;
#define M_180_PI (57.2957796)
#define M_PI_180 (0.01745329)
struct QRotatedRect
{
float cx;
float cy;
float w;
float h;
float angle;
QRotatedRect(float cx = 0, float cy = 0, float w = 0, float h = 0, float angle = 0)
{
this->cx = cx;
this->cy = cy;
this->w = w;
this->h = h;
this->angle = angle;
}
QRotatedRect(const QPointF& center, const QSizeF& size, float angle)
{
this->cx = center.x();
this->cy = center.y();
this->w = size.width();
this->h = size.height();
this->angle = angle;
}
void points(QPointF pts[]) const
{
float fAngle = angle * M_PI_180;
float a = (float)sin(fAngle) * 0.5f;
float b = (float)cos(fAngle) * 0.5f;
pts[0].setX(cx - a * h - b * w);
pts[0].setY(cy + b * h - a * w);
pts[1].setX(cx + a * h - b * w);
pts[1].setY(cy - b * h - a * w);
pts[2].setX(2 * cx - pts[0].x());
pts[2].setY(2 * cy - pts[0].y());
pts[3].setX(2 * cx - pts[1].x());
pts[3].setY(2 * cy - pts[1].y());
}
QRectF boundingRect() const
{
QPointF pts[4];
this->points(pts);
QRectF rt(qFloor(std::min(std::min(std::min(pts[0].x(), pts[1].x()), pts[2].x()), pts[3].x())),
qFloor(std::min(std::min(std::min(pts[0].y(), pts[1].y()), pts[2].y()), pts[3].y())),
qCeil (std::max(std::max(std::max(pts[0].x(), pts[1].x()), pts[2].x()), pts[3].x())),
qCeil (std::max(std::max(std::max(pts[0].y(), pts[1].y()), pts[2].y()),