⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 newmotiondlg.cpp

📁 机器视觉的程序
💻 CPP
📖 第 1 页 / 共 4 页
字号:
}


/* SCALABILITY: add SNR enhancement layer block data to base layer */
/* ISO/IEC 13818-2 section 7.8.3.4: Addition of coefficients from the two layes */
/*static void Sum_Block(comp)
int comp;
{
  short *Block_Ptr1, *Block_Ptr2;
  int i;

  Block_Ptr1 = base.block[comp];
  Block_Ptr2 = enhan.block[comp];

  for (i=0; i<64; i++)
    *Block_Ptr1++ += *Block_Ptr2++;
}
*/

void CNewmotionDlg::skipped_macroblock(int dc_dct_pred[3], int PMV[2][2][2], int *motion_type, 
  int motion_vertical_field_select[2][2], int *stwtype, int *macroblock_type)

{
  int comp;
  
  /* SCALABILITY: Data Paritioning */
  if (base.scalable_mode==SC_DP)
    ld = &base;

  for (comp=0; comp<block_count; comp++)
    Clear_Block(comp);

  /* reset intra_dc predictors */
  /* ISO/IEC 13818-2 section 7.2.1: DC coefficients in intra blocks */
  dc_dct_pred[0]=dc_dct_pred[1]=dc_dct_pred[2]=0;

  /* reset motion vector predictors */
  /* ISO/IEC 13818-2 section 7.6.3.4: Resetting motion vector predictors */
  if (CodeType==P_TYPE)
    PMV[0][0][0]=PMV[0][0][1]=PMV[1][0][0]=PMV[1][0][1]=0;

  /* derive motion_type */
  if (picture_structure==FRAME_PICTURE)
    *motion_type = MC_FRAME;
  else
  {
    *motion_type = MC_FIELD;

    /* predict from field of same parity */
    /* ISO/IEC 13818-2 section 7.6.6.1 and 7.6.6.3: P field picture and B field
       picture */
    motion_vertical_field_select[0][0]=motion_vertical_field_select[0][1] = 
      (picture_structure==BOTTOM_FIELD);
  }

  /* skipped I are spatial-only predicted, */
  /* skipped P and B are temporal-only predicted */
  /* ISO/IEC 13818-2 section 7.7.6: Skipped macroblocks */
  *stwtype = (CodeType==I_TYPE) ? 8 : 0;

 /* IMPLEMENTATION: clear MACROBLOCK_INTRA */
  *macroblock_type&= ~MACROBLOCK_INTRA;

  /* PMV_out[MB_Num][0]=PMV[0][0][0];
   PMV_out[MB_Num][1]=PMV[0][0][1];
   MB_Num++;
  */
}
/*
void CNewmotionDlg::motion_compensation(int MBA, int macroblock_type, int motion_type, int PMV[2][2][2], 
  int motion_vertical_field_select[2][2], int dmvector[2], int stwtype, int dct_type)

{
  int bx, by;
  int comp;

  /* derive current macroblock position within picture */
  /* ISO/IEC 13818-2 section 6.3.1.6 and 6.3.1.7 */
 /* bx = 16*(MBA%mb_width);
  by = 16*(MBA/mb_width);

  /* motion compensation */
/*  if (!(macroblock_type & MACROBLOCK_INTRA))
    form_predictions(bx,by,macroblock_type,motion_type,PMV,
      motion_vertical_field_select,dmvector,stwtype);
  
 
  /* copy or add block data into picture */
  /*for (comp=0; comp<block_count; comp++)
  {
    
    /* ISO/IEC 13818-2 section Annex A: inverse DCT */
   /* if (Reference_IDCT_Flag)
      Reference_IDCT(ld->block[comp]);
    else
      Fast_IDCT(ld->block[comp]);
    
    /* ISO/IEC 13818-2 section 7.6.8: Adding prediction and coefficient data */
 /*   Add_Block(comp,bx,by,dct_type,(macroblock_type & MACROBLOCK_INTRA)==0);
  }

}*/

void CNewmotionDlg::Sum_Block(int comp)

{
  short *Block_Ptr1, *Block_Ptr2;
  int i;

  Block_Ptr1 = base.block[comp];
  Block_Ptr2 = enhan.block[comp];

  for (i=0; i<64; i++)
    *Block_Ptr1++ += *Block_Ptr2++;
}

 void CNewmotionDlg::Saturate(short *Block_Ptr)

{
  int i, sum, val;

  sum = 0;

  /* ISO/IEC 13818-2 section 7.4.3: Saturation */
  for (i=0; i<64; i++)
  {
    val = Block_Ptr[i];

    if (val>2047)
      val = 2047;
    else if (val<-2048)
      val = -2048;

    Block_Ptr[i] = val;
    sum+= val;
  }

  /* ISO/IEC 13818-2 section 7.4.4: Mismatch control */
  if ((sum&1)==0)
    Block_Ptr[63]^= 1;

}
void CNewmotionDlg::Reference_IDCT(short *block)

{
  int i, j, k, v;
  double partial_product;
  double tmp[64];

  for (i=0; i<8; i++)
    for (j=0; j<8; j++)
    {
      partial_product = 0.0;

      for (k=0; k<8; k++)
        partial_product+= c[k][j]*block[8*i+k];

      tmp[8*i+j] = partial_product;
    }

  /* Transpose operation is integrated into address mapping by switching 
     loop order of i and j */

  for (j=0; j<8; j++)
    for (i=0; i<8; i++)
    {
      partial_product = 0.0;

      for (k=0; k<8; k++)
        partial_product+= c[k][i]*tmp[8*k+j];

      v = (int) floor(partial_product+0.5);
      block[8*i+j] = (v<-256) ? -256 : ((v>255) ? 255 : v);
    }
}

void CNewmotionDlg::Fast_IDCT(short *block)

{
  int i;

  for (i=0; i<8; i++)
    idctrow(block+8*i);

  for (i=0; i<8; i++)
    idctcol(block+i);
}

/* row (horizontal) IDCT
 *
 *           7                       pi         1
 * dst[k] = sum c[l] * src[l] * cos( -- * ( k + - ) * l )
 *          l=0                      8          2
 *
 * where: c[0]    = 128
 *        c[1..7] = 128*sqrt(2)
 */
void CNewmotionDlg::idctrow(short *blk)

{
  int x0, x1, x2, x3, x4, x5, x6, x7, x8;

  /* shortcut */
  if (!((x1 = blk[4]<<11) | (x2 = blk[6]) | (x3 = blk[2]) |
        (x4 = blk[1]) | (x5 = blk[7]) | (x6 = blk[5]) | (x7 = blk[3])))
  {
    blk[0]=blk[1]=blk[2]=blk[3]=blk[4]=blk[5]=blk[6]=blk[7]=blk[0]<<3;
    return;
  }

  x0 = (blk[0]<<11) + 128; /* for proper rounding in the fourth stage */

  /* first stage */
  x8 = W7*(x4+x5);
  x4 = x8 + (W1-W7)*x4;
  x5 = x8 - (W1+W7)*x5;
  x8 = W3*(x6+x7);
  x6 = x8 - (W3-W5)*x6;
  x7 = x8 - (W3+W5)*x7;
  
  /* second stage */
  x8 = x0 + x1;
  x0 -= x1;
  x1 = W6*(x3+x2);
  x2 = x1 - (W2+W6)*x2;
  x3 = x1 + (W2-W6)*x3;
  x1 = x4 + x6;
  x4 -= x6;
  x6 = x5 + x7;
  x5 -= x7;
  
  /* third stage */
  x7 = x8 + x3;
  x8 -= x3;
  x3 = x0 + x2;
  x0 -= x2;
  x2 = (181*(x4+x5)+128)>>8;
  x4 = (181*(x4-x5)+128)>>8;
  
  /* fourth stage */
  blk[0] = (x7+x1)>>8;
  blk[1] = (x3+x2)>>8;
  blk[2] = (x0+x4)>>8;
  blk[3] = (x8+x6)>>8;
  blk[4] = (x8-x6)>>8;
  blk[5] = (x0-x4)>>8;
  blk[6] = (x3-x2)>>8;
  blk[7] = (x7-x1)>>8;
}

/* column (vertical) IDCT
 *
 *             7                         pi         1
 * dst[8*k] = sum c[l] * src[8*l] * cos( -- * ( k + - ) * l )
 *            l=0                        8          2
 *
 * where: c[0]    = 1/1024
 *        c[1..7] = (1/1024)*sqrt(2)
 */
void CNewmotionDlg::idctcol(short *blk)

{
  int x0, x1, x2, x3, x4, x5, x6, x7, x8;

  /* shortcut */
  if (!((x1 = (blk[8*4]<<8)) | (x2 = blk[8*6]) | (x3 = blk[8*2]) |
        (x4 = blk[8*1]) | (x5 = blk[8*7]) | (x6 = blk[8*5]) | (x7 = blk[8*3])))
  {
    blk[8*0]=blk[8*1]=blk[8*2]=blk[8*3]=blk[8*4]=blk[8*5]=blk[8*6]=blk[8*7]=
      iclp[(blk[8*0]+32)>>6];
    return;
  }

  x0 = (blk[8*0]<<8) + 8192;

  /* first stage */
  x8 = W7*(x4+x5) + 4;
  x4 = (x8+(W1-W7)*x4)>>3;
  x5 = (x8-(W1+W7)*x5)>>3;
  x8 = W3*(x6+x7) + 4;
  x6 = (x8-(W3-W5)*x6)>>3;
  x7 = (x8-(W3+W5)*x7)>>3;
  
  /* second stage */
  x8 = x0 + x1;
  x0 -= x1;
  x1 = W6*(x3+x2) + 4;
  x2 = (x1-(W2+W6)*x2)>>3;
  x3 = (x1+(W2-W6)*x3)>>3;
  x1 = x4 + x6;
  x4 -= x6;
  x6 = x5 + x7;
  x5 -= x7;
  
  /* third stage */
  x7 = x8 + x3;
  x8 -= x3;
  x3 = x0 + x2;
  x0 -= x2;
  x2 = (181*(x4+x5)+128)>>8;
  x4 = (181*(x4-x5)+128)>>8;
  
  /* fourth stage */
  blk[8*0] = iclp[(x7+x1)>>14];
  blk[8*1] = iclp[(x3+x2)>>14];
  blk[8*2] = iclp[(x0+x4)>>14];
  blk[8*3] = iclp[(x8+x6)>>14];
  blk[8*4] = iclp[(x8-x6)>>14];
  blk[8*5] = iclp[(x0-x4)>>14];
  blk[8*6] = iclp[(x3-x2)>>14];
  blk[8*7] = iclp[(x7-x1)>>14];
}

void CNewmotionDlg::OnMih() 
{
	// TODO: Add your control notification handler code here
	double mv_mag[1000],angle[1000];
	double mv_sum;
	double mv_sqr=0.0;
	double std_avg=0.0;
	//double var=0.0;
	double avg_angle=0.0;
	double avg[100];
	int ang_num;
	int direction1;
	double std_dev[100],var[100],f_avg_angle[100];
	double t1,t2,t3,t4,l,F;
	//int intensity,
	direction=-1;
	float angles_of_activity[8]={0.,0.,0.,0.,0.,0.,0.,0.};

	for(int i=0;i<1000;i++)
	{
		mv_mag[i]=0.0;
		angle[i]=0;
	}
    for(int k=0;k<100;k++)
	{
		avg[k]=0.0;
		std_dev[k]=0.0;
		var[k]=0.0;
		f_avg_angle[k]=0.0;
	}
	for(int j=0;j<5;j++)
	{
		mih[j]=0;
	}

    switch(Pic_Rate)
	{
	   case 1:
		   F=23.976; break;
	   case 2:
		   F=24;  break;
	   case 3:
		   F=25;  break;
	   case 4:
		   F=29.97; break;
	   case 5:
		   F=30; break;
	   default:
		   break;
	}
    l=sqrt(mb_width*mb_width + mb_height*mb_height);
	t1 = 0.257*l/F;
    t2 = 0.706*l/F;
    t3 = 1.280*l/F;
    t4 = 2.111*l/F;



 for( j=0;j<P_Num;j++)
 {
	 mv_sum=0.0;

	for(i=0; i<mb_width*mb_height; i++) 
	{ 
	  mv_mag[i]=PMV_out[j][i][0]*PMV_out[j][i][0] + PMV_out[j][i][1]*PMV_out[j][i][1];
      mv_mag[i]=sqrt(mv_mag[i]);
	  mv_sum += mv_mag[i];
	 // mv_sqr = mv_mag[i]*mv_mag[i];
	 
	}
    avg[j] = mv_sum/(mb_width*mb_height);//MB_Num;
	for(i=0;i<mb_width*mb_height;i++)

	{
		var[j]+=(avg[j]-mv_mag[i])*(avg[j]-mv_mag[i]);
	}
	var[j]=double (var[j]/(mb_width*mb_height));//MB_Num);
	

  /*  std_dev[j]=sqrt(var[j]);
	if(std_dev[j]<t1)
	    std_dev[j] = 1;
    else if(std_dev[j]<t2)
	    std_dev[j] = 2;
    else if(std_dev[j]<t3)
	    std_dev[j] = 3;
    else if(std_dev[j]<t4)
    	std_dev[j] = 4;
    else
	    std_dev[j] = 5;*/

    if (var[j]<3.9) var[j]=1;
	else if (var[j]< 10.7) var[j]=2;
	else if (var[j]< 17.1) var[j]=3;
	else if (var[j]< 32)   var[j]=4;
	else if (var[j]>=32)   var[j]=5;
	
 }
 for( j=0;j<P_Num;j++)
 {
	 for(i=0;i<5;i++)
	 {
	   if(var[j]==i+1)
		// if(std_dev[j]==i+1)
		 mih[i]++;
	 }

 }

  /*  std_avg/=P_Num;
   
	
*/
	for( j=0;j<P_Num;j++)

	{   
		ang_num=0;
		for (i=0;i<mb_width*mb_height;i++)
		{
          if ((PMV_out[j][i][0] ==0))
		  {
	       angle[i]=90;
	       if (PMV_out[j][i][1]==0) angle[i]=0;

		  }
          else
		  {
	       angle[i]=atan2((double)PMV_out[j][i][1],(double)PMV_out[j][i][0]);
           angle[i]=(float) angle[i]/3.141592*180;
		  }

		  f_avg_angle[j]+=angle[i];
		}
		 
          f_avg_angle[j]=f_avg_angle[j]/MB_Num;//(mb_width*mb_height);

		  if (f_avg_angle[j]<0)
			  f_avg_angle[j]+=360.;

          direction1=activity_quantize_angle(f_avg_angle[j]);

          angles_of_activity[direction1]++;
     		  
		}
        

	float max=0.0;
	for(k=0;k<8;k++)
	{
	  
      angles_of_activity[k]/=P_Num;
	  
	}
    for(k=0;k<8;k++)
	{
		if(angles_of_activity[k]>=max)
		{ max=angles_of_activity[k];
		  direction=k;}
	}
	
	
   
    
    CShowMIH showdlg2;
    showdlg2.m_MIH1=mih[0];
	showdlg2.m_MIH2=mih[1];
	showdlg2.m_MIH3=mih[2];
	showdlg2.m_MIH4=mih[3];
	showdlg2.m_MIH5=mih[4];
	showdlg2.m_MIH6=direction;

    showdlg2.DoModal();
}

int CNewmotionDlg::activity_quantize_angle(float f_angle)
{
	int direc=0;
		/* Quantize angle using uniform 3 bit quantization over
		0-360 degrees i.e. 0,45,90,135,180,225,270,315
	   
	 */
    if((f_angle >=0.) && (f_angle <22.5)) direc=0;
	
	else if ((f_angle >=22.5) &&(f_angle < 67.5)) direc=1;
	else if ((f_angle >=67.5) &&(f_angle < 112.5)) direc=2;
	else if ((f_angle >=112.5) &&(f_angle < 157.5)) direc=3;
	else if ((f_angle >=157.5) &&(f_angle < 202.5)) direc=4;
	else if ((f_angle >=202.5) &&(f_angle < 247.5)) direc=5;
	else if ((f_angle >=247.5) &&(f_angle < 292.5)) direc=6;
	else if ((f_angle >=292.5) &&(f_angle < 337.5)) direc=7;
	else if ((f_angle >=337.5) &&(f_angle < 360.)) direc=0;
	return direc;
}

void CNewmotionDlg::OnSend() 
{
	// TODO: Add your control notification handler code here
	extern CDatabase DBCONN;
	extern 	CString DSN;

	CString tempmih1,tempmih2,tempmih3,tempmih4,tempmih5,tempdirection;
	tempmih1.Format("%d", mih[0]);
	tempmih2.Format("%d", mih[1]);
	tempmih3.Format("%d", mih[2]);
	tempmih4.Format("%d", mih[3]);
	tempmih5.Format("%d", mih[4]);
	tempdirection.Format("%d", direction);
	CString Temp;

	
	DBCONN.Open(NULL,FALSE,FALSE,DSN,FALSE);
	m_rset.m_pDatabase=&DBCONN;//把数据库传给记录集


	m_strsql="select shot from motion1";
	if(m_rset.IsOpen()) m_rset.Close();
	m_rset.Open(CRecordset::forwardOnly,m_strsql);
	while(!m_rset.IsEOF()) {
		m_rset.GetFieldValue("shot",Temp);//读字段
		if(PathName==Temp) 
		{

			MessageBox("该视频已经存在数据库中!");
			m_rset.Close();
			DBCONN.Close();
			return;
		}
		else
			m_rset.MoveNext();		
	}
	m_rset.Close();
	DBCONN.Close();

	DBCONN.Open(NULL,FALSE,FALSE,DSN,FALSE);
		
    m_rset.m_pDatabase=&DBCONN;//把数据库传给记录集
    m_strsql="select shot from motion1";
	if(m_rset.IsOpen()) m_rset.Close();
	m_rset.Open(CRecordset::forwardOnly,m_strsql);

	while(!m_rset.IsEOF()) {
		m_rset.MoveNext();
	}

	m_strsql="insert motion1(shot,mih1,mih2,mih3,mih4,mih5,direction) values(\'"+PathName+"\',"+tempmih1+","+tempmih2+","+tempmih3+","+tempmih4+","+tempmih5+","+tempdirection+")";
  //  MessageBox(m_strsql);
	DBCONN.ExecuteSQL(m_strsql);//更新数据库
	MessageBox("添加到数据库完成!");//更新完毕,提示一下
	
	m_rset.Close();
	DBCONN.Close();	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -