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

📄 combiningspatialenhancement.cpp

📁 matlab版图像数字处理这本书的配套代码
💻 CPP
字号:
#include"stdafx.h"
#include<math.h>
#include"CombiningSpatialEnhancement.h"


void  Laplacian(LPSTR pBits,LPSTR pLaplacian,LONG width,LONG height)
{
	LONG LineBytes=(width*8+31)/32*4;
	int temp,max,min;
	BYTE m_num;
	max=*pBits,min=*pBits;

	int i,j;
	int a[3][3]={{-1,-1,-1},{-1,8,-1},{-1,-1,-1}};
	for(i=0;i<height;i++)
	{
		for(j=0;j<width;j++)
		{
			temp=0;
			if((i==0||i==height-1)||(j==0||j==width-1))
			{
				m_num=*(pBits+LineBytes*(height-1-i)+j);
				temp=(int)m_num;
			}
			else
			{
				int m,n;
				for(m=-1;m<=1;m++)
				{
					for(n=-1;n<=1;n++)
					{
						m_num=*(pBits+LineBytes*(height-1-(i+m))+(j+n));
						temp+=((int)m_num)*a[1+m][1+n];
					}
				}			 						   
			}
			*(pLaplacian+LineBytes*(height-1-i)+j)=unsigned char(temp);
			if(temp<min)
				min=temp;
			if(temp>max)
				max=temp;
		}
	}
	
	max-=min;

	for(i=0;i<height;i++)
	{	
		for(j=0;j<width;j++)
		{
			temp=*(pLaplacian+LineBytes*(height-1-i)+j);
			*(pLaplacian+LineBytes*(height-1-i)+j)=unsigned char(((temp-min)*255)/max);
		}
	}
}


void  LaplacianEnhanced(LPSTR pBits,LPSTR pLaplacianEnhanced,LONG width,LONG height)
{
	LONG i,j;
	LONG LineBytes=(width*8+31)/32*4;
	int temp,max,min;
	BYTE m_num1,m_num2;
	int* pTemp=new int[LineBytes*height];

	
	Laplacian(pBits,pLaplacianEnhanced,width,height);
	m_num1=*pBits;
	m_num2=*pLaplacianEnhanced;
	max=min=(int)(m_num1+m_num2);
	for(i=0;i<height;i++)
	{
		for(j=0;j<width;j++)
		{
			m_num1=*(pBits+LineBytes*(height-1-i)+j);
			m_num2=*(pLaplacianEnhanced+LineBytes*(height-1-i)+j);
			temp=(int)(m_num1+m_num2);
			*(pTemp+LineBytes*(height-1-i)+j)=temp;
			if(temp>max)
				max=temp;
			if(temp<min)
				min=temp;
		}
	}
	max-=min;
		
	for(i=0;i<height;i++)
	{
		for(j=0;j<width;j++)
		{
			temp=*(pTemp+LineBytes*(height-1-i)+j);
			*(pLaplacianEnhanced+LineBytes*(height-1-i)+j)=BYTE((temp-min)*255/max);
		}
	}
	delete[] pTemp;
}


void  Sobel(LPSTR pBits,LPSTR pSobel,LONG width,LONG height)
{
	LONG LineBytes=(width*8+31)/32*4;
	int temp,tempx,tempy,max,min;
	max=min=*pBits;
	int i,j;
	BYTE m_num1,m_num2;
	int sx[3][3]={{-1,-2,-1},{0,0,0},{1,2,1}};
	int sy[3][3]={{-1,0,1},{-2,0,2},{-1,0,1}};
	int* pTemp=new int[LineBytes*height];
	for(i=0;i<height;i++)
	{
		for(j=0;j<width;j++)
		{
			tempx=tempy=0;
			if((i==0||i==height-1)||(j==0||j==width-1))
			{
				m_num1=*(pBits+LineBytes*(height-1-i)+j);
				temp=(int)m_num1;
			}
			else
			{
				int m,n;
				for(m=-1;m<=1;m++)
				{
					for(n=-1;n<=1;n++)
					{
						m_num1=(*(pBits+LineBytes*(height-1-(i+m))+(j+n)));
						m_num2=(*(pBits+LineBytes*(height-1-(i+m))+(j+n)));
						tempx+=((int)m_num1)*sx[1+m][1+n];
						tempy+=((int)m_num2)*sy[1+m][1+n];
					}
				}
				temp=int(abs(tempx)+abs(tempy));
			}
			
			if(temp>max)
				max=temp;
			if(temp<min)
				min=temp;
			*(pTemp+LineBytes*(height-1-i)+j)=temp;
		}
	}
	max-=min;
	for(i=0;i<height;i++)
	{
		for(j=0;j<width;j++)
		{
			temp=*(pTemp+LineBytes*(height-1-i)+j);
			*(pSobel+LineBytes*(height-1-i)+j)=(unsigned char)((temp-min)*255/max);
		}
		
	}
	delete[] pTemp;
}


void  SmoothSobel(LPSTR pBits,LPSTR pSobel,LPSTR pSmoothSobel,LONG width,LONG height)
{
	LONG LineBytes=(width*8+31)/32*4;
	LONG i,j;
	int temp,max,min;
	BYTE m_num;
	Sobel(pBits,pSobel,width,height);
	max=min=*pSobel;
	for(i=0;i<height;i++)
	{
		for(j=0;j<width;j++)
		{
			temp=0;
			if((i<=1||i>=height-2)||(j<=1||j>=width-2))
			{
				m_num=*(pSobel+LineBytes*(height-1-i)+j);
				temp=(int)m_num;
			}
			else
			{
				int m,n;
				for(m=-2;m<=2;m++)
				for(n=-2;n<=2;n++)
				{
					m_num=(*(pSobel+LineBytes*(height-1-(i+m))+(j+n)));
					temp+=m_num;
				}
				temp/=25;
			}
			if(temp>max)
				max=temp;
			if(temp<min)
				min=temp;
			*(pSmoothSobel+LineBytes*(height-1-i)+j)=(BYTE)temp;
		}
	}
	max-=min;
	for(i=0;i<height;i++)
	{
		for(j=0;j<width;j++)
		{
			m_num=*(pSmoothSobel+LineBytes*(height-1-i)+j);
			*(pSmoothSobel+LineBytes*(height-1-i)+j)=(BYTE)((m_num-min)*255/max);
		}
	}
}


void  MulSharpen(LPSTR pBits,LPSTR pLaplacianEnhanced,LPSTR pSobel,LPSTR pSmoothSobel,LONG width,LONG height)
{
	LONG LineBytes=(width*8+31)/32*4;
	LONG i,j;
	int temp,max,min;
	BYTE m_num1,m_num2;
	LaplacianEnhanced(pBits,pLaplacianEnhanced,width,height);
	SmoothSobel(pBits,pSobel,pSmoothSobel,width,height);
	m_num1=*pLaplacianEnhanced;
	m_num2=*pSmoothSobel;
	max=min=(int)(m_num1*m_num2);
	int* pTemp=(int *)new int[LineBytes*height];
	for(i=0;i<height;i++)
	{
		for(j=0;j<width;j++)
		{
			m_num1=*(pLaplacianEnhanced+LineBytes*(height-1-i)+j);
			m_num2=*(pSmoothSobel+LineBytes*(height-1-i)+j);
			temp=(int)(m_num1*m_num2);
			if(temp>max)
				max=temp;
			if(temp<min)
				min=temp;
			*(pTemp+LineBytes*(height-1-i)+j)=temp;
		}
	}
	max-=min;
	
	for(i=0;i<height;i++)
	{
		for(j=0;j<width;j++)
		{
			temp=*(pTemp+LineBytes*(height-1-i)+j);
			
		
			
			*(pSmoothSobel+LineBytes*(height-1-i)+j)=(BYTE)((temp-min)*255/max);

		}
	}
	delete[] pTemp;
}


void  SharpenEnhanced(LPSTR pBits,LPSTR pLaplacianEnhanced,LPSTR pSobel,LPSTR pSmoothSobel,LONG width,LONG height)
{
	LONG LineBytes=(width*8+31)/32*4;
	LONG i,j;
	int temp;
	BYTE m_num1,m_num2;
	MulSharpen(pBits,pLaplacianEnhanced,pSobel,pSmoothSobel,width,height);
	for(i=0;i<height;i++)
		for(j=0;j<width;j++)
		{
			m_num1=*(pBits+LineBytes*(height-1-i)+j);
			m_num2=*(pSmoothSobel+LineBytes*(height-1-i)+j);
			temp=int(m_num1+m_num2);
			if(temp>255)
				temp=255;
			*(pSmoothSobel+LineBytes*(height-1-i)+j)=(BYTE)temp;
		}

}


void  PowerLaw(LPSTR pBits,LPSTR pLaplacianEnhanced,LPSTR pSobel,LPSTR pSmoothSobel,LONG width,LONG height)
{
	LONG LineBytes=(width*8+31)/32*4;
	LONG i,j;
	int temp,max,min;
	BYTE m_num;
	float v=0.5;
	SharpenEnhanced(pBits,pLaplacianEnhanced,pSobel,pSmoothSobel,width,height);
	m_num=*pSmoothSobel;
	max=min=(int)pow(m_num,v);
	for(i=0;i<height;i++)
		for(j=0;j<width;j++)
		{
			m_num=*(pSmoothSobel+LineBytes*(height-1-i)+j);
			temp=int(m_num);
			temp=int(pow(temp,v));
			if(temp>max)
				max=temp;
			if(temp<min)
				min=temp;
			*(pSmoothSobel+LineBytes*(height-1-i)+j)=(BYTE)temp;
		}
	for(i=0;i<height;i++)
		for(j=0;j<width;j++)
		{
			m_num=*(pSmoothSobel+LineBytes*(height-1-i)+j);
			temp=int(m_num);
			
			*(pSmoothSobel+LineBytes*(height-1-i)+j)=(BYTE)((temp-min)*255/(max-min));
		}
}

⌨️ 快捷键说明

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