一 功能
实时显示通过调用halcon函数处理后的图像
二 前期
相机大恒MER-500-7UM,系统win7-64位,编程环境vs2010,软件halcon12.0,基础开发为大恒自带样例SDK包(C:\Program Files\Daheng Imavision\MER-Series\Samples\VC SDK\src\GxSingleCamMono)
三 方法
先说总体思想,直接在SDK开发基础上增加图像处理功能。先将bitmap转成HObject(必须要把bitmap格式转成halcon相关格式,然后才能调用halcon函数库处理图像),然后调用halcon库函数(halcon生成的图像处理cpp代码片段),最后再将处理后的图像格式由HObject改成bitmap,通过大恒自身带的函数显示出来。
1 打开halcon12.0->文件->浏览实例程序->图像分割->background_seg.hdev->文件->导出->C++,HALCON/C++。
2 打开大恒SDK开发包,位置如上文((C:\Program Files\Daheng Imavision\MER-Series\Samples\VC SDK\src\GxSingleCamMono)视安装目录而定)GxSingleCamMono_VS2010。然后把halcon生成的cpp文件代码截取加入到CGxSingleCamMonoDlg.cpp中。代码中加中文注释的部分是我添加的代码(A,bitmap转HObject;B,halcon图像处理;C,HObject转bitmap)
void __stdcall CGxSingleCamMonoDlg::OnFrameCallbackFun(GX_FRAME_CALLBACK_PARAM* pFrame)
{
CGxSingleCamMonoDlg *pDlg = (CGxSingleCamMonoDlg*)(pFrame->pUserParam);
int nImageHeight = (int)pDlg->m_nImageHeight;
int nImageWidth = (int)pDlg->m_nImageWidth;
if (pFrame->status == 0)
{
memcpy(pDlg->m_pBufferRaw,pFrame->pImgBuf,pFrame->nImgSize);
// Show image after flipping image data if mono camera has been used.
for(int i =0;i <nImageHeight;i++)
{
memcpy(pDlg->m_pImageBuffer+i*nImageWidth, pDlg->m_pBufferRaw+(nImageHeight-i-1)*nImageWidth,(size_t)nImageWidth);
}
///bitmap转HObject///
HObject Image;
GenImage1(&Image,"byte",(HTuple)nImageWidth,(HTuple)nImageHeight,(long)pDlg->m_pImageBuffer);
//大恒mer-125-30um相机时,使用(long long)pDlg->m_pImageBuffer;
///halcon图像处理///
HObject ho_EdgeAmplitude,ho_Edges;
SobelAmp(Image, &ho_EdgeAmplitude, "thin_sum_abs", 3);
Threshold(ho_EdgeAmplitude, &ho_Edges, 5, 255);
///HObject转bitmap///
HTuple hv_Pointer;
HTuple hv_Type="byte";
HTuple hv_Width=nImageWidth;
HTuple hv_Height=nImageHeight;
GetImagePointer1(ho_EdgeAmplitude, &hv_Pointer, &hv_Type, &hv_Width, &hv_Height);
BYTE *p=(BYTE *)hv_Pointer[0].L();
int height= (Hlong)hv_Height;
int width = (Hlong)hv_Width;
memcpy(pDlg->m_pImageBuffer,p,width*height*sizeof(BYTE));
pDlg->DrawImg();
if (pDlg->m_bIsSaveImg)
{
pDlg->SaveImage();
}
}
}
四 效果
1 未加代码实时图像如下:
2 加代码实时图像如下:
注意看,有白色边界。
五 参考
1 https://blog.csdn.net/d_a_r_k/article/details/66973582将bitmap转HObject,原文是彩色图,我的是8位灰度图,因此做了些修改。
2 http://www.ihalcon.com/read-2272.html 将HObject转bitmap。