转换数据格式RGB8、Mono8
MvGvspPixelType enDstPixelType = PixelType_Gvsp_Undefined;
unsigned int nChannelNum = 0;
//如果是彩色则转成RGB8
if (mvCamera->IsColor(pFrameInfo->enPixelType))
{
nChannelNum = 3;
enDstPixelType = PixelType_Gvsp_RGB8_Packed;
}
//如果是黑白则转换成Mono8
else if (mvCamera->IsMono(pFrameInfo->enPixelType))
{
nChannelNum = 1;
enDstPixelType = PixelType_Gvsp_Mono8;
}
int hgt = pFrameInfo->nHeight;
int wid = pFrameInfo->nWidth;
unsigned char *pConvertData = NULL;
pConvertData = (unsigned char*)malloc(pFrameInfo->nWidth * pFrameInfo->nHeight * nChannelNum);
unsigned int nConvertDataSize = pFrameInfo->nWidth * pFrameInfo->nHeight * nChannelNum;
// ch:像素格式转换 | en:Convert pixel format
MV_CC_PIXEL_CONVERT_PARAM stConvertParam = { 0 };
stConvertParam.nWidth = pFrameInfo->nWidth; //ch:图像宽 | en:image width
stConvertParam.nHeight = pFrameInfo->nHeight; //ch:图像高 | en:image height
stConvertParam.pSrcData = pData; //ch:输入数据缓存 | en:input data buffer
stConvertParam.nSrcDataLen = pFrameInfo->nFrameLen; //ch:输入数据大小 | en:input data size
stConvertParam.enSrcPixelType = pFrameInfo->enPixelType; //ch:输入像素格式 | en:input pixel format
stConvertParam.enDstPixelType = enDstPixelType; //ch:输出像素格式 | en:output pixel format
stConvertParam.pDstBuffer = pConvertData; //ch:输出数据缓存 | en:output data buffer
stConvertParam.nDstBufferSize = nConvertDataSize; //ch:输出缓存大小 | en:output buffer size
int nRet = MV_CC_ConvertPixelType(mvCamera->m_hDevHandle, &stConvertParam);
unsigned char * 转QImage
QImage image;
if (mvCamera->IsMono(pFrameInfo->enPixelType))
{
image = QImage(pData, pFrameInfo->nWidth, pFrameInfo->nHeight, QImage::Format_Indexed8);
}
else if (mvCamera->IsColor(pFrameInfo->enPixelType))
{
image = QImage(pConvertData, pFrameInfo->nWidth, pFrameInfo->nHeight, QImage::Format_RGB888);//浅拷贝
}
QImage imageSend = image.copy();//深拷贝
unsigned char * 转HObject
HObject ho_Image;
if (mvCamera->IsMono(pFrameInfo->enPixelType))
{
int wh = wid* hgt;
unsigned char* data= new unsigned char[wh];
memcpy(data, pConvertData, wid * hgt);
GenImage1(&ho_Image, "byte", wid, hgt, (Hlong)(data));
emit mvCamera->sendDeviceGrabHalconResultSignal(sn, ho_Image);
delete[]data;
data = NULL;
}
else if (mvCamera->IsColor(pFrameInfo->enPixelType))
{
int wh = wid * hgt;
//unsigned char*图像转换为HObject
unsigned char* dataRed = new unsigned char[wh];
unsigned char* dataGreen = new unsigned char[wh];
unsigned char* dataBlue = new unsigned char[wh];
unsigned char* data = new unsigned char[wh * 3];
memcpy(data, pConvertData, wh * 3);
for (int i = 0; i < wid * hgt; i++)
{
dataRed[i] = data[3 * i];
dataGreen[i] = data[3 * i + 1];
dataBlue[i] = data[3 * i + 2];
}
GenImage3(&ho_Image, "byte", wid, hgt, (Hlong)(dataRed), (Hlong)(dataGreen), (Hlong)(dataBlue));
emit mvCamera->sendDeviceGrabHalconResultSignal(sn, ho_Image);
delete[] data;
delete[] dataRed;
delete[] dataGreen;
delete[] dataBlue;
data = NULL;
dataRed = NULL;
dataGreen = NULL;
dataBlue = NULL;
}
unsigned char*转Mat
cv::Mat srcImage;
if (mvCamera->IsMono(pFrameInfo->enPixelType))
{
srcImage = cv::Mat(stImageInfo->stFrameInfo.nHeight, stImageInfo->stFrameInfo.nWidth, CV_8UC1, pData);
}
else if (mvCamera->IsColor(pFrameInfo->enPixelType))
{
RGB2BGR(pData, stImageInfo->stFrameInfo.nWidth, stImageInfo->stFrameInfo.nHeight);
srcImage = cv::Mat(stImageInfo->stFrameInfo.nHeight, stImageInfo->stFrameInfo.nWidth, CV_8UC3, pData);
}
int Coin::RGB2BGR(unsigned char* pRgbData, unsigned int nWidth, unsigned int nHeight)
{
if (NULL == pRgbData)
{
return MV_E_PARAMETER;
}
for (unsigned int j = 0; j < nHeight; j++)
{
for (unsigned int i = 0; i < nWidth; i++)
{
unsigned char red = pRgbData[j * (nWidth * 3) + i * 3];
pRgbData[j * (nWidth * 3) + i * 3] = pRgbData[j * (nWidth * 3) + i * 3 + 2];
pRgbData[j * (nWidth * 3) + i * 3 + 2] = red;
}
}
return MV_OK;
}