图像数据
h
class ImageConvert
{
public:
ImageConvert();
//halcon_opencv
void himg_to_cvimg(HImage &Hobj,Mat& cvimg);
void cvimg_to_himg(cv::Mat& pImage,HObject &himg);
//halcon_to_qimg
void himg_to_qimg(HalconCpp::HImage &from, QImage &to);
void qimg_to_himg(QImage &to,HalconCpp::HImage &from);
//opencv_qimg
void cvimg_to_qimg(const cv::Mat& mat,QImage& qimg);
void qimg_to_cvimg(QImage image,Mat& cvimg);
};
cpp
///H_cv///
void ImageConvert::himg_to_cvimg(HImage &Hobj,Mat& cvimg)
{
QImage tmp;
himg_to_qimg(Hobj,tmp);
qimg_to_cvimg(tmp,cvimg);
}
void ImageConvert::cvimg_to_himg(cv::Mat& pImage,HObject &himg)
{
HObject Hobj;
if (3 == pImage.channels())
{
cv::Mat pImageRed, pImageGreen, pImageBlue;
std::vector<cv::Mat> sbgr(3);
cv::split(pImage, sbgr);
int length = pImage.rows * pImage.cols;
uchar *dataBlue = new uchar[length];
uchar *dataGreen = new uchar[length];
uchar *dataRed = new uchar[length];
int height = pImage.rows;
int width = pImage.cols;
for (int row = 0; row < height; row++)
{
uchar* ptr = pImage.ptr<uchar>(row);
for (int col = 0; col < width; col++)
{
dataBlue[row * width + col] = ptr[3 * col];
dataGreen[row * width + col] = ptr[3 * col + 1];
dataRed[row * width + col] = ptr[3 * col + 2];
}
}
GenImage3(&Hobj, "byte", width, height, (Hlong)(dataRed), (Hlong)(dataGreen), (Hlong)(dataBlue));
delete[] dataRed;
delete[] dataGreen;
delete[] dataBlue;
}
else if (1 == pImage.channels())
{
int height = pImage.rows;
int width = pImage.cols;
uchar *dataGray = new uchar[width * height];
memcpy(dataGray, pImage.data, width * height);
GenImage1(&Hobj, "byte", width, height, (Hlong)(dataGray));
delete[] dataGray;
}
himg= Hobj;
}
///H_cv///
///H_Q///
void ImageConvert::himg_to_qimg(HalconCpp::HImage &from, QImage &to)
{
Hlong width;
Hlong height;
from.GetImageSize(&width, &height);
HTuple channels = from.CountChannels();
HTuple type = from.GetImageType();
if( strcmp(type[0].S(), "byte" )) // 如果不是 byte 类型,则失败
{
cout<<"himg_to_qimg fail1"<<endl;;
}
QImage::Format format;
switch(channels[0].I())
{
case 1:
format = QImage::Format_Grayscale8;
break;
case 3:
format = QImage::Format_RGB32;
break;
default:
cout<<"himg_to_qimg fail2"<<endl;;
}
if(to.width() != width || to.height() != height || to.format() != format)
{
to = QImage(static_cast<int>(width),
static_cast<int>(height),
format);
}
HString Type;
if(channels[0].I() == 1)
{
unsigned char * pSrc = reinterpret_cast<unsigned char *>( from.GetImagePointer1(&Type, &width, &height) );
memcpy( to.bits(), pSrc, static_cast<size_t>(width) * static_cast<size_t>(height) );
// return true;
}
else if(channels[0].I() == 3)
{
uchar *R, *G, *B;
from.GetImagePointer3(reinterpret_cast<void **>(&R),
reinterpret_cast<void **>(&G),
reinterpret_cast<void **>(&B), &Type, &width, &height);
for(int row = 0; row < height; row ++)
{
QRgb* line = reinterpret_cast<QRgb*>(to.scanLine(row));
for(int col = 0; col < width; col ++)
{
line[col] = qRgb(*R++, *G++, *B++);
}
}
// return true;
}
// cout<<"himg_to_qimg fail3"<<endl;
}
void ImageConvert::qimg_to_himg(QImage &from,HalconCpp::HImage &to)
{
Mat tmp;
qimg_to_cvimg(from,tmp);
cvimg_to_himg(tmp,to);
}
///H_Q///
///C_Q///
void ImageConvert::cvimg_to_qimg(const cv::Mat& mat,QImage& qimg)
{
// 8-bits unsigned, NO. OF CHANNELS = 1
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;
}
qimg= image.copy();
}
// 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);
qimg= image.rgbSwapped();
}
else if(mat.type() == CV_8UC4)
{
qDebug() << "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);
qimg= image.copy();
}
else
{
qDebug() << "ERROR: Mat could not be converted to QImage.";
qimg= QImage();
}
}
void ImageConvert::qimg_to_cvimg(QImage image,Mat& cvimg)
{
cv::Mat mat;
// qDebug() << image.format();
switch(image.format())
{
case QImage::Format_ARGB32:
case QImage::Format_RGB32:
case QImage::Format_ARGB32_Premultiplied:
mat = cv::Mat(image.height(), image.width(), CV_8UC4, (void*)image.constBits(), image.bytesPerLine());
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_BGR2RGB);
break;
case QImage::Format_Indexed8:
mat = cv::Mat(image.height(), image.width(), CV_8UC1, (void*)image.constBits(), image.bytesPerLine());
break;
}
cvimg= mat.clone();
}
///C_Q///
速度:
全部在0.01s之内