海思平台的前景提取实现

代码的主要思路是 上上帧 和 上一帧 相减得到 差值图像1,上一帧 和 当前帧 相减得到 差值图像2。差值图像1 与 2 做运算得到前景图像。

/************************************************************ FileName: sample_ive_sfd.c Author: Merz Description: frame subtract to extract the foreground image Version: 1.0 Function List: 1. SAMPLE_IVE_Sub 2. SAMPLE_IVE_Thresh 3. SAMPLE_IVE_Dilate 4. SAMPLE_IVE_And History: <author> <time> <version> <desc> Merz 19/11/4 1.0 build this file Merz 19/11/6 1.0 complete four functions ***********************************************************/ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #include <fcntl.h> #include <sys/mman.h> #include <sys/stat.h> #include <sys/types.h> #include <unistd.h> #include <signal.h> #include <semaphore.h> #include <pthread.h> #include <sys/prctl.h> #include <time.h> #include “sample_comm_ive.h” #define SAMPLE_IVE_SFD_IMAGE_NUM 2 //three frame subtract typedef struct hiSAMPLE_IVE_SFD_S { IVE_SRC_IMAGE_S astSrc[SAMPLE_IVE_SFD_IMAGE_NUM]; //input frame IVE_SRC_IMAGE_S stImg[SAMPLE_IVE_SFD_IMAGE_NUM]; //diff img IVE_DST_IMAGE_S stDst; //output frame FILE* pFpSrc; //read from pFpSrc FILE* pFpDst; //write to pFpDst }SAMPLE_IVE_SFD_S; static SAMPLE_IVE_SFD_S s_stSfd; static HI_VOID SAMPLE_IVE_Sfd_Uninit(SAMPLE_IVE_SFD_S *pstSfd) { HI_U16 i; for (i = 0; i < 2; i++) { IVE_MMZ_FREE(pstSfd->astSrc[i].au64PhyAddr[0], pstSfd->astSrc[i].au64VirAddr[0]); IVE_MMZ_FREE(pstSfd->stImg[i].au64PhyAddr[0], pstSfd->stImg[i].au64VirAddr[0]); } IVE_MMZ_FREE(pstSfd->stDst.au64PhyAddr[0], pstSfd->stDst.au64VirAddr[0]); IVE_CLOSE_FILE(pstSfd->pFpSrc); IVE_CLOSE_FILE(pstSfd->pFpDst); } static HI_S32 SAMPLE_IVE_Sfd_Init(SAMPLE_IVE_SFD_S *pstSfd, HI_U32 u32Width, HI_U32 u32Height, HI_CHAR *pchSrcFileName, HI_CHAR *pchDstFileName) { HI_S32 s32Ret = HI_SUCCESS; HI_U16 i; memset(pstSfd, 0, sizeof(SAMPLE_IVE_SFD_S)); for (i = 0; i < 2; i++) { s32Ret = SAMPLE_COMM_IVE_CreateImage(&(pstSfd->astSrc[i]), IVE_IMAGE_TYPE_U8C1, u32Width, u32Height); SAMPLE_CHECK_EXPR_GOTO(HI_SUCCESS != s32Ret, SFD_INIT_FAIL, “Error(%#x),Create src[%d] image failed!\n”, s32Ret, i); s32Ret = SAMPLE_COMM_IVE_CreateImage(&(pstSfd->stImg[i]), IVE_IMAGE_TYPE_U8C1, u32Width, u32Height); SAMPLE_CHECK_EXPR_GOTO(HI_SUCCESS != s32Ret, SFD_INIT_FAIL, “Error(%#x),Create stImg[%d] image failed!\n”, s32Ret, i); } s32Ret = SAMPLE_COMM_IVE_CreateImage(&(pstSfd->stDst), IVE_IMAGE_TYPE_U8C1, u32Width, u32Height); SAMPLE_CHECK_EXPR_GOTO(HI_SUCCESS != s32Ret, SFD_INIT_FAIL, “Error(%#x),Create stDst image failed!\n”, s32Ret); s32Ret = HI_FAILURE; pstSfd->pFpSrc = fopen(pchSrcFileName, “rb”); SAMPLE_CHECK_EXPR_GOTO(NULL == pstSfd->pFpSrc, SFD_INIT_FAIL, “Error,Open file %s failed!\n”, pchSrcFileName); pstSfd->pFpDst = fopen(pchDstFileName, “wb”); SAMPLE_CHECK_EXPR_GOTO(NULL == pstSfd->pFpDst, SFD_INIT_FAIL, “Error,Open file %s failed!\n”, pchDstFileName); s32Ret = HI_SUCCESS; SFD_INIT_FAIL: if (HI_SUCCESS != s32Ret) { SAMPLE_IVE_Sfd_Uninit(pstSfd); } return s32Ret; } /************************************************* Function: SAMPLE_IVE_Sub Description: create two gray image subtraction task Input: pstCurImg: type(U8C1) size(64×64~1920×1080) pstLastImg: type(U8C1) size(64×64~1920×1080) Output: pstDiffImg: type(U8C1) size(64×64~1920×1080) Return: s32Ret Others: *************************************************/ static HI_S32 SAMPLE_IVE_Sub(IVE_SRC_IMAGE_S *pstCurImg, IVE_SRC_IMAGE_S *pstLastImg, IVE_DST_IMAGE_S *pstDiffImg) { HI_S32 s32Ret; IVE_HANDLE IveHandle; IVE_SUB_CTRL_S stSubCtrl; memset(&stSubCtrl, 0, sizeof(stSubCtrl)); stSubCtrl.enMode = IVE_SUB_MODE_ABS; s32Ret = HI_MPI_IVE_Sub(&IveHandle, pstCurImg, pstLastImg, pstDiffImg, &stSubCtrl, HI_FALSE); SAMPLE_CHECK_EXPR_RET(HI_SUCCESS != s32Ret,HI_FALSE,”Error(%#x),HI_MPI_IVE_Sub failed!\n”,s32Ret); return s32Ret; } /************************************************* Function: SAMPLE_IVE_Thresh Description: create a gray image threshold tasks Input: pstInImg: type(U8C1) size(64×64~1920×1080) Output: pstOutImg: type(U8C1) size(64×64~1920×1080) Return: s32Ret Others: *************************************************/ static HI_S32 SAMPLE_IVE_Thresh(IVE_SRC_IMAGE_S *pstInImg, IVE_DST_IMAGE_S *pstOutImg) { HI_S32 s32Ret; IVE_HANDLE IveHandle; IVE_THRESH_CTRL_S stCtrl; memset(&stCtrl, 0, sizeof(stCtrl)); stCtrl.enMode = IVE_THRESH_MODE_BINARY; stCtrl.u8MinVal = 0; stCtrl.u8MaxVal = 255; stCtrl.u8LowThr = 35; s32Ret = HI_MPI_IVE_Thresh(&IveHandle, pstInImg, pstOutImg, &stCtrl, HI_FALSE); SAMPLE_CHECK_EXPR_RET(HI_SUCCESS != s32Ret,s32Ret,”Error(%#x),HI_MPI_IVE_Thresh failed!\n”,s32Ret); return s32Ret; } /************************************************* Function: SAMPLE_IVE_Dilate Description: create a binary image 5×5 template dilate tasks Input: pstInImg: type(U8C1) size(64×64~1920×1024) Output: pstOutImg: type(U8C1) size(64×64~1920×1024) Return: s32Ret Others: template coefficient can only to 0 or 255 *************************************************/ static HI_S32 SAMPLE_IVE_Dilate(IVE_SRC_IMAGE_S *pstInImg, IVE_DST_IMAGE_S *pstOutImg) { HI_S32 s32Ret; IVE_HANDLE IveHandle; IVE_DILATE_CTRL_S stDilateCtrl; memset(&stDilateCtrl, 0, sizeof(stDilateCtrl)); memset(&stDilateCtrl.au8Mask, 255, 25); s32Ret = HI_MPI_IVE_Dilate(&IveHandle, pstInImg, pstOutImg, &stDilateCtrl, HI_FALSE); SAMPLE_CHECK_EXPR_RET(HI_SUCCESS != s32Ret,s32Ret,”Error(%#x),HI_MPI_IVE_Dilate failed!\n”,s32Ret); return s32Ret; } /************************************************* Function: SAMPLE_IVE_And Description: create two binary image add tasks Input: stImg1: type(U8C1) size(64×64~1920×1080) stImg2: type(U8C1) size(64×64~1920×1080) Output: stDst: type(U8C1) size(64×64~1920×1080) Return: s32Ret Others: *************************************************/ static HI_S32 SAMPLE_IVE_And(IVE_IMAGE_S *stImg1, IVE_IMAGE_S *stImg2, IVE_DST_IMAGE_S *stDst) { HI_S32 s32Ret; IVE_HANDLE IveHandle; s32Ret = HI_MPI_IVE_And(&IveHandle, stImg1, stImg2, stDst, HI_FALSE); SAMPLE_CHECK_EXPR_RET(HI_SUCCESS != s32Ret,s32Ret,”Error(%#x),HI_MPI_IVE_And failed!\n”,s32Ret); return s32Ret; } static HI_S32 SAMPLE_IVE_SfdProc(SAMPLE_IVE_SFD_S *pstSfd) { HI_S32 s32Ret = HI_SUCCESS; HI_U32 u32FrmNum; HI_S32 s32CurIdx = 0; HI_U32 u32TotalFrm = 200; HI_BOOL TimeFlag = HI_TRUE; clock_t start, finish; double totaltime; for (u32FrmNum = 1; u32FrmNum < u32TotalFrm; u32FrmNum++) { SAMPLE_PRT(“Proc Frame %d/%d\n”,u32FrmNum,u32TotalFrm); s32Ret = SAMPLE_COMM_IVE_ReadFile(&(pstSfd->astSrc[s32CurIdx]), pstSfd->pFpSrc); SAMPLE_CHECK_EXPR_RET(HI_SUCCESS != s32Ret,s32Ret,”Error(%#x),Read src1 file failed!\n”,s32Ret); if (u32FrmNum > 1) { if(TimeFlag = HI_TRUE) { start = clock(); TimeFlag = HI_FALSE; } s32Ret = SAMPLE_IVE_Sub(&(pstSfd->astSrc[s32CurIdx]), &(pstSfd->astSrc[1 – s32CurIdx]), &(pstSfd->stImg[s32CurIdx])); s32Ret = SAMPLE_IVE_Thresh(&(pstSfd->stImg[s32CurIdx]), &(pstSfd->stImg[s32CurIdx])); s32Ret = SAMPLE_IVE_Dilate(&(pstSfd->stImg[s32CurIdx]), &(pstSfd->stImg[s32CurIdx])); if(u32FrmNum > 2) { s32Ret = SAMPLE_IVE_And(&(pstSfd->stImg[s32CurIdx]), &(pstSfd->stImg[1 – s32CurIdx]), &pstSfd->stDst); finish = clock(); totaltime = (double)(finish – start) / CLOCKS_PER_SEC; printf(“Time passed in seconds: %f\n”, totaltime); TimeFlag = HI_TRUE; s32Ret = SAMPLE_COMM_IVE_WriteFile(&pstSfd->stDst, pstSfd->pFpDst); } } s32CurIdx = 1 – s32CurIdx; //change cur image to last image } return s32Ret; } HI_VOID SAMPLE_IVE_Sfd(HI_VOID) { HI_S32 s32Ret = HI_SUCCESS; HI_U32 u32Width = 1280; HI_U32 u32Height = 720; HI_CHAR pchSrcFileName[IVE_FILE_NAME_LEN]; HI_CHAR pchDstFileName[IVE_FILE_NAME_LEN]; snprintf(pchSrcFileName, sizeof(pchSrcFileName), “./data/input/img/smoke_720p.yuv”); snprintf(pchDstFileName, sizeof(pchDstFileName), “./data/output/sfd/output.yuv”); memset(&s_stSfd,0,sizeof(s_stSfd)); SAMPLE_COMM_IVE_CheckIveMpiInit(); s32Ret = SAMPLE_IVE_Sfd_Init(&s_stSfd, u32Width, u32Height, pchSrcFileName, pchDstFileName); SAMPLE_CHECK_EXPR_GOTO(HI_SUCCESS != s32Ret, SFD_FAIL, “Error(%#x),SAMPLE_IVE_Sfd_Init failed!\n”, s32Ret); s32Ret = SAMPLE_IVE_SfdProc(&s_stSfd); if (HI_SUCCESS == s32Ret) { SAMPLE_PRT(“Process success!\n”); } SAMPLE_IVE_Sfd_Uninit(&s_stSfd); memset(&s_stSfd,0,sizeof(s_stSfd)); SFD_FAIL: SAMPLE_COMM_IVE_IveMpiExit(); }
<

免责声明:文章内容来自互联网,本站不对其真实性负责,也不承担任何法律责任,如有侵权等情况,请与本站联系删除。
转载请注明出处:海思平台的前景提取实现 https://www.yhzz.com.cn/a/17121.html

上一篇 2023-05-19
下一篇 2023-05-19

相关推荐

联系云恒

在线留言: 我要留言
客服热线:400-600-0310
工作时间:周一至周六,08:30-17:30,节假日休息。