#include "GMM.h"
#include "mpi_sys.h"
#include "hi_comm_sys.h"
#include "sample_assist.h"
#include "sample_file.h"
#include "sample_define.h"
#include "mpi_ive.h"
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include "cv.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mpi_sys.h"
#include"time.h"
//add by ggj for using
#include <iostream>
#include <fstream>
using namespace std;
using namespace cv;
HI_VOID GMMSampleSingle()
{
#if 1
IVE_SRC_IMAGE_S stIveImg; // image
IVE_DST_IMAGE_S stIveFg; //forth ground
IVE_DST_IMAGE_S stIveBg; // back ground
HI_BOOL bInstant = HI_TRUE; // must be true
IVE_GMM_CTRL_S stGMMCtrl;
IVE_HANDLE hIveHandle;
IVE_MEM_INFO_S *pstDst;
IplImage *pimgGray=NULL;
IplImage *pfg=NULL;
IplImage *pfgRGB=NULL;
IplImage *pBgGray=NULL;
HI_S32 s32Result;
HI_U32 FrameNum = 1;
CvSize S;
string buff = "list1.txt";
const char * outputDir = "output";
ifstream infile;
infile.open(buff.c_str());
getline(infile, buff);
IplImage * pimg=cvLoadImage(buff.c_str());
HI_U16 u16Width=pimg->width;
HI_U16 u16Height=pimg->height;
cout<<"u16Width "<<u16Width<<endl;
cout<<"u16Height "<<u16Height<<endl;
if(NULL == pimgGray)
{
printf("coming into pimgGray\n");
S = cvSize(pimg->width,pimg->height);
pimgGray = cvCreateImage(S,8,1);
if(HI_NULL == pimgGray)
{
printf("pimgGray creat error");
goto FAILURE;
}
}
// pstDst
pstDst = (IVE_DST_MEM_INFO_S *)malloc(sizeof(IVE_DST_MEM_INFO_S));
HI_CHECK_ET_GOTO(pstDst,HI_NULL,FAILURE);
stGMMCtrl.u0q16BgRatio = 45875;//0.7
stGMMCtrl.u0q16InitWeight = 3277;
stGMMCtrl.u22q10NoiseVar = 225 * 1024;
stGMMCtrl.u22q10MaxVar = 2000 * 1024;
stGMMCtrl.u22q10MinVar = 200 * 1024;
stGMMCtrl.u8q8VarThr = (HI_U16)(256 * 6.25) ; //2.5
stGMMCtrl.u8ModelNum = 3; // should be three model
//stGMMCtrl.u8ShadowVarThr = 16;
// init
memset(&stIveImg, 0, sizeof(IVE_SRC_IMAGE_S));
memset(&stIveFg, 0,sizeof(IVE_DST_IMAGE_S));
memset(&stIveBg, 0, sizeof(IVE_DST_IMAGE_S));
HI_MPI_SYS_Init();
while(getline(infile, buff))
{
pimg=cvLoadImage(buff.c_str());
if (!pimg)
{
cout<<"error input image"<<endl;
break;
}
cvCvtColor(pimg,pimgGray,CV_BGR2GRAY);
cout<<"pimg->nChannels(): "<<pimg->nChannels<<endl;
cout<<"pimgGray->nChannels(): "<<pimgGray->nChannels<<endl;
if(HI_NULL == stIveImg.pu8VirAddr[0])
{
s32Result = HI_CreateIveImage(&stIveImg,IVE_IMAGE_TYPE_U8C1,u16Width,u16Height);
cout<<"s32Result stIveImg HI_CreateIveImage "<<s32Result<<endl; // if s32Result is 0 success
HI_CHECK_ET_GOTO(s32Result,HI_FAILURE,FAILURE);
}
cout<<"stIveImg.u16Height :"<<stIveImg.u16Height<<" stIveImg.u16Stride[0]: "<<stIveImg.u16Stride[0]<<endl;
s32Result =HI_MPI_SYS_MmzAlloc(&stIveImg.u32PhyAddr[0],(HI_VOID**)&stIveImg.pu8VirAddr[0],NULL,HI_NULL,stIveImg.u16Height*stIveImg.u16Stride[0]);
memcpy(stIveImg.pu8VirAddr[0],pimgGray->imageData,pimgGray->width * pimgGray->height);
cout<<"s32Result stIveImg HI_MPI_SYS_MmzAlloc "<<s32Result<<endl;
//foreground
if(HI_NULL == stIveFg.pu8VirAddr[0])
{
printf("coming into foreground\n");
s32Result = HI_CreateIveImage(&stIveFg,IVE_IMAGE_TYPE_U8C1, u16Width,u16Height);
cout<<"s32Result Fg HI_CreateIveImage "<<s32Result<<endl;
HI_CHECK_ET_GOTO(s32Result,HI_FAILURE,FAILURE);
}
s32Result =HI_MPI_SYS_MmzAlloc(&stIveFg.u32PhyAddr[0],(HI_VOID**)&stIveFg.pu8VirAddr[0],NULL,HI_NULL,stIveFg.u16Height*stIveFg.u16Stride[0]);
cout<<"s32Result stIveImg HI_MPI_SYS_MmzAlloc "<<s32Result<<endl;
memcpy(stIveFg.pu8VirAddr[0],pimgGray->imageData, pimgGray->width * pimgGray->height);
// background
if(HI_NULL == stIveBg.pu8VirAddr[0])
{
printf("coming into background\n");
s32Result = HI_CreateIveImage(&stIveBg,IVE_IMAGE_TYPE_U8C1, u16Width,u16Height);
cout<<"s32Result Bg HI_CreateIveImage "<<s32Result<<endl;
HI_CHECK_ET_GOTO(s32Result,HI_FAILURE,FAILURE);
}
//add by ggj 20160909
s32Result =HI_MPI_SYS_MmzAlloc(&stIveBg.u32PhyAddr[0],(HI_VOID**)&stIveBg.pu8VirAddr[0],NULL,HI_NULL,stIveBg.u16Height*stIveBg.u16Stride[0]);
cout<<"s32Result stIveBg HI_MPI_SYS_MmzAlloc "<<s32Result<<endl;
memcpy(stIveBg.pu8VirAddr[0],pimgGray->imageData, pimgGray->width * pimgGray->height);
if(1 == FrameNum)
{
pstDst->pu8VirAddr = (HI_U8 *)malloc(stGMMCtrl.u8ModelNum * 7 * pimg->width * pimg->height);
HI_CHECK_ET_GOTO(s32Result,HI_FAILURE,FAILURE);
memset(pstDst->pu8VirAddr,0,stGMMCtrl.u8ModelNum * 7 * pimg->width * pimg->height);
pstDst->u32PhyAddr = (HI_U32)pstDst->pu8VirAddr;
pstDst->u32Size = stGMMCtrl.u8ModelNum * 7 * pimg->width * pimg->height;
}
if(FrameNum >= 500)
{
stGMMCtrl.u0q16LearnRate = 131; //0.02
}
else
{
//cout<<"FrameNum "<<FrameNum<<endl;
stGMMCtrl.u0q16LearnRate = 65535/FrameNum;
}
time_t start_t,end_t;
start_t = clock();
//cout<<"will get into GMM"<<endl;
cout<<stIveImg.u32PhyAddr[0]<<" "<<"stIveImg.u32PhyAddr[0]"<<endl;
cout<<stIveFg.u32PhyAddr[0]<<" "<<"stIveFg.u32PhyAddr[0]"<<endl;
cout<<stIveBg.u32PhyAddr[0]<<" "<<"stIveBg.u32PhyAddr[0]"<<endl;
s32Result = HI_MPI_IVE_GMM(&hIveHandle,&stIveImg,&stIveFg,&stIveBg,pstDst,&stGMMCtrl,bInstant);
if (HI_SUCCESS != s32Result)
{
printf("s32Result (%#x)\n", s32Result);
}
end_t = clock();
long cost_t = (long)(end_t - start_t);
cout<<"cost_t: "<<cost_t<<endl;
if(HI_SUCCESS != s32Result)
{
goto FAILURE;
}
if(pfg == HI_NULL)
{
S = cvSize(pimg->width,pimg->height);
pfg = cvCreateImage(S,8,1);
if(HI_NULL == pfg)
{
goto FAILURE;
}
}
if(HI_NULL == pfgRGB)
{
S = cvSize(pimg->width,pimg->height);
pfgRGB = cvCreateImage(S,8,3);
if(HI_NULL == pfgRGB)
{
goto FAILURE;
}
}
if(HI_NULL == pBgGray)
{
S = cvSize(pimg->width,pimg->height);
pBgGray = cvCreateImage(S,8,1);
if(HI_NULL == pBgGray)
{
goto FAILURE;
}
}
memcpy(pfg->imageData, stIveFg.pu8VirAddr[0],pimg->width * pimg->height);
memcpy(pBgGray->imageData,stIveBg.pu8VirAddr[0],pimg->height * stIveBg.u16Stride[0]);
cvCvtColor( pfg, pfgRGB, CV_GRAY2BGR );
FrameNum++;
}
FAILURE:
HI_DestroyIveImage(&stIveImg);
HI_DestroyIveImage(&stIveFg);
HI_DestroyIveImage(&stIveBg);
if(HI_NULL != pstDst->pu8VirAddr)
{
free(pstDst->pu8VirAddr);
}
if(HI_NULL != pstDst)
{
free(pstDst);
}
if(HI_NULL != pfgRGB)
cvReleaseImage(&pfgRGB);
if(HI_NULL != pfg)
cvReleaseImage(&pfg);
if(HI_NULL != pBgGray)
cvReleaseImage(&pBgGray);
HI_MPI_SYS_Exit();
return;
#endif
}
输出为:
u16Width 320
u16Height 240
coming into pimgGray
pimg->nChannels<>: 3
pimgGray->nChannels<>: 1
s32Result stIveImg HI_CreateIveImage 0
stIveImg.u16Height:240
stIveImg.u16Stride[0]:320
s32Result stIveImg HI_MPI_SYS_MmzAlloc 0
coming into foreground
s32Result Fg HI_CreateIveImage 0
s32Result Fg HI_MPI_SYS_MmzAlloc 0
coming into background
s32Result Bg HI_CreateIveImage 0
s32Result Bg HI_MPI_SYS_MmzAlloc 0
2401460224 stIveImg.u32PhyAddr[0]
2401538048 stIveFg.u32PhyAddr[0]
2401615872 stIveBg.u32PhyAddr[0]
pstImage-> u32PhyAddr[0] can not be zero
check image pstSrc failed
check GMM parameters failed.