一、读取单个图像(Halcon窗体嵌入Qt显示)
1.1 图像铺满整个窗口(支持中文路径)
//.h Hlong m_winID; //窗口ID HTuple m_windowHandle;//窗口句柄 HObject m_image; //窗体显示图像 HTuple m_imageWidth, m_imageHeight;
//初始化:窗口绑定Halcon句柄 m_winID = (Hlong)this->ui.ImageWidget->winId(); SetWindowAttr("background_color", "black");//设置窗口颜色 OpenWindow(0, 0, ui.ImageWidget->width(), ui.ImageWidget->height(), m_winID, "visible", "", &m_windowHandle);//打开窗口绑定halcon的句柄
#include< QFileDialog> #include "HalconCpp.h" using namespace HalconCpp; //获取图片所在地址 QString fileName = QFileDialog::getOpenFileName(this, tr("open image file"), "./", tr("Image files(*.bmp *.jpg *.pbm *.pgm *.png *.ppm *.xbm *.xpm);;All files (*.*)")); //图片打开失败报警 if (fileName.isEmpty()) { QMessageBox::information(NULL, QString::fromLocal8Bit("警告"), QString::fromLocal8Bit("图像打开失败")); return; } ReadImage(&m_image, HTuple(fileName.toLocal8Bit().toStdString().c_str()));//支持中文路径读取图片 GetImageSize(m_image, &m_imageWidth, &m_imageHeight); SetPart(m_windowHandle,0, 0, m_imageHeight.D(), m_imageWidth.D()); DispObj(m_image, m_windowHandle);
1.2 图像显示真实宽高比
1、Halcon读取相对路径下的图像
//.h Hlong m_winID; //窗口ID HTuple m_windowHandle;//窗口句柄 HObject m_image; //窗体显示图像 HTuple m_imageWidth, m_imageHeight;
//初始化:窗口绑定Halcon句柄 m_winID = (Hlong)this->ui.ImageWidget->winId(); SetWindowAttr("background_color", "black");//设置窗口颜色 OpenWindow(0, 0, ui.ImageWidget->width(), ui.ImageWidget->height(), m_winID, "visible", "", &m_windowHandle);//打开窗口绑定halcon的句柄
void FormScalingTest::dispImage(HObject _inImg,QWidget *_widget) { double widgetWidth = _widget->width(); double widgetHeight = _widget->height(); GetImageSize(_inImg, &m_imageWidth, &m_imageHeight); if (!(m_imageWidth.Length() > 0)) { return; } double ratioWidth = (1.0) * m_imageWidth.D() / widgetWidth; double ratioHeight = (1.0) * m_imageHeight.D() / widgetHeight; HTuple row1, column1, row2, column2; if (ratioWidth >= ratioHeight) { row1 = (-1.0) * (widgetHeight * ratioHeight - m_imageHeight) / 2; column1 = 0; row2 = row1 + widgetHeight * ratioWidth; column2 = column1 + widgetHeight * ratioWidth; } else { row1 = 0; column1 = (-1.0) * (widgetWidth * ratioHeight - m_imageWidth) / 2; row2 = row1 + widgetHeight * ratioHeight; column2 = column1 + widgetWidth * ratioHeight; } SetPart(m_windowHandle, row1, column1, row2, column2); DispObj(m_image, m_windowHandle); }
方式一:
#include< QFileDialog> #include "HalconCpp.h" using namespace HalconCpp; CFileDialog readfileDlg(TRUE,NULL,NULL,0,pcsun,this); if(IDOK ==readfileDlg.DoModal()) //当点击确定按钮时候,会记录文件路径 { path = readfileDlg.GetPathName(); } else { QMessageBox::information(NULL, QString::fromLocal8Bit("警告"), QString::fromLocal8Bit("图像打开失败")); return; } hv_FilePath[0] =(char *)path.GetBuffer(); //涉及到数据转换、 HalconCpp::HImage ho_Image; ReadImage(&ho_Image,hv_FilePath); //读取文件 dispImage(m_image, ui.ImageWidget);
方式二:
#include< QFileDialog> #include "HalconCpp.h" using namespace HalconCpp; QString fileName = QFileDialog::getOpenFileName(this, tr("Open File..."), "F:", tr("IMG(*png *jpg *bmp);;" "(*txt)")); HImage ho_Image; ReadImage(&ho_Image, fileName.toLocal8Bit().toStdString().c_str());//支持中文路径读取图片 dispImage(m_image, ui.ImageWidget);
二、读取单个图像(Qt窗体显示)
读取Halcon类型的图像显示在Qt上的窗体界面:
QImage img = ho_Image; if (img.isNull()) { ui->label_pic->setPixmap(QPixmap(":/res/icons/picture.png")); } else { ui->label_pic->setPixmap(QPixmap::fromImage(img).scaled(ui->label_pic->width(), \ ui->label_pic->height(), Qt::KeepAspectRatio)); }
三、读取文件夹图像(Halcon读取)
纯Halcon代码:
list_files ('E:/Test Image', ['files','follow_links'], ImageFiles) tuple_regexp_select (ImageFiles, ['\\.(tif|tiff|gif|bmp|jpg|jpeg|jp2|png|pcx|pgm|ppm|pbm|xwd|ima|hobj)$','ignore_case'], ImageFiles) for Index := 0 to |ImageFiles| - 1 by 1 read_image (Image, 'E:/Test Image/'+ Index + '.bmp') rgb1_to_gray (Image, GrayImage)
导出成C++代码:
HTuple hv_ImageFiles; HObject ho_Image; ListFiles("E://Test Image", (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); for (int Index = 1; Index <= hv_ImageFiles.TupleLength(); Index++) { ReadImage(&ho_Image, QString("E://Test Image//" + QString::number(Index) + ".bmp").toStdString().c_str()); }
戳戳小手帮忙点个免费的赞和关注吧,嘿嘿。 |