ImageProcess.cpp

Process images.

The sample code below shows how to convert pixel format and save images. After getting images, you can use MV_CC_ConvertPixelType() to convert data format and use MV_CC_SaveImageEx2() to save images.

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include "MvCameraControl.h"
// Wait for user to press enter to stop acquisition or end the sample program
void PressEnterToExit(void)
{
int c;
while ( (c = getchar()) != '\n' && c != EOF );
fprintf( stderr, "\nPress enter to exit.\n");
while( getchar() != '\n');
}
bool PrintDeviceInfo(MV_CC_DEVICE_INFO* pstMVDevInfo)
{
if (NULL == pstMVDevInfo)
{
printf("The Pointer of pstMVDevInfo is NULL!\n");
return false;
}
if (pstMVDevInfo->nTLayerType == MV_GIGE_DEVICE)
{
int nIp1 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0xff000000) >> 24);
int nIp2 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x00ff0000) >> 16);
int nIp3 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x0000ff00) >> 8);
int nIp4 = (pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x000000ff);
// Print current IP and user defined name
printf("Device Model Name: %s\n", pstMVDevInfo->SpecialInfo.stGigEInfo.chModelName);
printf("CurrentIp: %d.%d.%d.%d\n" , nIp1, nIp2, nIp3, nIp4);
printf("UserDefinedName: %s\n\n" , pstMVDevInfo->SpecialInfo.stGigEInfo.chUserDefinedName);
}
else if (pstMVDevInfo->nTLayerType == MV_USB_DEVICE)
{
printf("Device Model Name: %s\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chModelName);
printf("UserDefinedName: %s\n\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chUserDefinedName);
}
else if (pstMVDevInfo->nTLayerType == MV_GENTL_GIGE_DEVICE)
{
printf("UserDefinedName: %s\n", pstMVDevInfo->SpecialInfo.stGigEInfo.chUserDefinedName);
printf("Serial Number: %s\n", pstMVDevInfo->SpecialInfo.stGigEInfo.chSerialNumber);
printf("Model Name: %s\n\n", pstMVDevInfo->SpecialInfo.stGigEInfo.chModelName);
}
else if (pstMVDevInfo->nTLayerType == MV_GENTL_CAMERALINK_DEVICE)
{
printf("UserDefinedName: %s\n", pstMVDevInfo->SpecialInfo.stCMLInfo.chUserDefinedName);
printf("Serial Number: %s\n", pstMVDevInfo->SpecialInfo.stCMLInfo.chSerialNumber);
printf("Model Name: %s\n\n", pstMVDevInfo->SpecialInfo.stCMLInfo.chModelName);
}
else if (pstMVDevInfo->nTLayerType == MV_GENTL_CXP_DEVICE)
{
printf("UserDefinedName: %s\n", pstMVDevInfo->SpecialInfo.stCXPInfo.chUserDefinedName);
printf("Serial Number: %s\n", pstMVDevInfo->SpecialInfo.stCXPInfo.chSerialNumber);
printf("Model Name: %s\n\n", pstMVDevInfo->SpecialInfo.stCXPInfo.chModelName);
}
else if (pstMVDevInfo->nTLayerType == MV_GENTL_XOF_DEVICE)
{
printf("UserDefinedName: %s\n", pstMVDevInfo->SpecialInfo.stXoFInfo.chUserDefinedName);
printf("Serial Number: %s\n", pstMVDevInfo->SpecialInfo.stXoFInfo.chSerialNumber);
printf("Model Name: %s\n\n", pstMVDevInfo->SpecialInfo.stXoFInfo.chModelName);
}
else
{
printf("Not support.\n");
}
return true;
}
int main()
{
int nRet = MV_OK;
void* handle = NULL;
unsigned char *pDataForRGB = NULL;
unsigned char *pDataForSaveImage = NULL;
do
{
// Initialize SDK.
nRet = MV_CC_Initialize();
if (MV_OK != nRet)
{
printf("Initialize SDK fail! nRet [0x%x]\n", nRet);
break;
}
MV_CC_DEVICE_INFO_LIST stDeviceList;
memset(&stDeviceList, 0, sizeof(MV_CC_DEVICE_INFO_LIST));
// Enumerate devices
if (MV_OK != nRet)
{
printf("MV_CC_EnumDevices fail! nRet [%x]\n", nRet);
break;
}
if (stDeviceList.nDeviceNum > 0)
{
for (int i = 0; i < stDeviceList.nDeviceNum; i++)
{
printf("[device %d]:\n", i);
MV_CC_DEVICE_INFO* pDeviceInfo = stDeviceList.pDeviceInfo[i];
if (NULL == pDeviceInfo)
{
break;
}
PrintDeviceInfo(pDeviceInfo);
}
}
else
{
printf("Find No Devices!\n");
break;
}
printf("Please Intput camera index: ");
unsigned int nIndex = 0;
scanf("%d", &nIndex);
if (nIndex >= stDeviceList.nDeviceNum)
{
printf("Intput error!\n");
break;
}
// Select device and create handle
nRet = MV_CC_CreateHandle(&handle, stDeviceList.pDeviceInfo[nIndex]);
if (MV_OK != nRet)
{
printf("MV_CC_CreateHandle fail! nRet [%x]\n", nRet);
break;
}
// Open device
nRet = MV_CC_OpenDevice(handle);
if (MV_OK != nRet)
{
printf("MV_CC_OpenDevice fail! nRet [%x]\n", nRet);
break;
}
// Detect network optimal package size (only works for the GigE cameras)
if (stDeviceList.pDeviceInfo[nIndex]->nTLayerType == MV_GIGE_DEVICE)
{
int nPacketSize = MV_CC_GetOptimalPacketSize(handle);
if (nPacketSize > 0)
{
nRet = MV_CC_SetIntValueEx(handle,"GevSCPSPacketSize",nPacketSize);
if(nRet != MV_OK)
{
printf("Warning: Set Packet Size fail nRet [0x%x]!\n", nRet);
}
}
else
{
printf("Warning: Get Packet Size fail nRet [0x%x]!\n", nPacketSize);
}
}
nRet = MV_CC_SetEnumValue(handle, "TriggerMode", 0);
if (MV_OK != nRet)
{
printf("MV_CC_SetTriggerMode fail! nRet [%x]\n", nRet);
break;
}
// Get payload size
MVCC_INTVALUE stParam;
memset(&stParam, 0, sizeof(MVCC_INTVALUE));
nRet = MV_CC_GetIntValue(handle, "PayloadSize", &stParam);
if (MV_OK != nRet)
{
printf("Get PayloadSize fail! nRet [0x%x]\n", nRet);
break;
}
// Start image acquisition
nRet = MV_CC_StartGrabbing(handle);
if (MV_OK != nRet)
{
printf("MV_CC_StartGrabbing fail! nRet [%x]\n", nRet);
break;
}
MV_FRAME_OUT stImageInfo = {0};
memset(&stImageInfo, 0, sizeof(MV_FRAME_OUT));
nRet = MV_CC_GetImageBuffer(handle, &stImageInfo, 1000);
if (nRet == MV_OK)
{
printf("Now you GetOneFrame, Width[%d], Height[%d], nFrameNum[%d]\n\n",
stImageInfo.stFrameInfo.nExtendWidth, stImageInfo.stFrameInfo.nExtendHeight, stImageInfo.stFrameInfo.nFrameNum);
// Image processing
printf(" 0 to do nothing\n");
printf(" 1 to convert RGB\n");
printf(" 2 to save as BMP\n");
printf("Please Input Index: ");
int nInput = 0;
scanf("%d", &nInput);
switch (nInput)
{
// Do nothing and go on
case 0:
{
break;
}
// Convert image format to RGB. User can convert to other format as needed.
case 1:
{
// Set the interpolation method of Bayer format
nRet = MV_CC_SetBayerCvtQuality(handle, 1);
if (MV_OK != nRet)
{
printf("MV_CC_SetBayerCvtQuality fail! nRet [%x]\n", nRet);
break;
}
pDataForRGB = (unsigned char*)malloc(stImageInfo.stFrameInfo.nExtendWidth * stImageInfo.stFrameInfo.nExtendHeight * 4 + 2048);
if (NULL == pDataForRGB)
{
printf("pDataForRGB is null\n");
break;
}
// Convert pixel format
MV_CC_PIXEL_CONVERT_PARAM stConvertParam = {0};
// From top to bottom are: image width, image height, input data buffer, input data size, source pixel format,
// destination pixel format, output data buffer, provided output buffer size
stConvertParam.nWidth = stImageInfo.stFrameInfo.nExtendWidth;
stConvertParam.nHeight = stImageInfo.stFrameInfo.nExtendHeight;
stConvertParam.pSrcData = stImageInfo.pBufAddr;
stConvertParam.nSrcDataLen = stImageInfo.stFrameInfo.nFrameLenEx;
stConvertParam.enSrcPixelType = stImageInfo.stFrameInfo.enPixelType;
stConvertParam.enDstPixelType = PixelType_Gvsp_RGB8_Packed;
stConvertParam.pDstBuffer = pDataForRGB;
stConvertParam.nDstBufferSize = stImageInfo.stFrameInfo.nExtendWidth * stImageInfo.stFrameInfo.nExtendHeight * 4 + 2048;
nRet = MV_CC_ConvertPixelType(handle, &stConvertParam);
if (MV_OK != nRet)
{
printf("MV_CC_ConvertPixelType fail! nRet [%x]\n", nRet);
break;
}
FILE* fp = fopen("AfterConvert_RGB.raw", "wb");
if (NULL == fp)
{
printf("fopen failed\n");
break;
}
fwrite(pDataForRGB, 1, stConvertParam.nDstLen, fp);
fclose(fp);
printf("convert succeed\n");
break;
}
case 2:
{
pDataForSaveImage = (unsigned char*)malloc(stImageInfo.stFrameInfo.nExtendWidth * stImageInfo.stFrameInfo.nExtendHeight * 4 + 2048);
if (NULL == pDataForSaveImage)
{
printf("pDataForSaveImage is null\n");
break;
}
// Fill in the parameters of saving image
MV_SAVE_IMAGE_PARAM_EX stSaveParam;
memset(&stSaveParam, 0, sizeof(MV_SAVE_IMAGE_PARAM_EX));
// From top to bottom are: output image format, source pixel format, provided output buffer size, image width
// image height, input data buffer, output data buffer, JPG encoding quality
stSaveParam.enImageType = MV_Image_Bmp;
stSaveParam.enPixelType = stImageInfo.stFrameInfo.enPixelType;
stSaveParam.nBufferSize = stImageInfo.stFrameInfo.nExtendWidth * stImageInfo.stFrameInfo.nExtendHeight * 4 + 2048;
stSaveParam.nWidth = stImageInfo.stFrameInfo.nExtendWidth;
stSaveParam.nHeight = stImageInfo.stFrameInfo.nExtendHeight;
stSaveParam.pData = stImageInfo.pBufAddr;
stSaveParam.nDataLen = stImageInfo.stFrameInfo.nFrameLenEx;
stSaveParam.pImageBuffer = pDataForSaveImage;
stSaveParam.iMethodValue = 1;
nRet = MV_CC_SaveImageEx2(handle, &stSaveParam);
if(MV_OK != nRet)
{
printf("failed in MV_CC_SaveImage,nRet[%x]\n", nRet);
break;
}
FILE* fp = fopen("image.bmp", "wb");
if (NULL == fp)
{
printf("fopen failed\n");
break;
}
fwrite(pDataForSaveImage, 1, stSaveParam.nImageLen, fp);
fclose(fp);
printf("save image succeed\n");
break;
}
default:
break;
}
MV_CC_FreeImageBuffer(handle, &stImageInfo);
}
else
{
printf("No data[%x]\n", nRet);
}
// Stop image acquisition
nRet = MV_CC_StopGrabbing(handle);
if (MV_OK != nRet)
{
printf("MV_CC_StopGrabbing fail! nRet [%x]\n", nRet);
break;
}
// Destroy handle
nRet = MV_CC_DestroyHandle(handle);
if (MV_OK != nRet)
{
printf("MV_CC_DestroyHandle fail! nRet [%x]\n", nRet);
break;
}
handle = NULL;
} while (0);
if (handle != NULL)
{
handle = NULL;
}
if (pDataForRGB)
{
free(pDataForRGB);
pDataForRGB = NULL;
}
if (pDataForSaveImage)
{
free(pDataForSaveImage);
pDataForSaveImage = NULL;
}
// Finalize SDK
PressEnterToExit();
printf("exit.\n");
return 0;
}