在上一篇的基础上,将mil获取到的图像数据,用MbufGet2d函数转换为unsigned char类型数据, 然后用halcon算子生成图像并保存。转换代码如下:
全部源码如下:
#include "stdafx.h"
#include <conio.h>
#include <mil.h>
#include "halconcpp/HalconCpp.h"
using namespace HalconCpp;
/* Image scale. */
#define IMAGE_SCALE 1
typedef unsigned char BYTE ;
void main(void)
{
MIL_ID MilApplication;
MIL_ID MilSystem;
MIL_ID MilDigitizerRs170;
MIL_ID MilDigitizerCcir;
MIL_ID MilImageRs170;
MIL_ID MilImageCcir;
MIL_ID MilDisplayRs170;
MIL_ID MilDisplayCcir;
double Time;
long NbGrab = 0;
long BoardType;
HalconCpp::HObject ho_ImageRs,ho_ImageCi,ho_srcImg;
BYTE * us_ImgRs,*us_ImgCi;
long lx,ly;
/* Allocate defaults. */
MappAlloc(M_DEFAULT, &MilApplication);
MsysAlloc(M_SYSTEM_METEOR_II, M_DEV0, M_DEFAULT, &MilSystem);
/* Check if this example is supported on the allocated system. */
BoardType = MsysInquire(MilSystem, M_BOARD_TYPE, M_NULL);
if ( (BoardType != M_METEOR_II) &&
(BoardType != M_METEOR_II_MC) )
{
printf("\nSorry this example is not supported on the allocated system.\n");
MsysFree(MilSystem);
MappFree(MilApplication);
return;
}
printf("Press Enter to begin.\n");
getchar();
/* Allocate multiple digitizers on M_DEV0 with different DCFs.*/
MdigAlloc(MilSystem, M_DEV0, MIL_TEXT("D:\\M_RS170.dcf"), M_DEFAULT, &MilDigitizerRs170);
MdigAlloc(MilSystem, M_DEV0, MIL_TEXT("D:\\M_CCIR.dcf"), M_DEFAULT, &MilDigitizerCcir);
/* A history of all calls to MdigControl, MdigChannel, MdigReference and
MdigHookFunction are maintained for each allocated digitizer. It is
therefore not necessary to redo these calls after a digitizer context
switch, the restore is done automatically by the driver. */
MdigControl(MilDigitizerRs170, M_GRAB_SCALE, IMAGE_SCALE);
/* Enable camera lock, this is done to make sure we are locked on the new
camera signal after a digitizer context switch.
Note: changing M_CAMERA_LOCK_SENSITIVITY, and M_CAMERA_UNLOCK_SENSITIVITY
will affect frame rate performance and grab stability, The user should
be aware that he may need to change these values for his specific camera
combination. A lower value of lock and unlock sentivity gives a higher locking/
unlocking speed but might result in grab sync-lost error messages. */
MdigControl(MilDigitizerRs170, M_CAMERA_LOCK, M_ENABLE);
MdigControl(MilDigitizerRs170, M_CAMERA_LOCK_SENSITIVITY, M_DEFAULT);
MdigControl(MilDigitizerRs170, M_CAMERA_UNLOCK_SENSITIVITY, M_DEFAULT);
MdigChannel(MilDigitizerRs170, M_CH0);
MdigControl(MilDigitizerCcir, M_GRAB_SCALE, IMAGE_SCALE);
MdigControl(MilDigitizerCcir, M_CAMERA_LOCK, M_ENABLE);
MdigControl(MilDigitizerCcir, M_CAMERA_LOCK_SENSITIVITY, M_DEFAULT);
MdigControl(MilDigitizerCcir, M_CAMERA_UNLOCK_SENSITIVITY, M_DEFAULT);
MdigChannel(MilDigitizerCcir, M_CH1);
//alloc
lx = (long)(MdigInquire(MilDigitizerRs170, M_SIZE_X, M_NULL)*IMAGE_SCALE);
ly = (long)(MdigInquire(MilDigitizerRs170, M_SIZE_Y, M_NULL)*IMAGE_SCALE);
us_ImgRs = new BYTE[lx * ly];
lx = (long)(MdigInquire(MilDigitizerCcir, M_SIZE_X, M_NULL)*IMAGE_SCALE);
ly = (long)(MdigInquire(MilDigitizerCcir, M_SIZE_Y, M_NULL)*IMAGE_SCALE);
us_ImgCi = new BYTE[lx * ly];
MbufAlloc2d(MilSystem,
(long)(MdigInquire(MilDigitizerRs170, M_SIZE_X, M_NULL)*IMAGE_SCALE),
(long)(MdigInquire(MilDigitizerRs170, M_SIZE_Y, M_NULL)*IMAGE_SCALE),
8L+M_UNSIGNED,
M_IMAGE+M_GRAB+M_DISP+M_PROC,
&MilImageRs170);
MbufAlloc2d(MilSystem,
(long)(MdigInquire(MilDigitizerCcir, M_SIZE_X, M_NULL)*IMAGE_SCALE),
(long)(MdigInquire(MilDigitizerCcir, M_SIZE_Y, M_NULL)*IMAGE_SCALE),
8L+M_UNSIGNED,
M_IMAGE+M_GRAB+M_DISP+M_PROC,
&MilImageCcir);
MbufClear(MilImageRs170, 128L);
MbufClear(MilImageCcir, 128L);
MdispAlloc(MilSystem, M_DEFAULT, M_DEF_DISPLAY_FORMAT, M_DEFAULT, &MilDisplayRs170);
MdispAlloc(MilSystem, M_DEFAULT, M_DEF_DISPLAY_FORMAT, M_DEFAULT, &MilDisplayCcir);
MdispControl(MilDisplayRs170, M_WINDOW_TITLE_NAME, (long)MIL_TEXT("Rs170 Display"));
MdispControl(MilDisplayCcir, M_WINDOW_TITLE_NAME, (long)MIL_TEXT("Ccir Display"));
MdispSelect(MilDisplayRs170, MilImageRs170);
MdispSelect(MilDisplayCcir, MilImageCcir);
MappTimer(M_TIMER_RESET, M_NULL);
while(!_kbhit())//没有键盘输入
{
/* Here a digitizer context switch is made at every loop:
the new digitizer state is restored along with all controls. */
/* Note: For faster digitizer switching time the user should
create custom DCF's with the channel already selected,
the user should also remove the call to MdigChannel
at the initialisation phase of this example. You can
create these DCF's using Matrox Intellicam. */
if((NbGrab % 2) == 0)
MdigGrab(MilDigitizerRs170, MilImageRs170);
else
MdigGrab(MilDigitizerCcir, MilImageCcir);
NbGrab++;
if((NbGrab % 30) == 0)
{
MappTimer(M_TIMER_READ, &Time);
printf("\r%d\tframes grabbed at %f fps.", NbGrab, 30/Time);
MappTimer(M_TIMER_RESET, M_NULL);
}
}
getchar();
MbufGet2d(MilImageRs170,0,0,
(long)(MdigInquire(MilDigitizerRs170, M_SIZE_X, M_NULL)*IMAGE_SCALE),
(long)(MdigInquire(MilDigitizerRs170, M_SIZE_Y, M_NULL)*IMAGE_SCALE),
us_ImgRs);
MbufGet2d(MilImageCcir,0,0,
(long)(MdigInquire(MilDigitizerCcir, M_SIZE_X, M_NULL)*IMAGE_SCALE),
(long)(MdigInquire(MilDigitizerCcir, M_SIZE_Y, M_NULL)*IMAGE_SCALE),
us_ImgCi);
GenImage1(&ho_ImageRs,"byte",(MdigInquire(MilDigitizerRs170, M_SIZE_X, M_NULL)*IMAGE_SCALE),
(long)(MdigInquire(MilDigitizerRs170, M_SIZE_Y, M_NULL)*IMAGE_SCALE),(Hlong)(us_ImgRs));
HalconCpp::WriteImage(ho_ImageRs,"bmp",0,"D:\\rs170.bmp");
GenImage1(&ho_ImageCi,"byte",(MdigInquire(MilDigitizerCcir, M_SIZE_X, M_NULL)*IMAGE_SCALE),
(long)(MdigInquire(MilDigitizerCcir, M_SIZE_Y, M_NULL)*IMAGE_SCALE),Hlong(us_ImgCi));
HalconCpp::WriteImage(ho_ImageCi,"bmp",0,"D:\\CCIR.bmp");
/* Free all allocated buffers. */
if(NULL != us_ImgCi)
delete []us_ImgCi;
if(NULL != us_ImgRs)
delete []us_ImgRs;
MdispFree(MilDisplayRs170);
MdispFree(MilDisplayCcir);
MdigFree(MilDigitizerRs170);
MdigFree(MilDigitizerCcir);
MbufFree(MilImageRs170);
MbufFree(MilImageCcir);
MsysFree(MilSystem);
MappFree(MilApplication);
return;
}