www.pudn.com > intra.rar > intra.c, change:2008-05-24,size:7646b


#include <stdlib.h> 
#include <string.h> 
#include <stdio.h> 
#include <math.h> 
#include <malloc.h> 
#define imgpel unsigned char 
imgpel   imgY_org_frm1[576][720];//读取前景图象 
imgpel   imgUV_org_frm1[2][288][360]; 
imgpel   imgY_org_frm4[576][720];//读取背景图象 
imgpel   imgUV_org_frm4[2][288][360]; 
imgpel   imgY_org_frm3[576][720];//差值图象(2值化) 
imgpel   imgUV_org_frm3[2][288][360]; 
imgpel   imgY_org_frm2[288][360];//用于中间值转换 
#define  img_width  720//set graphics width and height 
#define  img_height 576 
#define  hang img_width/16//为了计算其宏块16*16 
#define   shu img_height/16 
#define    mb 16        //define 宏块的大小16*16 
#define  many mb*mb/6  //设置阈值来clear mixpoint 
 
void main() 
{ 
	int i,k,h; 
	int y,x; 
	int m,n,sum=0; 
 
  char *temp1 = NULL;//char set null 
  char *temp2 = NULL; 
  char *temp3 = NULL; 
  char *temp4 = NULL; 
  char *temp5 = NULL; 
  char *temp6 = NULL; 
 
   FILE *fp1; 
   FILE *fp2; 
   FILE *fp3; 
fp1 = fopen("E:\\yuv\\book4.yuv", "rb");//open book4.yuv to "rb" 
fp2 = fopen("E:\\yuv\\background.yuv", "rb");//open background.yuv to "rb" 
fp3 = fopen("E:\\yuv\\chazhi.yuv","wb");//差值图象(2值化) 
  if (!fp1) 
     printf("can not open1\n"); 
  if (!fp2) 
     printf("can not open2\n"); 
  if (!fp3) 
     printf("can not open3\n"); 
 /*********************************/ 
  /*分配内存*/ 
  temp4=(unsigned char*)malloc(img_width*img_height*sizeof(unsigned char*)); 
  temp5=(unsigned char*)malloc(img_width/2*img_height/2*sizeof(unsigned char*)); 
  temp6=(unsigned char*)malloc(img_width/2*img_height/2*sizeof(unsigned char*)); 
  temp1=(unsigned char*)malloc(img_width*img_height*sizeof(unsigned char*)); 
  temp2=(unsigned char*)malloc(img_width/2*img_height/2*sizeof(unsigned char*)); 
  temp3=(unsigned char*)malloc(img_width/2*img_height/2*sizeof(unsigned char*)); 
  /********************/ 
  /*main operate*/ 
 
fread(temp4,1,img_height*img_width,fp2);//background.yuv背景图象 
fread(temp5,1,img_height/2*img_width/2,fp2); 
fread(temp6,1,img_height/2*img_width/2,fp2); 
for (y=0; y<img_height; y++)//读入background图象 
	for (x=0; x<img_width; x++) 
		imgY_org_frm4 [y][x] = temp4[y*img_width+x]; 
for (y=0; y<img_height/2; y++) 
	for (x=0; x<img_width/2; x++) 
		{ 
		imgUV_org_frm1[0][y][x] = temp5[y*img_width/2+x]; 
		imgUV_org_frm1[1][y][x] = temp6[y*img_width/2+x]; 
		} 
while(!feof(fp1)) 
{ 
 fread(temp1,1,img_height*img_width,fp1);//读取前景图象book4.yuv 
 fread(temp2,1,img_height/2*img_width/2,fp1); 
 fread(temp3,1,img_height/2*img_width/2,fp1); 
 for (y=0; y<img_height; y++)//读入before图象 
   for (x=0; x<img_width; x++) 
     imgY_org_frm1 [y][x] = temp1[y*img_width+x];//亮度 
for (y=0; y<img_height/2; y++) 
	for (x=0; x<img_width/2; x++) 
		{ 
		imgUV_org_frm1[0][y][x] = temp2[y*img_width/2+x]; 
		imgUV_org_frm1[1][y][x] = temp3[y*img_width/2+x]; 
		} 
 
	  for (y = 0; y < img_height; y++) // 进行差值 
	for (x = 0; x < img_width; x++) 
    imgY_org_frm3[y][x]=abs(imgY_org_frm1[y][x]-imgY_org_frm4 [y][x]); 
/**********************/ 
//确定选中2值图像 
for (y=0; y<img_height; y++) 
{ 
for (x=0; x<img_width; x++) 
	{ 
if(imgY_org_frm3[y][x]>25)imgY_org_frm3[y][x]=255; 
  else imgY_org_frm3[y][x]=0; 
    } 
 }	   
 /**********************/ 
//2值图象进行分块编码 
 
//图象进行填充(块填充) 
for(m=1;m<shu+1;m++) 
{ 
   for(n=1;n<hang+1;n++) 
    { 
     if(sum!=0)sum=0; 
   for(y=(m-1)*mb;y<m*mb;y++) 
		 {   
		for(x=(n-1)*mb;x<n*mb;x++) 
			{ 
				   if(imgY_org_frm3[y][x]==255) sum=sum+1; 
			}  
		 } 
    if(sum<many) 
			{ 
			for(y=(m-1)*mb;y<m*mb;y++) 
				{   
					for(x=(n-1)*mb;x<n*mb;x++) 
					{ 
						if(imgY_org_frm3[y][x]==255) imgY_org_frm3[y][x]=0; 
					}  
				} 
			} 
	 else//图象进行填充(块填充) 
			{ 
			for(y=(m-1)*mb;y<m*mb;y++) 
				{   
					for(x=(n-1)*mb;x<n*mb;x++) 
					{ 
						if(imgY_org_frm3[y][x]==0) imgY_org_frm3[y][x]=255; 
					}  
				} 
			} 
 
    } 
} 
 
/***********************/ 
//图象进行填充(行填充) 
for (y = 0; y < img_height; y++) 
	for (x = 0; x < img_width; x++) 
		{ 
		for(h=x;h<img_width-x;h++) 
			{ 
			if(imgY_org_frm3[y][h]==255)k=h; 
			} 
			if(imgY_org_frm3[y][x]==255&&255==imgY_org_frm3[y][k]) 
			{ 
			for(x=x;x<k;x++) 
				{ 
				if(imgY_org_frm3[y][x]==0) imgY_org_frm3[y][x]=255; 
				} 
             } 
         } 
/*****************************/ 
	for(y=0;y<img_height;y++)//tian chong 
{  
	for(x=0;x<img_width;x++) 
	{ 
        if(imgY_org_frm3[y][x]==255&& 
			(imgY_org_frm3[y+5][x]==255||imgY_org_frm3[y+9][x]==255||imgY_org_frm3[y+15][x]||imgY_org_frm3[y+25][x]||imgY_org_frm3[y+45][x])) 
				{ 
			        imgY_org_frm3[y-1][x]=255; 
					imgY_org_frm3[y-2][x]=255; 
					imgY_org_frm3[y-3][x]=255; 
				//	imgY_org_frm3[y-4][x]=255; 
			        imgY_org_frm3[y+1][x]=255; 
					imgY_org_frm3[y+2][x]=255; 
					imgY_org_frm3[y+3][x]=255; 
				//	imgY_org_frm3[y+4][x]=255; 
				} 
	} 
	} 
/************************/ 
//图象的轮廓扩张 
for(y=0;y<img_height;y++)//tian chong 
{  
	for(x=0;x<img_width;x++) 
	{   
 
        if(imgY_org_frm3[y][x]==255&&imgY_org_frm3[y+5][x]==255) 
				{ 
			        imgY_org_frm3[y-1][x]=255; 
					imgY_org_frm3[y-2][x]=255; 
					imgY_org_frm3[y-3][x]=255; 
					imgY_org_frm3[y-4][x]=255; 
				} 
 
 
		if(imgY_org_frm3[y][x]==255&&imgY_org_frm3[y][x+5]==255) 
				{  
			        imgY_org_frm3[y][x-1]=255; 
					imgY_org_frm3[y][x-2]=255; 
					imgY_org_frm3[y][x-3]=255; 
					imgY_org_frm3[y][x-4]=255; 
				} 
 
 
	 
 
	} 
} 
 
for(y=img_height;y>0;y--)//tian chong 
{  
	for(x=img_width;x>0;x--) 
	{   
 
        if(imgY_org_frm3[y][x]==255&&imgY_org_frm3[y-5][x]==255) 
				{ 
			        imgY_org_frm3[y+1][x]=255; 
					imgY_org_frm3[y+2][x]=255; 
					imgY_org_frm3[y+3][x]=255; 
					imgY_org_frm3[y+4][x]=255; 
				} 
 
 
		if(imgY_org_frm3[y][x]==255&&imgY_org_frm3[y][x-8]==255) 
				{  
			        imgY_org_frm3[y][x+1]=255; 
					imgY_org_frm3[y][x+2]=255; 
					imgY_org_frm3[y][x+3]=255; 
					imgY_org_frm3[y][x+4]=255; 
					imgY_org_frm3[y][x+5]=255; 
					imgY_org_frm3[y][x+6]=255; 
					imgY_org_frm3[y][x+7]=255; 
				} 
 
 
 
	} 
} 
 
 
 
/************************/ 
/*****************************/ 
//恢复选中区域图象 
for (y = 0; y < img_height; y++)//hight light 
{for (x = 0; x < img_width; x++) 
	{if(imgY_org_frm3[y][x]==255)imgY_org_frm1 [y][x]; 
        //else imgY_org_frm1 [y][x]=imgY_org_frm4[y][x]; 
        else imgY_org_frm1 [y][x]=0; 
	} 
} 
/*for (y=0; y<img_height; y+=2)//下采样50% 
	{ 
			for (x=0; x<img_width; x+=2) 
			 { 
				  imgY_org_frm2 [y/2][x/2] = imgY_org_frm1 [y][x]; 
			  } 
		 } 
 
for (y = 0; y < img_height/2; y++) 
{ 
   for (x = 0; x < img_width/2; x++)//问题解决拉 
   {  
	   if(imgY_org_frm2 [y][x]==0) 
    
   { 
 		imgUV_org_frm1[0][y][x]=128; 
	 
		imgUV_org_frm1[1][y][x]=128;  
   } 
   } 
} 
/************************/ 
//显示数据压缩0和1(2值图象) 
/*for (y = 0; y < img_height; y++)//hight light 
{for (x = 0; x < img_width; x++) 
	{if(imgY_org_frm3[y][x]==255) 
		{ 
	     imgY_org_frm3 [y][x]=1; 
         printf(%d,imgY_org_frm3 [y][x]); 
		} 
		 else  
	  printf(%d,imgY_org_frm3 [y][x]) 
	} 
} 
*/ 
/************************/ 
 //输出差值图象(2值化) 
 
for (y = 0; y < img_height; y++) 
    for (x = 0; x < img_width; x++) 
        fputc(imgY_org_frm1 [y][x],fp3); 
for (y = 0; y < img_height/2; y++) 
	for (x = 0; x < img_width/2; x++) 
	//	fputc(imgUV_org_frm1[0][y][x],fp3); 
	fputc(128,fp3); 
for (y = 0; y < img_height/2; y++) 
for (x = 0; x < img_width/2; x++)  
//	fputc(imgUV_org_frm1[1][y][x],fp3); 
   fputc(128,fp3); 
/*****************************/ 
} 
 
 
    fclose(fp1); 
	fclose(fp2); 
	fclose(fp3); 
    
   
}