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

📄 omlib.cpp

📁 图像处理软件,功能比较基础
💻 CPP
📖 第 1 页 / 共 5 页
字号:
{
	int temp;	
	int **Temp;
	Temp= new int *[input.row];
	for (int s=0;s<input.row;s++)
	{
		Temp[s]=new int [input.col];
		ZeroMemory(Temp[s],input.col*sizeof(int));
	}
	if (!Temp)
		return FALSE;
	for (int i=1;i<input.row-1;i++)
		for (int j=1;j<input.col-1;j++)
		{
			temp=0;
			for (int k=0;k<3;k++)
		     for (int l=0;l<3;l++)
			 {
				 temp+=input.p[i+k-1][j+l-1]*ker.kernel[k*3+l];
			 }			
			 Temp[i][j]=abs(temp);		
			 output.p[i][j]=(int)Temp[i][j]/fenmu;
		}		
		return TRUE;
}

BOOL Robert(Picture input,Picture output)
{
	int tempx,tempy;	
	int **Temp;
	Temp= new int *[input.row];
	for (int s=0;s<input.row;s++)
	{
		Temp[s]=new int [input.col];
		ZeroMemory(Temp[s],input.col*sizeof(int));
	}
	if (!Temp)
		return FALSE;
	for (int i=0;i<input.row-1;i++)
		for (int j=0;j<input.col-1;j++)
		{
			tempx=0;tempy=0;
			{
				 tempx=input.p[i][j]-input.p[i+1][j+1];
				 tempy=input.p[i][j+1]-input.p[i+1][j];
			 }			
			 Temp[i][j]=abs(tempx)+abs(tempy);		
		}
		normalize(Temp,&input.row,&input.col,output);		
		return TRUE;
}



int  **Radon(Picture input,int *step0,int &cornernumz,int &rhoMax)
{
	unsigned char **pic;
	int row,col;
	float step;
    pic=input.p;
	row=input.row;
	col=input.col;
	step=float(1.0/(float)(*step0));
	int Xb,Ix,Yb,Iy;
	float *x,*y,*theta_sin,*theta_cos;
	int **tempx,**tempy,**tempz;
	int cornernum;
	
	rhoMax=(int)(sqrt((col)*(col)+(row)*(row)));
	x=new float [rhoMax];
	y=new float [rhoMax];
	
	for(int i=0; i<col; i++)
		x[i] = (float)(i-(col)/2.0);
	for(i=0; i<row; i++)
		y[i] = (float)(i-(row)/2.0);
	cornernum=(int)(90/(step));
	cornernumz=2*cornernum;
	theta_sin =new float [cornernum];
	theta_cos =new float [cornernum];
	for (i=0;i<cornernum;i++)
		theta_sin[i]=(float)sin(((float)i)*PI/((float)(2*cornernum)));
	for (i=0;i<cornernum;i++)
		theta_cos[i]=(float)cos(((float)i)*PI/((float)(2*cornernum)));
	
	

	tempx=new int *[cornernum];
	tempy=new int *[cornernum];
	for (i=0;i<cornernum;i++)
	{
		tempx[i]=new int [rhoMax];	
		tempy[i]=new int [rhoMax];	
		ZeroMemory(tempx[i],4*rhoMax);
		ZeroMemory(tempy[i],4*rhoMax);
	}	
    tempz=new int *[cornernumz];
	for (i=0;i<cornernumz;i++)
	{
		tempz[i]=new int [rhoMax];	
		ZeroMemory(tempz[i],4*rhoMax);
	}

	

//====================变换过程================
	
	
	
	for (i=0;i<col;i++)
	{
		for (int j=0;j<row;j++)
		{
			for (int k=0;k<cornernum;k++)
			{
				if (pic[j][i]==0)
				{
					Xb=(int)(x[i]*(theta_cos[k])-y[j]*(theta_sin[k]));//xb对应着col
		        	Ix=Xb+(int)(rhoMax/2.0);
			        Yb=(int)(-x[i]*(theta_sin[k])-y[j]*(theta_cos[k]));//yb对应着row
					Iy=Yb+(int)(rhoMax/2.0);
					tempy[k][rhoMax-Iy-1]++;
					tempx[k][Ix]++;						
				}
			}
		}
	}
	for (i=0;i<cornernum;i++)
		for (int j=0;j<rhoMax;j++)
		{
			tempz[i][j]=tempx[i][j];
		}
    for (i=cornernum;i<cornernumz;i++)
		for (int j=0;j<rhoMax;j++)
		{
			tempz[i][j]=tempy[i-cornernum][rhoMax-j-1];
		}

//==============释放空间=========		
		
		delete [] x;
		delete [] y;
		delete [] theta_sin;
	    delete [] theta_cos;
//需要调试(44过不去)
		for (i=0;i<cornernum;i++)
		{
			delete [] tempx[i];
			delete [] tempy[i];
		}
		delete []tempx;
		delete []tempy;
//=================================
		return tempz;
}


BOOL Fliter_Med(Picture oldpic,Picture dealpic)
{

	int temp[9];	
	int num;
	int max;
	for (int i=1;i<oldpic.row-1;i++)
		for (int j=0;j<oldpic.col-1;j++)
		{
			
			for (int k=0;k<3;k++)			
				for (int l=0;l<3;l++)
				{
					temp[k*3+l]=oldpic.p[i+k-1][j+l-1];
				}
			for (int l=0;l<4;l++)
			{
				max=temp[0];
				num=0;
				for (int m=1;m<9;m++)
				{
					if (max<temp[m])
					{
						max=temp[m];
						num=m;
					}
				}				
					temp[num]=0;
			}
			max=temp[0];			
			for (int m=1;m<9;m++)
				{
					if (max<temp[m])
					{
						max=temp[m];
					}
				}
			dealpic.p[i][j]=max;
		}
}




			




BOOL writepic(CString FileName,Picture input)
{
	FileName.MakeLower();
	if (FileName.Find(".pic")!=-1)
	{
		unsigned char **picture;
		int row,col;
		FILE       *fp;
		unsigned char head_byte[64];
		int type=1;
		picture=input.p;
		col=input.col;
		row=input.row;
		if ((fp=fopen(FileName,"wb"))==NULL)
		{
			AfxMessageBox("写入文件错误!");
			return FALSE;
		}
		for(int i=0;i<64;i++)  head_byte[i] = 0;
		head_byte[0] = 'I';  /* image type. 'I':b/w image, 'C':color image */
		head_byte[1] = 'M';
		head_byte[2] = head_byte[3] = 0;  /* comment space */
		head_byte[4] = col % 256;
		head_byte[5] = col / 256;  /* column size */
		head_byte[6] = row % 256;
		head_byte[7] = row / 256;  /* row size */
		head_byte[8] = head_byte[9] = 0;  /* column start */
		head_byte[10] = head_byte[11] = 0;  /* row start */
		head_byte[12] = head_byte[13] =0;
		head_byte[14] = type % 256;
		head_byte[15] = type / 256;  /* data type. 1:unsigned char, 2:int,   */
		/*            3:float,         4:double */
		fwrite(head_byte,64,1,fp);
		for(int RowNo=0;RowNo<row;RowNo++)
			fwrite(picture[RowNo],1,col,fp);
		fclose(fp);
	}
	else if(FileName.Find(".bmp")!=-1)
	{		
		SaveDIBFile(ChangeToHDIB(input),FileName);
	}		
	else if(FileName.Find(".tif")!=-1)
	{
		char *tempfilename;
		tempfilename = new char [FileName.GetLength()];
		strcpy(tempfilename,FileName);
		OutputTifImageWithName(input.p,input.row,input.col,tempfilename);
		delete []tempfilename;
	}
	else if(FileName.Find(".raw")!=-1)
	{
		FILE       *fp;				
		if ((fp=fopen(FileName,"wb"))==NULL)
		{
			AfxMessageBox("写入文件错误!");
			return FALSE;
		}
		fseek(fp,0,SEEK_SET);
		for(int RowNo=0;RowNo<input.row;RowNo++)
			fwrite(input.p[RowNo],1,input.col,fp);
		fclose(fp);
	}

	return TRUE;
}




void ffree_2d(void **a,int row)
{
  int i;

  for(i=0;i<row;i++) 
	  free(a[i]);
  free(a);
}


BOOL WINAPI WriteBmpHead(BITMAPINFO *BitmapInfo,WORD ColLen,WORD RowLen,WORD BitCount,int nDelta)
{
    DWORD ImgSize;
    int i;

    ImgSize=(DWORD)ColLen*RowLen;
    BitmapInfo->bmiHeader.biSize=40;
    BitmapInfo->bmiHeader.biWidth=ColLen;
    BitmapInfo->bmiHeader.biHeight=RowLen;
    BitmapInfo->bmiHeader.biPlanes=1;
    BitmapInfo->bmiHeader.biBitCount=BitCount;
    BitmapInfo->bmiHeader.biCompression=0;
    BitmapInfo->bmiHeader.biSizeImage=ImgSize;
    BitmapInfo->bmiHeader.biXPelsPerMeter=0xB13;
    BitmapInfo->bmiHeader.biYPelsPerMeter=0xB13;
    BitmapInfo->bmiHeader.biClrUsed=256;
    BitmapInfo->bmiHeader.biClrImportant=0;
    BitmapInfo->bmiHeader.biSizeImage=ColLen*RowLen;
	for(i=0;i<256;i++)
    {
		if((i+nDelta)>=0&&(i+nDelta)<=255)
		{
			BitmapInfo->bmiColors[i].rgbBlue=(BYTE)i+nDelta;
			BitmapInfo->bmiColors[i].rgbGreen=(BYTE)i+nDelta;
			BitmapInfo->bmiColors[i].rgbRed=(BYTE)i+nDelta;
			BitmapInfo->bmiColors[i].rgbReserved=0;
		}
		else if(i+nDelta<0)
		{
			BitmapInfo->bmiColors[i].rgbBlue=0;
			BitmapInfo->bmiColors[i].rgbGreen=0;
			BitmapInfo->bmiColors[i].rgbRed=0;
			BitmapInfo->bmiColors[i].rgbReserved=0;
		}
		else 
		{
			BitmapInfo->bmiColors[i].rgbBlue=255;
			BitmapInfo->bmiColors[i].rgbGreen=255;
			BitmapInfo->bmiColors[i].rgbRed=255;
			BitmapInfo->bmiColors[i].rgbReserved=0;
		}
    }
    return 1;
}

HDIB WINAPI ChangeToHDIB(Picture input,int nDelta)
{
	int HeadBytes;
	int nPixelBytes;
	int Row,Col;
	unsigned char** Image;
	BITMAPINFO* bmfInfo;
	DWORD dwBitsSize,dwSize,dOffset,dwHeadsize;
	HDIB hDIB;
	LPSTR pDIB,lpDIBBits;
	WORD wHeight,wWidth,wNewWidth;
	HeadBytes=0;
	nPixelBytes=8;
	Row=input.row;
	Col=input.col;
	Image = input.p;

	wWidth = Col;
	wHeight = Row;
	
	wNewWidth=(((wWidth*8)+31)/32)*4;
	dwSize=(DWORD)wNewWidth*wHeight;
	dwHeadsize=sizeof(BITMAPINFOHEADER)+256*sizeof(RGBQUAD);
	dwBitsSize=dwHeadsize+dwSize;
	dOffset=dwSize;

	 // Allocate memory for DIB
 
	hDIB = (HDIB) ::GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, dwBitsSize);
	if (hDIB == 0)
	{
		return NULL;
	}
	pDIB = (LPSTR) ::GlobalLock((HGLOBAL) hDIB);
	bmfInfo=(BITMAPINFO*)pDIB;
	WriteBmpHead(bmfInfo,wWidth,wHeight,8,nDelta);
	lpDIBBits=pDIB+dwHeadsize;

  	for(WORD i=0;i<wHeight;i++)
	{
		dOffset-=wNewWidth;
		for(WORD j=0;j<wWidth;j++)
			*(lpDIBBits+dOffset+j)=Image[i][j];
	}
	::GlobalUnlock((HGLOBAL) hDIB);
	return hDIB;
}













reconstruct (int **input,int *cornernum,int *rhoMax,float *step,int *row,int *col,unsigned char **output,int *threshold)
{
	int max;
	int max_thetanum;
	int max_rhoMaxnum;

	for (int y=0;y<1;y++)	
	{   
		max=10000;
		max_thetanum=0;
	    max_rhoMaxnum=0;		
		for (int i=0;i<*cornernum;i++)
			for (int j=0;j<*rhoMax;j++)
			{
				if (input[i][j]!=0)
				{
					if (input[i][j]<max)
					{
						max=input[i][j];
						max_thetanum=i;
						max_rhoMaxnum=j;
					}
				}
			}
	
			int r,c;
			r=10,c=5;
		
			for (int p=0;p<r;p++)
				for (int q=0;q<r;q++)
					if ((max_thetanum+p-c)>=0&&(max_thetanum+p-c)<*cornernum&&(max_rhoMaxnum+q-c)>=0&&(max_rhoMaxnum+q-c)<*rhoMax)
					{
						input[max_thetanum+p-c][max_rhoMaxnum+q-c]=0;
					}		
					max_rhoMaxnum=(int)(max_rhoMaxnum-(*rhoMax)/2);
					invradon (&max_thetanum,&max_rhoMaxnum,step,row,col,output);
					
	}
	return TRUE;
//==========
}






BOOL invradon(int *theta,int *rhoMax,float *step,int *row,int *col,unsigned char **outpic)
{
	int rowy,colx;
	double ta;
    
	ta=PI*(*theta)*(*step)/180.0;
	if (ta<0.0001&&ta>-0.0001)
	{
		colx=(int)(*col/2+(*rhoMax));
		if (colx<*col&&colx>=0)
		{
			for (int RowNum=0;RowNum<*row;RowNum++)
			{
				outpic[RowNum][colx]=255;
			}
		}
	}
	else
	{
		/*
		for (int ColNum=0;ColNum<*col;ColNum++)
		{
			rowy=(int)( -(*rhoMax)/sin(ta)+(ColNum-(*col/2))*cos(ta)/sin(ta) );
			rowy=(int)(rowy+(*row)/2);
			if (rowy<*row&&rowy>=0)
				outpic[rowy][ColNum]=255;				
		}
		*/
		for (int RowNum=0;RowNum<*row;RowNum++)
		{
			colx=(int)((RowNum-(*row/2))*sin(ta)/cos(ta)+(*rhoMax)/cos(ta));
			colx=(int)(colx+(*col)/2);
			if(colx<*col&&colx>=0)
				outpic[RowNum][colx]=255;				
		}
	}
	return TRUE;
}




//===================高斯噪声============================

float GaussNoise( float mean, float deviate)
{
    int        u1,u2;
    float      v1,v2,S;
    double     tmpv;
    static int FirstFlag = 1;

    if ( FirstFlag)
    {
	srand( (unsigned) time(NULL) );	/*  or use srand() function  */
	FirstFlag = 0;
    }

    do
    {
      u1 = rand();
      u2 = rand();
      v1 = 2 * (float)u1 / RAND_MAX - 1;
      v2 = 2 * (float)u2 / RAND_MAX - 1;
      S  = v1 * v1 + v2 * v2;
    } while (S>1);

    tmpv = v1 * sqrt(-2*log(S)/S);
    /*  now tmpv is a random variable of standard narmal N(0,1)  */

    tmpv = tmpv * deviate + mean;

    return (float) tmpv;
}

void ImageAddGuassNoise(Picture input, Picture output,float mean,float deviate)
{
	int row , col;
	unsigned char **img;
	img=input.p;
	row=input.row;
	col=input.col;
	for(int i=0;i<row;i++)
	for(int j=0;j<col;j++)
	{
		float nos1 = img[i][j] + 255 * GaussNoise( mean,deviate);
		int nos =(int) nos1;
		if(nos < 0) output.p[i][j] = 0;
		else if(nos > 255 ) output.p[i][j] = 255;
		else output.p[i][j] = (unsigned char) nos;
	}	
	return ;
}


//===============翻转=====================
BYTE **ImgRotate(Picture input , int& dr, int& dc, int corner , BOOL dir, BYTE nBackgrd )
{
	/*
		BYTE **src;
	src=input.p;
	int sr,sc;
	sr=input.row;
	sc=input.col;
	double a11, a12, a21, a22,theta;
	theta=(corner/180.0*PI);
	a11 =cos(theta);
	a12 = sin(theta);

⌨️ 快捷键说明

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