3516camshift实现
static HI_VOID *CYF_Cam_Proc_H(HI_VOID *pArgs)
{
HI_U32 srcWidth = 768;
HI_U32 srcHeight = 576;
HI_U32 srcSize = 768*576;
HI_S32 s32Ret;
VI_FRAME_INFO_S stFrameInfo;
VI_CHN viIveChn = 2;
VENC_CHN vencChn = 0;
IVE_HANDLE IveHandle;
my_CvTermCriteria mycriteria;
mycriteria.epsilon = 1;
mycriteria.max_iter =10;
my_CvBox2D camBox;
HI_BOOL bHistDone = HI_FALSE;
HI_U32 *pImage = NULL;
HI_U8 *pbuf_H;
HI_U8 *pbuf_roiH;
HI_U8 *pNormHist = NULL;
HI_U8 *pBackPrj = NULL;
IVE_SRC_INFO_S stSrcRoi;
IVE_MEM_INFO_S stDstRoi;
IVE_IMAGE_S stHueRoi;
IVE_IMAGE_S stHistRoi;
IVE_MEM_INFO_S stDst;
HI_VOID *pVirtDst;
HI_VOID *pVirtDst2;
CYF_IVE_MD_S pstMd_RGB;
my_CvRect rect_rio;
pbuf_H = (HI_U8*)malloc(srcSize);
memset(pbuf_H,1,srcSize);
pbuf_roiH = (HI_U8*)malloc(srcSize);
memset(pbuf_roiH,1,srcSize);
pNormHist = (HI_U8*)malloc(256);
memset(pNormHist,1,256);
pBackPrj = (HI_U8*)malloc(srcSize);
memset(pBackPrj,1,srcSize);
/******************************** step 1 初始化,分配内存等************************/
s32Ret = HI_MPI_SYS_MmzAlloc_Cached(&pstMd_RGB.stSrc.u32PhyAddr[0], (void**)&pstMd_RGB.stSrc.pu8VirAddr[0],NULL, HI_NULL, srcSize*3);
if(s32Ret==HI_FAILURE)
{
printf("HI_MPI_SYS_MmzAlloc_Cached RGB failed!\n");
return HI_FAILURE;
}
s32Ret = HI_MPI_SYS_MmzAlloc_Cached(&stHueRoi.u32PhyAddr[0], (void**)&stHueRoi.pu8VirAddr[0],NULL, HI_NULL, srcSize);
if(s32Ret==HI_FAILURE)
{
printf("HI_MPI_SYS_MmzAlloc_Cached ROI_H failed!\n");
return ;
}
s32Ret = HI_MPI_SYS_MmzAlloc_Cached(&stHistRoi.u32PhyAddr[0], (void**)&stHistRoi.pu8VirAddr[0],NULL, HI_NULL, 256*4);
if(s32Ret==HI_FAILURE)
{
printf("HI_MPI_SYS_MmzAlloc_Cached Hist failed!\n");
return ;
}
PhyAddr_Free1 =stHueRoi.u32PhyAddr[0];
PhyAddr_Free2 =pstMd_RGB.stSrc.u32PhyAddr[0];
PhyAddr_Free3 = stHistRoi.u32PhyAddr[0];
signal(SIGINT, CYF_CAM_HandleSig);
signal(SIGTERM, CYF_CAM_HandleSig);
/******************************** step 2 设置帧缓存为1帧************************/
if (HI_MPI_VI_SetFrameDepth(viIveChn, 1))
{
printf("HI_MPI_VI_SetFrameDepth err, viIveChn chn %d \n", viIveChn);
return ;
}
usleep(50000);
while (1)
{
if(stCamObj.bisDetect == HI_TRUE)
{
/******************************** step 3 获取帧***********************/
printf("find the CamObj ,start to get frame \n");
s32Ret = HI_MPI_VI_GetFrame(viIveChn, &stFrameInfo);
if (HI_SUCCESS != s32Ret)
{
printf("HI_MPI_VI_GetFrame fail,viIveChn(%d),Error(%#x)\n",viIveChn,s32Ret);
return;
}
else
{
}
/******************************** step 4 调取原始帧的H 通道分量***********************/
/********调用ive将原始帧转换成rgb数据,以BRG_pack顺序存储 *********/
CYF_GetRGB_from_Frame(&stFrameInfo,&pstMd_RGB);
/**********RGB转HS
V 只得到H分量********/
pVirtDst2 = pstMd_RGB.stSrc.pu8VirAddr[0];
cyf_Get_H_from_RGB((uchar*)pVirtDst2,pbuf_H,srcWidth,srcHeight);
if( bHistDone == FALSE)
{
}
if( bHistDone == FALSE)
{
rect_rio.x = stCamObj.stPosition.u16Left+10;
rect_rio.y = stCamObj.stPosition.u16Top+80;
rect_rio.width = (stCamObj.stPosition.u16Right - stCamObj.stPosition.u16Left)/16*16;
rect_rio.height = (stCamObj.stPosition.u16Bottom - stCamObj.stPosition.u16Top)/16*16;
cyf_get_RIO_from_Y(pbuf_roiH, pbuf_H ,srcWidth,srcHeight, rect_rio );
/******************************** step 5 对ROI区域做直方图(调用ive)**********************/
cyf_roi_copyto_ive(&stHueRoi,(HI_U8*)pbuf_roiH,rect_rio.width,rect_rio.height);
printf("cyf_roi_copyto_ive \n");
HI_BOOL bFinish, bBlock;
HI_BOOL bInstant = HI_TRUE;
stSrcRoi.enSrcFmt = IVE_SRC_FMT_SINGLE;
stSrcRoi.stSrcMem.u32PhyAddr = stHueRoi.u32PhyAddr[0];
stSrcRoi.stSrcMem.u32Stride = stHueRoi.u32Width;
stSrcRoi.u32Width = stHueRoi.u32Width;
stSrcRoi.u32Height = stHueRoi.u32Height;
stDstRoi.u32Stride = stHueRoi.u32Width;
stDstRoi.u32PhyAddr = stHistRoi.u32PhyAddr[0];
s32Ret = HI_MPI_IVE_HIST(&IveHandle, &stSrcRoi,&stDstRoi, bInstant);
if (HI_SUCCESS != s32Ret)
{
printf("%s: HI_MPI_IVE_HIST failed!Error(%#x)\n", s32Ret);
return ;
}
printf("HI_MPI_IVE_HIST \n");
s32Ret = HI_MPI_IVE_Query(IveHandle, &bFinish, bBlock);
if(s32Ret != HI_SUCCESS)
{
printf("HI_MPI_IVE_Query fail,Error(%#x)\n",s32Ret);
return;
}
pImage = (HI_U32*)stHistRoi.pu8VirAddr[0];
cyf_Hist_normalize(pNormHist, (unsigned int*)pImage, 0, 255,256);
printf(" *********stCamObj's Hist normalize done ****************** \n");
bHistDone = HI_TRUE;
}
/******************************** step 6 对原始帧的H分量做反向投影**********************/
cyf_calcBackProject(pbuf_H ,pBackPrj , pNormHist ,srcWidth, srcHeight);
printf("cyf_calcBackProject done \n");
/******************************** step 7.camshift调用c接口函数?*********************/
printf("myCamShift2 before rect_rio.x is %d \n",rect_rio.x);
camBox= myCamShift2( pBackPrj,&rect_rio, mycriteria , 768, 576 ) ;
stCamRectShow.x = camBox.center.x - camBox.size.width/2;
stCamRectShow.y = camBox.center.y - camBox.size.height/2 ;
stCamRectShow.height = camBox.size.height;
stCamRectShow.width = camBox.size.width;
printf("cambox center x = done %f\n ",rect_rio.x);
s32Ret = HI_MPI_VI_ReleaseFrame(viIveChn, &stFrameInfo);
if (HI_SUCCESS != s32Ret)
{
printf("HI_MPI_VI_ReleaseFrame fail,viIveChn(%d),Error(%#x)\n",viIveChn,s32Ret);
}
}
usleep(10000);
}