TESTHALCON.h
#pragma once
#include <QtWidgets/QWidget>
#include <QImage>
#include <QObject>
#include "..\TEST-HALCON\halcon12\x64\include\halconcpp\HalconCpp.h"
using namespace HalconCpp;
/****************************OPOENCV2.4.9******************************/
#include "..\TEST-HALCON\opencvlib249\x64\include\opencv2\opencv.hpp"
using namespace cv;
/***************************************/
class TESTHALCON : public QWidget
{
Q_OBJECT
public:
TESTHALCON(QWidget *parent = Q_NULLPTR);
static TESTHALCON* GetInstance();
private:
bool TESTHALCON::MatToQImage(cv::Mat& mat, QImage &_image);
bool QImageToMat(const QImage &srcImage, cv::Mat *dstImage);
bool HObject2Mat(HalconCpp::HObject & srcImage, cv::Mat * dstImage);
//Halcon中的HObject类型转QImage类型
bool HObject2QImage(const HalconCpp::HObject *src, QImage &dst/*, QVector<uint> g_Color_table*/);
bool CvMat2HObject(cv::Mat & src, HalconCpp::HObject * HObj);
bool QImage2Mat(QImage image, cv::Mat &mat);
//QImage类型转Halcon中的HObject类型
int QImageToHOjbect(QImage &image, HObject *Object);
//Halcon中的HObject类型转OpenCV中的cv::Mat类型
static bool HObject2Mat(const HalconCpp::HObject &Hobj, cv::Mat &Image);
HObject QImageToHObject(QImage image); //QImage彩色转Halcon彩色
bool HObject2QImage(const HalconCpp::HObject &Hobj, QImage &Image);
void HObjectToQImage(HObject himage, QImage **qimage);
void loadConfig(QString path);
void drawPoint(QStringList path);
protected:
static TESTHALCON* m_pInstance;
private:
Ui::TESTHALCONClass ui;
};
TESTHALCON.cpp
#include "TESTHALCON.h"
#include <vector>
#include <qDebug>
#include "..\TEST-HALCON\halcon12\x64\include\halconcpp\HalconCpp.h"
using namespace HalconCpp;
/****************************OPOENCV2.4.9***************************************/
#include "..\TEST-HALCON\opencvlib249\x64\include\opencv2\opencv.hpp"
using namespace cv;
/*******************************************************************************/
构造函数
TESTHALCON::TESTHALCON(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
/*HObject himg;
cv::Mat cvimg;
QImage qimg;
//qimg.load("D:\\3333.bmp");
//qimg.load("D:\\33.tiff");
//ReadImage(&himg, "D:\\33.tiff");
///*bool bflag*/cvimg = QImage2Mat(qimg/*, &cvimg*/);
//cv::imwrite("D:\\write_HObj_cv_6.bmp", cvimg);
//cv::imwrite("D:\\write_HObj_cv_6.tiff", cvimg);
//QImageToHOjbect(qimg,&himg);
//WriteImage(himg, "tiff", 0, "D:\\++++.tiff");
//HObject2Mat(himg, cvimg);
//cv::imwrite("D:\\write_HObj_cv_6.tiff", cvimg);
//HObject2QImage(&himg,qimg);
//HObject2QImage(himg, qimg);
//qimg.save("D:\\------.tiff");
//QImage2HImage(qimg, himg);
loadConfig(qApp->applicationDirPath() + "/DevConfig/xxx.txt");
}
TESTHALCON* TESTHALCON::m_pInstance = nullptr;
TESTHALCON* TESTHALCON::GetInstance()
{
if (m_pInstance == nullptr)
{
m_pInstance = new TESTHALCON;
}
return m_pInstance;
}
//Mat转为QImage图像对象;
bool TESTHALCON::MatToQImage(cv::Mat& mat, QImage &_image)
{
if (mat.type() == CV_8UC1)
{
QImage image(mat.cols, mat.rows, QImage::Format_Indexed8);
// Set the color table (used to translate colour indexes to qRgb values)
image.setColorCount(256);
for (int i = 0; i < 256; i++)
{
image.setColor(i, qRgb(i, i, i));
}
// Copy input Mat
uchar* pSrc = mat.data;
for (int row = 0; row < mat.rows; row++)
{
uchar* pDest = image.scanLine(row);
memcpy(pDest, pSrc, mat.cols);
pSrc += mat.step;
}
_image = image;
return true;
}
// 8-bits unsigned, NO. OF CHANNELS = 3
else if (mat.type() == CV_8UC3)
{
// Copy input Mat
const uchar* pSrc = (const uchar*)mat.data;
// Create QImage with same dimensions as input Mat
QImage image(pSrc, mat.cols, mat.rows, mat.step, QImage::Format_RGB888);
_image = image.rgbSwapped();
return true;
}
else if (mat.type() == CV_8UC4)
{
// Copy input Mat
const uchar* pSrc = (const uchar*)mat.data;
// Create QImage with same dimensions as input Mat
QImage image(pSrc, mat.cols, mat.rows, mat.step, QImage::Format_ARGB32);
_image = image.copy();
return true;
}
else
{
_image = QImage();
return false;
}
}
// Hobject转为Mat图像对象; src: Mat图像对象;[Return]QImage图像对象;---
bool TESTHALCON::HObject2Mat(HalconCpp::HObject & srcImage, cv::Mat * dstImage)
{
if ( !dstImage) return false;
int mType = CV_8UC1;
HTuple lChannels = 0;
CountChannels(srcImage, &lChannels);
Hlong strlChannels = lChannels;
if (strlChannels != 1 && strlChannels != 3) return false;
// 图像类型获取
HTuple lWid = 0, lHei = 0;
GetImageSize(srcImage, &lWid, &lHei);
HTuple cType[16] = { 0 };
GetImageType(srcImage, cType);
int szBytesChn = 0;
QString strvalue = cType[0].S();
if (strvalue == QString("byte")) {
mType = (strlChannels == 1) ? CV_8UC1 : CV_8UC3;
szBytesChn = lWid*lHei;
}
else if (strvalue == QString("uint2") && 1 == strlChannels) {
mType = CV_16UC1;
szBytesChn = lWid*lHei * 2;
}
else { return false; }
// 构造新的Mat对象
if (dstImage->cols != lWid || dstImage->rows != lHei || dstImage->type() != mType) {
dstImage->release();
dstImage->create(lWid, lHei, mType);
}
// 复制图像数据
if (1 == strlChannels) {
char* ptr = nullptr;
GetImagePointer1(srcImage, (HTuple*)&ptr, cType, (HTuple*)&lWid, (HTuple*)&lHei);
if (ptr) {
memcpy_s(dstImage->data, szBytesChn, ptr, szBytesChn);
}
}
else if (3 == strlChannels && cType->ToString() == QString("byte")) {
char *ptrGreen = nullptr, *ptrRed = nullptr, *ptrBlue = nullptr;
GetImagePointer3(srcImage, (HTuple*)&ptrRed, (HTuple*)&ptrGreen, (HTuple*)&ptrBlue, cType, (HTuple*)&lWid, (HTuple*)&lHei);
if (ptrBlue && ptrGreen && ptrRed) {
std::vector<cv::Mat> vecM(3);
vecM[0].create(lWid, lHei, CV_8UC1);
vecM[1].create(lWid, lHei, CV_8UC1);
vecM[2].create(lWid, lHei, CV_8UC1);
memcpy_s(vecM[2].data, szBytesChn, ptrRed, szBytesChn);
memcpy_s(vecM[1].data, szBytesChn, ptrGreen, szBytesChn);
memcpy_s(vecM[0].data, szBytesChn, ptrBlue, szBytesChn);
cv::merge(vecM, *dstImage);
}
}
else { return false; }
return true;
}
//Halcon中的HObject类型转QImage类型
bool TESTHALCON::HObject2QImage(const HalconCpp::HObject *src, QImage &dst/*, QVector<uint> g_Color_table*/)
{
if (!src) return false;
HTuple imgType[32] = { 0 };
GetImageType(*src, imgType);
HObject dumpImg;
GenEmptyObj(&dumpImg);
QString strvalue = imgType[0].S();
if (strvalue ==QString("byte")) {
ScaleImageMax(*src, &dumpImg);
}
else {
CopyImage(*src, &dumpImg);
}
QImage qimDst;
HTuple strlCh = 0, strlWidth = 0, strlHeight = 0;
Hlong lCh = 0, lWidth = 0, lHeight = 0;
CountChannels(*src, &strlCh);
lCh = strlCh;
if (1 == lCh)
{
HTuple dataPtr ;
GetImagePointer1(dumpImg, &dataPtr, imgType, &strlWidth, &strlHeight);
lWidth = strlWidth;
lHeight = strlHeight;
if (!dataPtr || lWidth < 1 || lHeight < 1) return false;
QVector<uint> g_Color_table;
for (int i = 0; i < 256; i++) {
g_Color_table << qRgb(i, i, i);
}
if (strvalue == QString("byte"))
{
qimDst = QImage((uchar*)&dataPtr, lWidth, lHeight, lWidth, QImage::Format_Indexed8);
qimDst.setColorTable(g_Color_table);
}
else {
qimDst = QImage((uchar*)&dataPtr, lWidth, lHeight, lWidth, QImage::Format_Grayscale16);
qimDst.setColorTable(g_Color_table);
}
dst = qimDst.copy();
}
else if (3 == lCh)
{
uchar *greenPtr = nullptr, *redPtr = nullptr, *bluePtr = nullptr;
GetImagePointer3(dumpImg, (HTuple *)&redPtr, (HTuple *)&greenPtr, (HTuple *)&bluePtr, imgType, &strlWidth, &strlHeight);
lWidth = strlWidth;
lHeight = strlHeight;
if (!redPtr || !greenPtr || !bluePtr || lWidth < 1 && lHeight < 1) return false;
int nAlignWidth = ((lWidth * lCh + 3) / 4) * 4;
int szAlign = int(lWidth*lHeight*lCh * sizeof(uchar));
uchar *alignBuf = new uchar[szAlign];
memset(alignBuf, 0, szAlign);
for (int h = 0; h < lHeight; h++)
{
for (int w = 0; w < lWidth; w++)
{
*(alignBuf + h*lWidth*lCh + w*lCh + 0) = *(redPtr + w + h*lWidth);
*(alignBuf + h*lWidth*lCh + w*lCh + 1) = *(greenPtr + w + h*lWidth);
*(alignBuf + h*lWidth*lCh + w*lCh + 2) = *(bluePtr + w + h*lWidth);
}
}
qimDst = QImage(alignBuf, lWidth, lHeight, lWidth*lCh, QImage::Format_RGB888);
dst = qimDst.copy();
delete[] alignBuf;
alignBuf = nullptr;
}
else { return false; }
return true;
}
//Mat转为HObject图像对象并重新申请内存;成功 - true,失败 - false;
bool TESTHALCON::CvMat2HObject(cv::Mat & src, HalconCpp::HObject * HObj)
{
if (!src.data || src.empty() || !HObj) return false;
HObject tmpImg;
GenEmptyObj(&tmpImg);
int format = src.type();
switch (format)
{
case CV_8UC1:
GenImage1(&tmpImg, "byte", src.cols, src.rows, (Hlong)src.data);
break;
case CV_8UC3:
{
std::vector<cv::Mat> rgbChn_v(3);
cv::split(src, rgbChn_v);
GenImage3(&tmpImg, "byte", src.cols, src.rows, (Hlong)rgbChn_v.at(2).data, (Hlong)rgbChn_v.at(1).data, (Hlong)rgbChn_v.at(0).data);
}
break;
case CV_16U:
GenImage1(&tmpImg, "uint2", src.cols, src.rows, (Hlong)src.data);
break;
default:
return false;
}
CopyImage(tmpImg, HObj);
return true;
}
//QImage格式转化Mat类型-ok
//Qt读入彩色图后一般为Format_RGB32格式(4通道),而OpenCV一般用3通道的,因此进行了转换。
bool TESTHALCON::QImage2Mat(QImage image, cv::Mat &mat)
{
qDebug() << image.format();
switch (image.format())
{
case QImage::Format_RGB32: //一般Qt读入彩色图后为此格式
mat = cv::Mat(image.height(), image.width(), CV_8UC4, (void*)image.constBits(), image.bytesPerLine());
cv::cvtColor(mat, mat, cv::COLOR_BGRA2BGR); //转3通道
break;
case QImage::Format_RGB888:
mat = cv::Mat(image.height(), image.width(), CV_8UC3, (void*)image.constBits(), image.bytesPerLine());
cv::cvtColor(mat, mat, cv::COLOR_RGB2BGR);
break;
case QImage::Format_Indexed8:
mat = cv::Mat(image.height(), image.width(), CV_8UC1, (void*)image.constBits(), image.bytesPerLine());
break;
case QImage::Format_Grayscale16://单通道16位图
mat = cv::Mat(image.height(), image.width(), CV_16UC1, (void*)image.constBits(), image.bytesPerLine());
break;
default:
return false;
}
return true;
}
QImageToHOjbect
int TESTHALCON::QImageToHOjbect(QImage &image, HObject *Object)
{
if (image.format() == QImage::Format_RGB888
|| image.format() == QImage::Format_RGB32
|| image.format() == QImage::Format_Indexed8
|| image.format() == QImage::Format_Grayscale16){}
else{return -1; }
int ret = 0;
unsigned char *image_red = nullptr;
unsigned char *image_green = nullptr;
unsigned char *image_blue = nullptr;
int r, c;
int w = image.width();
int h = image.height();
try
{
image_red = (unsigned char *)malloc(w*h);
if (image.format() == QImage::Format_RGB888
|| image.format() == QImage::Format_RGB32)
{
image_green = (unsigned char *)malloc(w*h);
image_blue = (unsigned char *)malloc(w*h);
}
}
catch (...)
{
free(image_red);
free(image_green);
free(image_blue);
return -1;
}
if (image.format() == QImage::Format_RGB888)
{
for (r = 0; r < h; r++)
{
uchar* pRow = image.scanLine(r);
for (c = 0; c < w; c++)
{
image_red[r*w + c] = pRow[3 * c + 0];
image_green[r*w + c] = pRow[3 * c + 1];
image_blue[r*w + c] = pRow[3 * c + 2];
}
}
GenImage3Extern(Object, "byte", w, h, (Hlong)image_red, (Hlong)image_green, (Hlong)image_blue, (Hlong)free);
free(image_red); free(image_green); free(image_blue);
}
else if (image.format() == QImage::Format_RGB32)
{
for (r = 0; r < h; r++)
{
uchar* pRow = image.scanLine(r);
for (c = 0; c < w; c++)
{
image_red[r*w + c] = pRow[4 * c + 2];
image_green[r*w + c] = pRow[4 * c + 1];
image_blue[r*w + c] = pRow[4 * c + 0];
}
}
GenImage3Extern(Object, "byte", w, h, (Hlong)image_red, (Hlong)image_green, (Hlong)image_blue, (Hlong)free);
}
else if (image.format() == QImage::Format_Indexed8)
{
for (r = 0; r < h; r++)
{
uchar* pRow = image.scanLine(r);
for (c = 0; c < w; c++)
{
image_red[r*w + c] = pRow[c];
//image_red[r*w + c] = 0.30 * pBmp[3 * (c + r*w) + 0] + 0.59 * pBmp[3 * (c + r*w) + 1] + 0.11 *pBmp[3 * (c + r*w) + 2];
}
}
GenImage1Extern(Object, "byte", w, h, (Hlong)image_red, (Hlong)free);
}
else if (image.format() == QImage::Format_Grayscale16)
{
for (r = 0; r < h; r++)
{
uchar* pRow = image.scanLine(r);
for (c = 0; c < w; c++)
{
image_red[r*w + c] = pRow[c];
//image_red[r*w + c] = 0.30 * pBmp[3 * (c + r*w) + 0] + 0.59 * pBmp[3 * (c + r*w) + 1] + 0.11 *pBmp[3 * (c + r*w) + 2];
}
}
//GenImage1Extern(Object, "uint2", w, h, (Hlong)image_red, (Hlong)free);
GenImage1(Object,"tiff", w, h, (Hlong)image.data_ptr());
WriteImage(*Object, "tiff", 0, "D:\\++++.tiff");
}
return ret;
}
//Halcon中的HObject类型转OpenCV中的cv::Mat类型-ok
bool TESTHALCON::HObject2Mat(const HalconCpp::HObject &Hobj, cv::Mat &Image)
{
HalconCpp::HTuple htCh = HalconCpp::HTuple();
HalconCpp::HTuple hv_Length, cType;
HalconCpp::HObject ho_img;
HObject dumpImg;
HTuple strType[32] = { 0 };
GetImageType(Hobj, strType);
QString strvalue = strType[0].S();
if (strvalue == QString("uint2"))
{//16bit--Halcon HObject转OpenCV Mat
HTuple channels;
HString cType;
ConvertImageType(Hobj, &ho_img, "uint2");
CountChannels(ho_img, &channels);
Hlong width = 0;
Hlong height = 0;
if (channels[0].I() == 1)
{
HImage hImg(ho_img);
void *ptr = hImg.GetImagePointer1(&cType, &width, &height);//GetImagePointer1(Hobj, &ptr, &cType, &wid, &hgt);
int W = width;
int H = height;
Image.create(H, W, CV_16UC1);
unsigned char *pdata = static_cast<unsigned char *>(ptr);
memcpy(Image.data, pdata, W*H*2);
}
else if (channels[0].I() == 3)
{
void *Rptr; void *Gptr; void *Bptr;
HImage hImg(ho_img);
hImg.GetImagePointer3(&Rptr, &Gptr, &Bptr, &cType, &width, &height);
int W = width;
int H = height;
Image.create(H, W, CV_16UC3);
vector<cv::Mat> VecM(3);
VecM[0].create(H, W, CV_16UC1);
VecM[1].create(H, W, CV_16UC1);
VecM[2].create(H, W, CV_16UC1);
unsigned char *R = (unsigned char *)Rptr;
unsigned char *G = (unsigned char *)Gptr;
unsigned char *B = (unsigned char *)Bptr;
memcpy(VecM[2].data, R, W*H);
memcpy(VecM[1].data, G, W*H);
memcpy(VecM[0].data, B, W*H);
cv::merge(VecM, Image);
}
return true;
}
else {
ConvertImageType(Hobj, &ho_img, "byte");
CountChannels(ho_img, &htCh);
TupleLength(htCh, &hv_Length);
if (hv_Length.L() == 0)
{
return false;
}
HalconCpp::HTuple width;
HalconCpp::HTuple height;
int W = 0, H = 0;
if (htCh[0].I() == 1)
{
HalconCpp::HTuple ptr;
GetImagePointer1(ho_img, &ptr, &cType, &width, &height);
W = (int)width;
H = (int)height;
BYTE *p = (BYTE *)ptr[0].L(); //必须是L(),不能是I()
Image = cv::Mat(H, W, CV_8UC1, p); //单通道
//或者
//Image.create(H, W, CV_8UC1);
//memcpy(Image.data, p, W * H * sizeof(BYTE));
}
else if (htCh[0].I() == 3)
{
HalconCpp::HTuple ptrR, ptrG, ptrB;
GetImagePointer3(ho_img, &ptrR, &ptrG, &ptrB, &cType, &width, &height);
W = (int)width;
H = (int)height;
Image.create(H, W, CV_8UC3); //三通道
vector<cv::Mat> vecM(3);
vecM[2].create(H, W, CV_8UC1);
vecM[1].create(H, W, CV_8UC1);
vecM[0].create(H, W, CV_8UC1);
uchar *pr = (uchar *)ptrR[0].L();
uchar *pg = (uchar *)ptrG[0].L();
uchar *pb = (uchar *)ptrB[0].L();
memcpy(vecM[2].data, pr, W * H); //Mat以bgr格式输出
memcpy(vecM[1].data, pg, W * H);
memcpy(vecM[0].data, pb, W * H);
cv::merge(vecM, Image);
}
return true;
}
return false;
}
//QImage彩色转Halcon彩色
HObject TESTHALCON::QImageToHObject(QImage image) //QImage彩色转Halcon彩色
{
//注意:在使用这个函数之前,最好先判断图像是否三通道
unsigned char *data = image.bits();//获取图像像素字节数据的首地址
int width = image.width();//图像宽
int height = image.height();//图像高
unsigned char* dataRed = (unsigned char*)malloc(width*height);//存储处理后的数据
unsigned char* dataGreen = (unsigned char*)malloc(width*height);//存储处理后的数据
unsigned char* dataBlue = (unsigned char*)malloc(width*height);//存储处理后的数据
for (int i = 0; i<height; i++) {
for (int j = 0; j<width; j++) {
dataRed[i*width + j] = *(data + 2);
dataGreen[i*width + j] = *(data + 1);
dataBlue[i*width + j] = *data;
data += 4;
}
}
HObject Image;
GenImage3(&Image, "byte", width, height, (Hlong)(dataRed), (Hlong)(dataGreen), (Hlong)(dataBlue));
return Image;
}
//Halcon中的HObject类型转QImage类型-ok
bool TESTHALCON::HObject2QImage(const HalconCpp::HObject &Hobj, QImage &Image)
{
HalconCpp::HTuple htCh = HalconCpp::HTuple();
HalconCpp::HTuple hv_Length;
HalconCpp::HTuple cType;
HalconCpp::HObject ho_img;
HTuple type[] = { 0 };
GetImageType(Hobj, type);
QString strvalue = type[0].S();
if (QString("byte") == strvalue)
{
ConvertImageType(Hobj, &ho_img, "byte");
CountChannels(ho_img, &htCh);
TupleLength(htCh, &hv_Length);
if (hv_Length.L() == 0)
{
return false;
}
HalconCpp::HTuple width;
HalconCpp::HTuple height;
int W = 0, H = 0;
if (htCh[0].I() == 1)
{
HalconCpp::HTuple ptr;
GetImagePointer1(ho_img, &ptr, &cType, &width, &height);
W = (int)width;
H = (int)height;
BYTE *p = (BYTE *)ptr[0].L(); //必须是L(),不能是I()
//Set buf data
QImage tmp = QImage(W, H, QImage::Format_Indexed8);
Image = tmp.copy();
memcpy(Image.bits(), p, W * H); //单通道
QVector<QRgb> vtColorTable;
for (int k = 0; k < 256; ++k)
{
vtColorTable.push_back(qRgb(k, k, k));
}
Image.setColorTable(vtColorTable);
}
else if (htCh[0].I() == 3)
{
HalconCpp::HTuple ptrR, ptrG, ptrB;
GetImagePointer3(ho_img, &ptrR, &ptrG, &ptrB, &cType, &width, &height);
W = (int)width;
H = (int)height;
uchar *pr = (uchar *)ptrR[0].L(); //三通道
uchar *pg = (uchar *)ptrG[0].L();
uchar *pb = (uchar *)ptrB[0].L();
QImage tmp = QImage(W, H, QImage::Format_RGB888);
Image = tmp.copy();
int bytesPerLine = W * 3;
int lineHeadId = 0;
int pixId = 0;
BYTE *data24 = Image.bits();
for (int i = 0; i < H; i++)
{
lineHeadId = bytesPerLine * i;
for (int k = 0; k < W; k++)
{
pixId = lineHeadId + k * 3;
data24[pixId + 0] = pr[W * i + k];
data24[pixId + 1] = pg[W * i + k];
data24[pixId + 2] = pb[W * i + k];
}
}
}
return true;
}
else
{
ConvertImageType(Hobj, &ho_img, "uint2");
CountChannels(ho_img, &htCh);
TupleLength(htCh, &hv_Length);
if (hv_Length.L() == 0)
{
return false;
}
HalconCpp::HTuple width;
HalconCpp::HTuple height;
int W = 0, H = 0;
if (htCh[0].I() == 1)
{
HalconCpp::HTuple ptr;
GetImagePointer1(ho_img, &ptr, &cType, &width, &height);
W = (int)width;
H = (int)height;
BYTE *p = (BYTE *)ptr[0].L(); //必须是L(),不能是I()
//Set buf data
QImage tmp = QImage(W, H, QImage::Format_Grayscale16);
Image = tmp.copy();
memcpy(Image.bits(), p, W * H*2); //单通道
QVector<QRgb> vtColorTable;
for (int k = 0; k < 256; ++k)
{
vtColorTable.push_back(qRgb(k, k, k));
}
Image.setColorTable(vtColorTable);
}
else if (htCh[0].I() == 3)
{
HalconCpp::HTuple ptrR, ptrG, ptrB;
GetImagePointer3(ho_img, &ptrR, &ptrG, &ptrB, &cType, &width, &height);
W = (int)width;
H = (int)height;
uchar *pr = (uchar *)ptrR[0].L(); //三通道
uchar *pg = (uchar *)ptrG[0].L();
uchar *pb = (uchar *)ptrB[0].L();
QImage tmp = QImage(W, H, QImage::Format_RGB888);
Image = tmp.copy();
int bytesPerLine = W * 3;
int lineHeadId = 0;
int pixId = 0;
BYTE *data24 = Image.bits();
for (int i = 0; i < H; i++)
{
lineHeadId = bytesPerLine * i;
for (int k = 0; k < W; k++)
{
pixId = lineHeadId + k * 3;
data24[pixId + 0] = pr[W * i + k];
data24[pixId + 1] = pg[W * i + k];
data24[pixId + 2] = pb[W * i + k];
}
}
}
return true;
}
return false;
}
加载txt文件
void TESTHALCON::loadConfig(QString path)
{
Mat img;
img = imread(qApp->applicationDirPath() + "/2.tif", -1);
QStringList v;
QFile file(path);
if (!file.exists())return;
//创建QSettings对象并指定ini文件路径并将格式设置为ini
QSettings setting(path, QSettings::NativeFormat);
setting.setIniCodec("UTF-8");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))//打开文件,以只读的方式打开文本文件
{
qDebug() << file.errorString();
}
else {
QTextStream in(&file);
QString str;
while (!in.atEnd())
{
str = in.readLine();
qDebug() << str;
v << (str);
}
}
file.close();
for (int i = 0; i < v.size(); i++)
{
}
QStringList SY;
QFile file1(qApp->applicationDirPath() + "/defect_3072x3072.xy");
if (!file1.exists())return;
//创建QSettings对象并指定ini文件路径并将格式设置为ini
QSettings setting1(qApp->applicationDirPath() + "defect_3072x3072.xy", QSettings::NativeFormat);
setting.setIniCodec("UTF-8");
if (!file1.open(QIODevice::ReadOnly | QIODevice::Text))//打开文件,以只读的方式打开文本文件
{
qDebug() << file1.errorString();
}
else {
QTextStream in(&file1);
QString str;
while (!in.atEnd())
{
str = in.readLine();
qDebug() << str;
SY << (str);
}
}
file.close();
cv::Scalar color_blue(255, 47, 0);
foreach (QString s, SY)
{
QStringList list = s.split(",");
if (!s.contains(",")) continue;
for(int i = 0;, i< list.size(); i++)
{
QString str1, str2;
if (i == 0) { str1 = list[i]; }
if (i == 1) { str2 = list[i]; }
if(i ==1)
cv::putText((img, "", cv::Point(str1, str2), cv::FONT_HERSHEY_SIMPLEX, 0.8, color_blue, 2);
}
}
cv::imwrite("D:\\write_HObj_cv_6.tiff", img);
}
画点
void TESTHALCON::drawPoint(QStringList path)
{
}
/**
* @brief QImage2HImage 将 Qt QImage 转换为 Halcon 的 HImage
* @param from 输入的 QImage
* @param to 输出的 HImage ,from 和 to 不共享内存数据。 每次都会为 to 重新分配内存。
* @return true 表示转换成功,false 表示转换失败。
*/
void HObjectToQImage(HObject himage, QImage **qimage)
{
HTuple hChannels;
HTuple width, height;
width = height = 0;
HTuple htype;
ConvertImageType(himage, &himage, "byte");//将图片转化成byte类型
CountChannels(himage, &hChannels); //判断图像通道数
if (hChannels[0].I() == 1)//单通道图
{
HTuple hv_pointer;
unsigned char *ptr;
GetImagePointer1(himage, &hv_pointer, &htype, &width, &height);
ptr = (unsigned char *)hv_pointer[0].L();
*(*qimage) = QImage(ptr, width, height, width, QImage::Format_Indexed8);//不知道是否已自动4字节对齐
}
else if (hChannels[0].I() == 3)//三通道图
{
HTuple hv_ptrRed, hv_ptrGreen, hv_ptrBlue;
GetImagePointer3(himage, &hv_ptrRed, &hv_ptrGreen, &hv_ptrBlue, &htype, &width, &height);
uchar *ptrRed = (uchar*)hv_ptrRed[0].L();
uchar *ptrGreen = (uchar*)hv_ptrGreen[0].L();
uchar *ptrBlue = (uchar*)hv_ptrBlue[0].L();
int bytesperline = (width * 8 * 3 + 31) / 32 * 4;//针对位深为8的三通道图进行每行4字节对齐补齐
int bytecount = bytesperline*height;//整个图像数据需要的字节数
uchar* data8 = new uchar[bytecount];
int lineheadid, pixid;
for (int i = 0; i<height; i++)
{
lineheadid = bytesperline*i;//计算出图像第i行的行首在图像数据中的地址
for (int j = 0; j<width; j++)
{
pixid = lineheadid + j * 3;//计算坐标为(i,j)的像素id
data8[pixid] = ptrRed[width*i + j];
data8[pixid + 1] = ptrGreen[width*i + j];
data8[pixid + 2] = ptrBlue[width*i + j];
}
}
*(*qimage) = QImage(data8, width, height, QImage::Format_RGB888);
}
}
标签:转换,HalconCpp,int,image,HTuple,uchar,图像,数据格式,QImage
From: https://www.cnblogs.com/alisa5270/p/17926093.html