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

📄 dibpro.c

📁 机械工业出版社 Lab Windows/CVI逐步深入与开发实例源代码
💻 C
字号:
#include <analysis.h>
#include "toolbox.h"
#include <ansi_c.h>
#include "dibpro.h"

int Fourier(BYTE* dibBits, int width, int height)
{
	
	BYTE*	pByte;
	
	double	temp;
	
	int	i,j,fw,fh,fwp,fhp;
	
	double *time,*freq,*fifo1,*fifo2;
	
	fw=fh=1;
	fwp=fhp=0;
	
	while(fw*2<=width)
	{   fw*= 2;
		fwp++;
	}
	
	while(fh*2<=height)
	{   fh*=2;
		fhp++;
	}
	
	time=(double *)malloc(sizeof(double)*fw*fh);
	freq=(double *)malloc(sizeof(double)*fw*fh);
	fifo1=(double *)malloc(sizeof(double)*fw*fh);
	fifo2=(double *)malloc(sizeof(double)*fw*fh);
	
	for(i=0; i<fh; i++)
	{
		for(j=0; j<fw; j++)
		{
			pByte = (BYTE*)dibBits + WIDTHBYTES(width) * (height-1-i)+j;
			time[j+fw*i]=*(pByte);
			freq[j+fw*i]=0;
		}
	}
	
	for(i=0;i<fh;i++)
		FFT(time+fw*i,freq+fw*i,fw);
		
	for(i=0;i<fh;i++)
	{
		for(j=0;j<fw;j++)
		{
			fifo1[i+fh*j]=time[j+fw*i];
			fifo2[i+fh*j]=freq[j+fw*i];
		}
	}
	
	for(i=0;i<fh;i++)
	{
		for(j=0;j<fw;j++)
		{
			time[i+fh*j]=fifo1[i+fh*j];
			freq[i+fh*j]=fifo2[i+fh*j];
		}
	}
	
	for(i=0;i<fw;i++)
		FFT (time+i*fh, freq+i*fh, fh);
		
	for(i=0;i<fh;i++)
	{
		for(j=0;j<fw;j++)
		{
			temp = sqrt (freq[j*fh+i]*freq[j*fh+i]+time[j*fh+i]*time[j*fh+i])/ 100;
			if (temp>255)  temp=255;
			pByte=(BYTE*)dibBits+WIDTHBYTES(width)* 
				(height-1-(i<fh/2 ? i+fh/2:i-fh/2))+(j<fw/2 ? j+fw/2:j-fw/2);
			*(pByte)=(BYTE)(temp);
		}
	}
	
	free(fifo1);
	free(fifo2);
	free(time);
	free(freq);
	return 1;
}  

void DCT(double *time, double *freq, int rank)
{
	int	count;
	int		i;
	double	temp;
	double *real,*image;
	
	count = 1<<rank;
	
    real=(double *)malloc(sizeof(double)*count*2);
    image=(double *)malloc(sizeof(double)*count*2);
    
	for(i=0;i<count*2;i++)
	 { real[i]=0;
	   image[i]=0;
	 }
	 
	for(i=0;i<count;i++)
		real[i]=time[i];
		
	FFT(real,image,pow(2,rank+1));
	
	temp = 1/sqrt(count);
	
	freq[0] = real[0] * temp;
	
	temp *= sqrt(2);
	
	for(i = 1; i < count; i++)
	{
		freq[i]=(real[i] * cos(i*PI/(count*2)) +
		  image[i] * sin(i*PI/(count*2))) * temp;
	}
	
	free(real);
	free(image);
} 

void  IDCT(double *freq, double *time, int rank)
{
	int	count;
	int		i;
	double	temp, d;
    double *real,*image;
    
	count = 1<<rank;
	
    real=(double *)malloc(sizeof(double)*count*2);
    image=(double *)malloc(sizeof(double)*count*2);
    
	for(i=0;i<count*2;i++)
	 { 
	   real[i]=0;
	   image[i]=0;
	 }
	 
	for(i=0;i<count;i++)
	{
		real[i]=freq[i] * cos(i*PI/(count*2));
		image[i]=freq[i] * sin(i*PI/(count*2));
	}
	
	InvFFT(real,image,pow(2,rank+1));
	
	temp = sqrt(2.0/count);
	
	d = (sqrt(1.0/count) - temp) * freq[0];
	
	for(i = 0; i < count; i++)
		time[i] = d + real[i]* temp * 2 * count;
		
	free(real);
	free(image);
}

int  DIBDct(BYTE* dibBits, int width, int height)
{
	BYTE*	pByte; 
	double *time,*freq;
	int	i,j,dw,dh,dwp,dhp;
	double	temp;
	
	dw=dh=1;
	dwp=dhp=0;
	
	while(dw*2<=width)
	{
		dw*=2;
		dwp++;
	}
	
	while(dh*2<=height)
	{
		dh*=2;
		dhp++;
	}
	
	time=(double *)malloc(sizeof(double)*dw*dh);
	freq=(double *)malloc(sizeof(double)*dw*dh);
	
	for(i=0;i<dh;i++)
	{
		for(j=0;j<dw;j++)
		{
			pByte=(BYTE*)dibBits+WIDTHBYTES(width)*(height-1-i)+j;
			time[j+i*dw]=*(pByte);
		}
	}
	
	for(i=0;i<dh;i++)
		DCT(&time[dw*i],&freq[dw*i],dwp);
		
	for(i=0;i<dh;i++)
	{
		for(j=0;j<dw;j++)
		{
			time[j*dh+i]=freq[j+dw*i];
		}
	}
	
	for(j=0;j<dw;j++)
		DCT(&time[j*dh],&freq[j*dh],dhp);
		
	for(i=0;i<dh;i++)
	{
		for(j=0;j<dw;j++)
		{
			temp=fabs(freq[j*dh+i]);
			if (temp>255)
				temp=255;
			pByte=(BYTE*)dibBits+WIDTHBYTES(width)*(height-1-i)+j;
			*(pByte)=(BYTE)(temp);
		}
	}
	
	free(time);
	free(freq);
	return 1;
}


void Template(BYTE* dibBits, int width, int height, 
    int tempHeight,int tempWidth,int tempMX,int tempMY,double *array,double coef)
{

	BYTE*	pNewBits;
	BYTE*	pByte1;
	BYTE*	pByte2;
	int	i,j,k,l;
	float	result;
    int progreeDialog;
    
	progreeDialog = CreateProgressDialog ("Calculating",
										   "Percent Complete", 1,
										   VAL_NO_MARKERS, "");
										   
	pNewBits = (BYTE*)malloc (sizeof(BYTE)*WIDTHBYTES(width)*height);
	
	if (pNewBits == NULL)
		return ;
		
    for(i=0;i<WIDTHBYTES(width)*height;i++)
       pNewBits[i]= dibBits[i];
       
	for(i = tempMY; i < height - tempHeight + tempMY + 1; i++)
	{
		for(j = tempMX; j < width - tempWidth + tempMX + 1; j++)
		{
			pByte2 = (BYTE*)pNewBits+WIDTHBYTES(width)*(height-1-i)+j;
			
			result = 0;
			
			for (k=0; k<tempHeight;k++)
			{
				for (l=0; l<tempWidth;l++)
				{
					pByte1=(unsigned char*)dibBits+WIDTHBYTES(width)* 
					       (height-1-i+tempMY-k)+j-tempMX+l;
						
					result+=(*pByte1)*array[k*tempWidth+l];
				
				}
			}
			
			result*=coef;
			
			result=(float)fabs(result);
			
			if(result>255)
				*pByte2=255;
			else
				*pByte2=(unsigned char)(result + 0.5);
		}
		   UpdateProgressDialog (progreeDialog,
								 (int)(100*i/height-tempHeight+tempMY+1),0);
	}
	
    for(i=0;i<WIDTHBYTES(width)*height;i++)
       dibBits[i]= pNewBits[i];
       
    free(pNewBits);
   
    DiscardProgressDialog (progreeDialog);
	return ;

}

void  RandomNoiseDIB (BYTE* dibBits, int width, int height)
{
	BYTE*	pByte;
	int i,j;
	BYTE pixel;
	BYTE NoisePoint;
	
	srand((unsigned)time(NULL));
	
	for (j = 0;j < height ;j++)
	{
		for(i = 0;i < WIDTHBYTES(width) ;i++)
		{
			NoisePoint=rand()/1024;
			
			pByte = (char *)dibBits + WIDTHBYTES(width) * j + i;
			
			pixel = (BYTE)*pByte;
			
			*pByte = (BYTE)(pixel*224/256 + NoisePoint);
			
		}
	}
	return ;
}

void  SaltNoiseDIB (BYTE* dibBits, int width, int height) 
{
	BYTE*	pByte;
	int i,j;
	
	srand((unsigned)time(NULL));
	
	for (j = 0;j < height ;j++)
	{
		for(i = 0;i < WIDTHBYTES(width) ;i++)
		{
			if(rand()>31500)
			{
				pByte = (char *)dibBits + WIDTHBYTES(width) * j + i;
				*pByte = 0;
			}
		}
	}
	return ;
}

⌨️ 快捷键说明

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