c
复制代码
#include "CHalconLabel.h"
CHalconLabel::CHalconLabel(QWidget* parent)
: QLabel(parent)
, m_bIsMove(false)
, m_bIsDrawROI(false)
{
//初始化图像
GenEmptyObj(&hCurrentImage);
//初始化ROI区域
GenEmptyObj(&m_drawnRegion);
//设置文本位置---居中且上方,设置文本颜色
this->setAlignment(Qt::AlignTop | Qt::AlignHCenter);
this->setStyleSheet("color: red;");
}
CHalconLabel::~CHalconLabel()
{
}
void CHalconLabel::resizeEvent(QResizeEvent * ev)
{
if (m_hHalconID != NULL)
{
//防止窗口闪烁
SetSystem("flush_graphic", "false");
//重新显示
//显示二,维持原有图像比例且居中显示
ClearWindow(m_hHalconID);
DetachBackgroundFromWindow(m_hHalconID);
int labelWidth = this->width(); //窗口尺寸
int labelHeight = this->height();
HTuple imgWidth, imgHeight; //原图尺寸
HTuple m_scaledWidth, m_scaledHeight; //缩放后的尺寸
HTuple m_hvScaledRate; //缩放比例
GetImageSize(hCurrentImage, &imgWidth, &imgHeight);
//获取缩放系数
TupleMin2(1.0 * labelWidth / imgWidth, 1.0 * labelHeight / imgHeight, &m_hvScaledRate);
//进行图像缩放
ZoomImageFactor(hCurrentImage, &ho_ImageZoom, m_hvScaledRate, m_hvScaledRate, "constant");
GetImageSize(ho_ImageZoom, &m_scaledWidth, &m_scaledHeight);
if (1.0 * labelWidth / imgWidth < 1.0 * labelHeight / imgHeight)
{
SetWindowExtents(m_hHalconID, labelHeight / 2.0 - m_scaledHeight / 2.0, 0, labelWidth, m_scaledHeight);
}
else
{
SetWindowExtents(m_hHalconID, 0, labelWidth / 2.0 - m_scaledWidth / 2.0, m_scaledWidth, labelHeight);
}
SetPart(m_hHalconID, 0, 0, imgHeight - 1, imgWidth - 1);
AttachBackgroundToWindow(hCurrentImage, m_hHalconID);
SetSystem("flush_graphic", "true");
DetachBackgroundFromWindow(m_hHalconID);
AttachBackgroundToWindow(hCurrentImage, m_hHalconID);
DispObj(hCurrentImage, m_hHalconID);
AttachBackgroundToWindow(hCurrentImage, m_hHalconID);
DispObj(m_drawnRegion, m_hHalconID); // 添加此行以重新显示ROI
}
}
void CHalconLabel::wheelEvent(QWheelEvent* ev)
{
double Zoom; //放大或缩小倍率
HTuple mouseRow, mouseCol, Button;
HTuple startRowBf, startColBf, endRowBf, endColBf, Ht, Wt, startRowAft, startColAft, endRowAft, endColAft;
//滚轮前滑,放大
if (ev->delta() > 0)
{
Zoom = 2.0;//单步放大倍率
}
else//否则缩小
{
Zoom = 1 / 2.0;
}
//获取光标在原图上的位置,注意是原图坐标,不是Label下的坐标
HTuple hv_Exception, hv_ErrMsg;
try
{
GetMposition(m_hHalconID, &mouseRow, &mouseCol, &Button);
}
catch (HException& HDevExpDefaultException)
{
return;
}
//获取原图显示的部分,注意也是原图坐标
GetPart(m_hHalconID, &startRowBf, &startColBf, &endRowBf, &endColBf);
//缩放前显示的图像宽高
Ht = endRowBf - startRowBf;
Wt = endColBf - startColBf;
//普通版halcon能处理的图像最大尺寸是32K*32K。如果无限缩小原图像,导致显示的图像超出限制,则会造成程序崩溃
if (Ht * Wt < 20000 * 20000 || Zoom == 2.0)
{
//计算缩放后的图像区域
startRowAft = mouseRow - ((mouseRow - startRowBf) / Zoom);
startColAft = mouseCol - ((mouseCol - startColBf) / Zoom);
endRowAft = startRowAft + (Ht / Zoom);
endColAft = startColAft + (Wt / Zoom);
//如果放大过大,则返回
if (endRowAft - startRowAft < 2)
{
return;
}
if (m_hHalconID != NULL)
{
//如果有图像,则先清空图像
DetachBackgroundFromWindow(m_hHalconID);
}
SetPart(m_hHalconID, startRowAft, startColAft, endRowAft, endColAft);
AttachBackgroundToWindow(hCurrentImage, m_hHalconID);
}
AttachBackgroundToWindow(hCurrentImage, m_hHalconID);
DispObj(m_drawnRegion, m_hHalconID); // 添加此行以重新显示ROI
}
void CHalconLabel::mousePressEvent(QMouseEvent* ev)
{
HTuple mouseRow, mouseCol, Button;
try
{
GetMposition(m_hHalconID, &mouseRow, &mouseCol, &Button);
}
catch (HException)
{
return;
}
//鼠标按下时的行列坐标
m_tMouseDownRow = mouseRow;
m_tMouseDownCol = mouseCol;
m_bIsMove = true;
}
void CHalconLabel::mouseReleaseEvent(QMouseEvent* ev)
{
m_bIsMove = false;
}
void CHalconLabel::mouseMoveEvent(QMouseEvent* ev)
{
//情况一:鼠标绘制ROI区域时,不响应鼠标移动事件
if (m_bIsDrawROI) {
this->setCursor(Qt::ArrowCursor); //指针设置为普通类型
return;
}
//情况二:鼠标按下并移动时,只移动图像
HTuple startRowBf, startColBf, endRowBf, endColBf, mouseRow, mouseCol, Button;
try
{
SetCheck("~give_error"); //不要报错
GetMposition(m_hHalconID, &mouseRow, &mouseCol, &Button);
if (mouseCol.Length() <= 0 || mouseRow.Length() < 0)
{
return;
}
SetCheck("give_error");
//在绘图显示界面上显示坐标
//this->setText(QString("X坐标:%1 Y坐标:%2 ").arg(mouseCol[0].D()).arg(mouseRow[0].D()));
}
catch (HException)
{
return;
}
if (m_bIsMove)
{
this->setCursor(Qt::PointingHandCursor); //设置鼠标样式为手型指针
//计算移动值
double RowMove = mouseRow[0].D() - m_tMouseDownRow[0].D();
double ColMove = mouseCol[0].D() - m_tMouseDownCol[0].D();
//得到当前的窗口坐标
GetPart(m_hHalconID, &startRowBf, &startColBf, &endRowBf, &endColBf);
//移动图像
if (m_hHalconID != NULL)
{
//如果有图像,则先清空图像
DetachBackgroundFromWindow(m_hHalconID);
}
SetPart(m_hHalconID, startRowBf - RowMove, startColBf - ColMove, endRowBf - RowMove, endColBf - ColMove);
SetCheck("~give_error");
AttachBackgroundToWindow(hCurrentImage, m_hHalconID);
//当光标不在Halcon窗口内时返回,否则会报错
SetCheck("give_error");
}
//情况三:鼠标未按下移动时,实时获取当前图像坐标
else {
this->setCursor(Qt::ArrowCursor); //指针设置为普通类型
HTuple pointGray;
try
{
SetCheck("~give_error"); //不要报错
GetGrayval(hCurrentImage, mouseRow, mouseCol, &pointGray);
//当光标不在Halcon窗口内时返回,否则会报错
if (mouseCol.Length() <= 0 || pointGray[0].D() < 0)
{
return;
}
SetCheck("give_error"); //不要报错
}
catch (HException)
{
// 设置文本的颜色为红色
// this->setText(QString("X坐标:- Y坐标:- 灰度值:-"));
return;
}
//在绘图显示界面上显示坐标
//this->setText(QString("X坐标:%1 Y坐标:%2 灰度值:%3").arg(mouseCol[0].D()).arg(mouseRow[0].D()).arg(pointGray[0].D()));
}
AttachBackgroundToWindow(hCurrentImage, m_hHalconID);
DispObj(m_drawnRegion, m_hHalconID); // 添加此行以重新显示ROI
}
void CHalconLabel::DrawCircles()
{
//绘制的过程中,不能鼠标移动图像,不响应鼠标移动事件
m_bIsDrawROI = true;
HObject currentRegion;
HTuple Row, Column, Radius;
DrawCircle(m_hHalconID, &Row, &Column, &Radius);
GenCircle(¤tRegion, Row, Column, Radius);
Union2(m_drawnRegion, currentRegion, &m_drawnRegion);
//设置ROI边缘线
SetColor(m_hHalconID, "red");
SetDraw(m_hHalconID, "margin");
SetLineWidth(m_hHalconID, 2);
DispObj(m_drawnRegion, m_hHalconID);
//绘制结束以后,实时获取当前图像的像素坐标
m_bIsDrawROI = false;
}
void CHalconLabel::DrawRectangles()
{
m_bIsDrawROI = true;
HObject currentRegion;
HTuple R1, C1, R2, C2;
DrawRectangle1(m_hHalconID, &R1, &C1, &R2, &C2);
GenRectangle1(¤tRegion, R1, C1, R2, C2);
Union2(m_drawnRegion, currentRegion, &m_drawnRegion);
//设置ROI边缘线
SetColor(m_hHalconID, "red");
SetDraw(m_hHalconID, "margin");
SetLineWidth(m_hHalconID, 2);
DispObj(m_drawnRegion, m_hHalconID);
m_bIsDrawROI = false;
}
void CHalconLabel::DrawRotateRectangles()
{
m_bIsDrawROI = true;
HObject currentRegion;
HTuple Row, Column, Phi, Length1, Length2;
DrawRectangle2(m_hHalconID, &Row, &Column, &Phi, &Length1, &Length2);
GenRectangle2(¤tRegion, Row, Column, Phi, Length1, Length2);
Union2(m_drawnRegion, currentRegion, &m_drawnRegion);
//设置ROI边缘线
SetColor(m_hHalconID, "red");
SetDraw(m_hHalconID, "margin");
SetLineWidth(m_hHalconID, 2);
DispObj(m_drawnRegion, m_hHalconID);
m_bIsDrawROI = false;
}
void CHalconLabel::DrawEllipses()
{
m_bIsDrawROI = true;
HObject currentRegion;
HTuple Row, Column, Phi, Radius1, Radius2;
DrawEllipse(m_hHalconID, &Row, &Column, &Phi, &Radius1, &Radius2);
GenEllipse(¤tRegion, Row, Column, Phi, Radius1, Radius2);
Union2(m_drawnRegion, currentRegion, &m_drawnRegion);
//设置ROI边缘线
SetColor(m_hHalconID, "red");
SetDraw(m_hHalconID, "margin");
SetLineWidth(m_hHalconID, 2);
DispObj(m_drawnRegion, m_hHalconID);
m_bIsDrawROI = false;
}
void CHalconLabel::ClearROI()
{
//直接清空
GenEmptyRegion(&m_drawnRegion);
}
void CHalconLabel::DispalyImageROI()
{
DisplayImage(hCurrentImage);
HObject emptyRegion;
HTuple isEqual;
GenEmptyRegion(&emptyRegion);
TestEqualRegion(emptyRegion, m_drawnRegion, &isEqual);
if (isEqual == 0) {
DispObj(m_drawnRegion, m_hHalconID);
}
}
void CHalconLabel::DisplayRegion()
{
HObject image;
HObject emptyRegion;
HTuple isEqual;
GenEmptyRegion(&emptyRegion);
TestEqualRegion(emptyRegion, m_drawnRegion, &isEqual);
if (isEqual != 0) {
return;
}
ReduceDomain(hCurrentImage, m_drawnRegion, &image);
ClearWindow(m_hHalconID);
DisplayImage(image);
}
HObject CHalconLabel::GetRegion()
{
return m_drawnRegion;
}
void CHalconLabel::SetID()
{
if (m_hHalconID == NULL) {
SetWindowAttr("background_color", "black"); //设置背景色
m_hLabelID = (Hlong)this->winId();
OpenWindow(0, 0, this->width(), this->height(), m_hLabelID, "visible", "", &m_hHalconID);
}
}
void CHalconLabel::SetPixelTracke(bool ret)
{
//设置鼠标追踪,可以实时响应鼠标移动事件
this->setMouseTracking(ret);
}
void CHalconLabel::DisplayImage(HObject hDisplayImage)
{
//隔离数据
CopyImage(hDisplayImage, &hCurrentImage);
//显示二,维持原有图像比例且居中显示
ClearWindow(m_hHalconID);
DetachBackgroundFromWindow(m_hHalconID);
int labelWidth = this->width(); //窗口尺寸
int labelHeight = this->height();
HTuple imgWidth, imgHeight; //原图尺寸
HTuple m_scaledWidth, m_scaledHeight; //缩放后的尺寸
HTuple m_hvScaledRate; //缩放比例
GetImageSize(hCurrentImage, &imgWidth, &imgHeight);
//获取缩放系数
TupleMin2(1.0 * labelWidth / imgWidth, 1.0 * labelHeight / imgHeight, &m_hvScaledRate);
//进行图像缩放
ZoomImageFactor(hCurrentImage, &ho_ImageZoom, m_hvScaledRate, m_hvScaledRate, "constant");
GetImageSize(ho_ImageZoom, &m_scaledWidth, &m_scaledHeight);
if (1.0 * labelWidth / imgWidth < 1.0 * labelHeight / imgHeight)
{
SetWindowExtents(m_hHalconID, labelHeight / 2.0 - m_scaledHeight / 2.0, 0, labelWidth, m_scaledHeight);
}
else
{
SetWindowExtents(m_hHalconID, 0, labelWidth / 2.0 - m_scaledWidth / 2.0, m_scaledWidth, labelHeight);
}
SetPart(m_hHalconID, 0, 0, imgHeight - 1, imgWidth - 1);
AttachBackgroundToWindow(hCurrentImage, m_hHalconID);
}
void CHalconLabel::ResetDisplayImage()
{
ClearWindow(m_hHalconID);
DetachBackgroundFromWindow(m_hHalconID);
int labelWidth = this->width(); //窗口尺寸
int labelHeight = this->height();
HTuple imgWidth, imgHeight; //原图尺寸
HTuple m_scaledWidth, m_scaledHeight; //缩放后的尺寸
HTuple m_hvScaledRate; //缩放比例
GetImageSize(hCurrentImage, &imgWidth, &imgHeight);
//获取缩放系数
TupleMin2(1.0 * labelWidth / imgWidth, 1.0 * labelHeight / imgHeight, &m_hvScaledRate);
//进行图像缩放
ZoomImageFactor(hCurrentImage, &ho_ImageZoom, m_hvScaledRate, m_hvScaledRate, "constant");
GetImageSize(ho_ImageZoom, &m_scaledWidth, &m_scaledHeight);
if (1.0 * labelWidth / imgWidth < 1.0 * labelHeight / imgHeight)
{
SetWindowExtents(m_hHalconID, labelHeight / 2.0 - m_scaledHeight / 2.0, 0, labelWidth, m_scaledHeight);
}
else
{
SetWindowExtents(m_hHalconID, 0, labelWidth / 2.0 - m_scaledWidth / 2.0, m_scaledWidth, labelHeight);
}
SetPart(m_hHalconID, 0, 0, imgHeight - 1, imgWidth - 1);
AttachBackgroundToWindow(hCurrentImage, m_hHalconID);
}