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

📄 lianma.cpp

📁 对图像进行编码
💻 CPP
字号:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<io.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<math.h>
#define maxh 2000
#define maxw 2000

void Load_Bmp();
void Creat_Bmp();
void lianmabianma();
void lianmajiema();

char sbmpname[50],sbmpfile[50];
short bitperpixel;
int imgx[maxh][maxw],imgy[maxh][maxw],g[maxh][maxw],scanwidth;
long int cc[30000];
long offset,width,height;
long 
int cnum;

void main()
{
	Load_Bmp();
    lianmabianma();
    lianmajiema();
	Creat_Bmp();
	printf("sucessful");
}

void Load_Bmp()
{
	int	i;
	FILE *fp;

	printf("\n input the name of source image bmpfile(without.bmp):");
	scanf("%s",sbmpfile);
	strcpy(sbmpname,sbmpfile);
	strcat(sbmpname,".bmp");

	if((fp=fopen(sbmpname,"rb"))==NULL)
	{
        printf("load figure error:Can't Open Source image...");
		exit(1);
	}
	fseek(fp,10,SEEK_SET); 
	fread(&offset,sizeof(long),1,fp);

	fseek(fp,18,SEEK_SET); 
	fread(&width,sizeof(long),1,fp);
	fread(&height,sizeof(long),1,fp);
	fseek(fp,28,SEEK_SET);
	fread(&bitperpixel,sizeof(short),1,fp);

	if(bitperpixel==8)
		scanwidth=1;
	else
		exit(1);
	fseek(fp,offset,SEEK_SET);

	int k;
	for( k=0; k<height;k++)
	{
		for( i=0; i<width; i++)
		{
            fread(&imgx[k][i], 1, scanwidth, fp);
		}
	}
	fclose(fp);
}

void Creat_Bmp()
{
	char bmpheader[1078+10];
	int i,j; 
	FILE	*fp1,*fp2;

	if((fp1=fopen(sbmpname,"rb"))==NULL)
	{
		printf("Can't Open source bmpfile,error.");
		exit(1);
	}

	fseek(fp1,0,SEEK_SET);
	fread(bmpheader,sizeof(char),offset,fp1);
	fclose(fp1);
	if((fp2=fopen(sbmpname,"wb"))==NULL)
	{
		printf("Can't Create object bmp,error.");
		exit(1);
	}
	fseek(fp2,0,SEEK_SET);
	fwrite(bmpheader,sizeof(char),offset,fp2);
    fseek(fp2,offset,SEEK_SET);
	for(i=0;i<height;i++)
	{
		for(j=0;j<width;j++)
		{
			fwrite(&imgy[i][j],1,1,fp2);
		}
	}
	fclose(fp2);
	printf("Creat Bmp File Successfully.\n");
}


void lianmabianma()
{
	int k,nrn,n[9];
	long int lend;
	int thresh=160,thresh2=180;
	int i,j;
	long int l=0; 
	int l2,i1=0,j1=0;
	i=0;j=0;l2=0;cnum=0;
    
	/*检测出发点*/
	for(i=0;i<height;i++)
	{
		for(j=0;j<width;j++)
		{
			if(imgx[i][j]==255)g[i][j]=0;
			if(imgx[i][j]==0)g[i][j]=1;
		}
	}
step1:
	for(i=1;i<height-1;i++)
	{
		for(j=1;j<width-1;j++)
		{
		    if(g[i][j]==0)continue;
			nrn=g[i-1][j-1]+g[i-1][j]+g[i-1][j+1]+g[i][j-1]+g[i][j+1]+g[i+1][j-1]+g[i+1][j]+g[i+1][j+1];
	/*孤立点检测*/
			if(nrn==0)
			{
				g[i][j]=0;
				l2++;
				goto step1;
			}
	/*端点检测*/
			if(nrn>=1)
			{
				g[i][j]=0;
                cnum=cnum+1;
				cc[l]=long int(i*1000+j);
				l++;l2++;
				goto step2;
			}
		}
	}
	/*是否还存在端点检测*/
	if(l2!=0)
	{
		l2=0;
		goto step1;
	}
   /*因无端点出发点可检测故结束*/
	lend=l;
	cc[l]=-10;
	printf("The number of the lines of this image is:%d,The longer is:%d\n",cnum,l);
	return;

	
step2:		g[i][j]=0;
			n[3]=g[i-1][j-1];n[2]=g[i-1][j];n[1]=g[i-1][j+1];
			n[4]=g[i][j-1];n[8]=g[i][j+1];
			n[5]=g[i+1][j-1];n[6]=g[i+1][j];n[7]=g[i+1][j+1];
			for(k=1;k<=8;k++)
			{
				if(n[k]==0)continue;
				if(k==8)k=0;
				//判断是否是前一个点?否继续移向相邻点
				if(l!=1){if(abs(k-cc[l-1])==4)continue;}
				cc[l]=k;
				l++;l2++;
/* 将3×3窗口移向相邻点*/
				switch(k)
				{
				case 1:i--;j++;break;
				case 2:i--;break;
				case 3:i--;j--;break;
				case 4:j--;break;
				case 5:i++;j--;break;
				case 6:i++;break;
				case 7:i++;j++;break;
				case 0:j++;break;
				}
				if((i<2)||(i>height-2)||(j<2)||(j>width-2))break;
				if((i1==i)&&(j1==j))continue;
				i1=i;
				j1=j;
				goto step2;
			}/*转向下一个出发点*/
			goto step1;
		}

void lianmajiema()
{
	long int l,lmax=10000;
	int i,j;
	int k;
	int s=0;
	int stai[25000],staj[25000],endi[25000],endj[25000];
	int lend[200];
	int arg=0;
	float b[4]={0,0,0,0},d[4]={0,0,0,0};
	float gg,ggg;
	for(i=0;i<height;i++)
	{
		for(j=0;j<width;j++)
		{
			imgy[i][j]=255;
		}
	}
	for(l=0;l<lmax;l++)
	{
		if(cc[l]<0)
		{
			endi[s]=i;endj[s]=j;
//			printf("The %d line's endpoint on:(%d,%d)\n",s,endi[s],endj[s]);
		//	printf("This line longer is %d",lend[s]);
			if((endi[s]-stai[s])!=0)
				{
				 gg=float(endj[s]-staj[s]);
				 ggg=float(endi[s]-stai[s]);
				 gg=gg/ggg;
				 if((gg>=0.0)&&(gg<1.0)){b[0]++;d[0]=d[0]+lend[s];}
				 if(gg>=1.0){b[1]++;d[1]=d[1]+lend[s];}
				 if(gg<=-1.0){b[2]++;d[2]=d[2]+lend[s];}
				 if((-1.0<gg)&&(gg<0.0)){b[3]++;d[3]=d[3]+lend[s];}
//				 printf("*The line xielv=%f*\n\n",gg);
				}
				else{b[1]++;d[1]=d[1]+lend[s];printf("*The line xielv=>>>\n\n");}
				printf("\n%f,%f,%f,%f\n",b[0]/cnum,b[1]/cnum,b[2]/cnum,b[3]/cnum);
				printf("\n%f,%f,%f,%f\n",d[0],d[1],d[2],d[3]); 
			break;
		}
		if(cc[l]>8)
		//端点坐标
		{
			if(s!=0)
			{
				endi[s]=i;endj[s]=j;
//    			printf("endpoint on:(%d,%d)* \n  ",endi[s],endj[s]);
//				printf("This line longer is %d",lend[s]);
//				g=int(arg/360);arg=arg-g*360;
//				printf("\n*The line arg=%d*\n\n",arg);
				if((endi[s]-stai[s])!=0)
				{
				 gg=float(endj[s]-staj[s]);
				 ggg=float(endi[s]-stai[s]);
				 gg=gg/ggg;
				 if((gg>=0.0)&&(gg<1.0)){b[0]++;d[0]=d[0]+lend[s];}
				 if(gg>=1.0){b[1]++;d[1]=d[1]+lend[s];}
				 if(gg<=-1.0){b[2]++;d[2]=d[2]+lend[s];}
				 if((-1.0<gg)&&(gg<0.0)){b[3]++;d[3]=d[3]+lend[s];}
//				 printf("*The line xielv=%f*\n\n",gg);
				}
			    else
				{
					b[1]++;d[1]=d[1]+lend[s];
//	                printf("*The line xielv=>>>\n\n");
				}
				arg=0;
			}
			s++;lend[s]=1;
			i=cc[l]/1000;
			j=cc[l]%1000;
			stai[s]=i;staj[s]=j;
//	        printf("*The %d line's startpoint on:(%d,%d),",s,stai[s],staj[s]);
			imgy[i][j]=0;
			continue;
		}
		k=cc[l];lend[s]++;
		switch(k)
		{
		case 1:i--;j++;imgy[i][j]=0;arg=arg+45;break;
		case 2:i--;imgy[i][j]=0;arg=arg+90;break;
		case 3:i--;j--;imgy[i][j]=0;arg=arg+135;break;
		case 4:j--;imgy[i][j]=0;arg=arg+180;break;
		case 5:i++;j--;imgy[i][j]=0;arg=arg-135;break;
		case 6:i++;imgy[i][j]=0;arg=arg-90;break;
		case 7:i++;j++;imgy[i][j]=0;arg=arg-45;break;
		case 0:j++;imgy[i][j]=0;break;
		}
		if(i>=height)i=height-1;
		if(j>=width)j=width-1;
		
	}
}
/*	for(s=0;s<ss;s++)
	{
		if(lend[s]>max)
		{max=lend[s];k=s+1;}
		printf("The %d line long is:%d\n",s+1,lend[s]);
	}
	i=stai[k];j=staj[k];
	arg=0;
	l=0;l1=0;
step3:		if(cc[l]==(i*1000+j))
			{
step4:			l++;
				k=cc[l];
				if(cc[l]>8)goto step5;
				l1++;
				switch(k)
				{
					case 1:i--;j++;arg=arg+45;break;
					case 2:i--;arg=arg+90;break;
		            case 3:i--;j--;arg=arg+135;break;
		            case 4:j--;arg=arg+180;break;
					case 5:i++;j--;arg=arg-135;break;
					case 6:i++;arg=arg-90;break;
					case 7:i++;j++;arg=arg-45;break;
					case 0:j++;break;
				}
				goto step4;
			}
			else {l++;goto step3;}
step5:	s=int(arg/360);arg=arg-s*360;
		printf("l=%d,The line's arg=%d,\n",l1+1,arg);*/

			

⌨️ 快捷键说明

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