c++编写的图像读取、保存、相减、放大(最近邻插值。双线性插值)、均值滤波、中值滤波

         算法均为原创,因博主为新手,水平有限,需在这里对程序做一些说明,编译环境为VS2010,处理图像的格式为BMP,目前只编写了灰度图像的处理方法,图像大小为256*256。放大倍数为2倍,其中最近邻插值为通过链表处理,其余图像的处理方法为二维矩阵下处理,中值滤波中运用了快速排序算法,程序也会上传,写的不好还望大佬勿喷,游客支持。

#include "stdafx.h"

#include  
#include    
#include   
#include   
#include   
#include   
#include   
#include   
#include
#include"quicksort.h"
using namespace std;
//---------------------------------------------------------------------------------------   
#define DOUBLE 2
//读入图像数据的指针  
unsigned char *pBmpBuf;
unsigned char *pBmpBuf_r;
unsigned char *pBmpBuf_l;
unsigned char *pBmpBuf_plus;//减法图像
unsigned char *pBmpBuf_zoom;//尺度变换图像
unsigned char *pBmpBuf_zoom2;
unsigned char *pBmpBuf_gaussian;
unsigned char *pBmpBuf_salt;
int bmpWidth;//图像的宽  
int bmpHeight;//图像的高  
RGBQUAD *pColorTable;//颜色表指针  
int biBitCount;//图像类型,每像素位数  

void quickSort(int s[], int l, int r);
//-------------------------------------------------------------------------------------------  
//读图像的位图数据、宽、高、颜色表及每像素位数等数据进内存,存放在相应的全局变量中  
bool readBmp(char *bmpName)   
{  
	

    FILE *fp=fopen(bmpName,"rb");//二进制读方式打开指定的图像文件  
  
    if(fp==0)  
        return 0;  
  
    //跳过位图文件头结构BITMAPFILEHEADER  
  
    fseek(fp, sizeof(BITMAPFILEHEADER),0);  
	//cout<255)
				BUF_BIG[ii][jj] = 255;
		}
	}
	/*变回指针赋给全局变量*指针pBmpBuf_zoom2*/
	//bmpBUF = (unsigned char*)&BUF_BIG[0][0];
	//strcpy((char*)bmpBUF , (char*)BUF_BIG[0][0]);
	
	kk = 0;
	for( ii = 0;ii < height*DOUBLE;ii++)
	{
		for(jj = 0;jj < width*DOUBLE ;jj++)
		{
			 *(bmpBUF + kk)  = BUF_BIG[ii][jj] ;
			 kk++;
		}
	}
	
	delete []BUF_BIG;//清除申请的空间
	return(bmpBUF);
}
void  filter_average(unsigned long int Buf[256][256], int width, int height,  RGBQUAD *pColorTable)
{
	int i,j,kk;
	/*边界像素值不动*/
	/* 3*3模版滤波 */
	for(i = 1;i < height-1;i++)
	{
		for(j = 1;j < width-1;j++)
		{
			Buf[i][j] = (Buf[i-1][j-1]+Buf[i-1][j]+Buf[i-1][j+1]+Buf[i][j-1]+Buf[i][j]+Buf[i][j+1]+Buf[i+1][j-1]\
				      +Buf[i+1][j]+Buf[i+1][j+1])/9;
				
		}
	}
	kk = 0;
	for( i = 0;i < height;i++)
	{
		for(j = 0;j < width ;j++)
		{
			 *(pBmpBuf_gaussian + kk)  = Buf[i][j] ;
			 kk++;
		}
	}
}
void  filter_middle(unsigned long int Buf[256][256], int width, int height, RGBQUAD *pColorTable)
{
	int i,j,k=0;
	int kk = 0;
	int s[9];
	//边界值不动,采用3*3模版滤波
	for(i = 1 ; i < height - 1 ; i++)
	{
		for(j = 1 ; j < width - 1 ; j++)
		{
			s[k++] = Buf[i-1][j-1];
			s[k++] = Buf[i-1][j];
			s[k++] = Buf[i-1][j+1];
			s[k++] = Buf[i][j-1];
			s[k++] = Buf[i][j];
			s[k++] = Buf[i][j+1];
			s[k++] = Buf[i+1][j-1];
			s[k++] = Buf[i+1][j];
			s[k++] = Buf[i+1][j+1];
			k=0;
			quickSort( s, 0,  sizeof(s)/sizeof(int)-1) ; 
			Buf[i][j] = s[4];
		}
	}
	for( i = 0;i < height;i++)
	{
		for(j = 0;j < width ;j++)
		{
			 *(pBmpBuf_salt + kk)  = Buf[i][j] ;
			 kk++;
		}
	}
}
void shuzu(unsigned char *imgBuf, int width, int height, int biBitCount, RGBQUAD *pColorTable)//实验程序,不参与主函数。
{
	int i,j,k = 0;
	unsigned  int BUF[256][256];
	unsigned char *pBmpBuf_zoom_shuzu;
	int lineByte=(width * biBitCount)/32*4;
	pBmpBuf_zoom_shuzu=new unsigned char[lineByte * height];
	//
	for( i = 0;i<256;i++)
	{
		for(j = 0;j < 256 ;j++)
		{
			BUF[i][j] = *(imgBuf + k);
			k++;
		}
	}
	k = 0;
	pBmpBuf_zoom_shuzu = new unsigned char[width*height];
	for( i = 0;i < 256;i++)
	{
		for(j = 0;j < 256 ;j++)
		{
			 *(pBmpBuf_zoom_shuzu + k)  = BUF[i][j] ;
			 k++;
		}
	}
	char writePath[]="变数组存储.BMP";//图片处理后再存储    
     saveBmp(writePath, pBmpBuf_zoom_shuzu,256, 256, 8, pColorTable);
	 
	 //
}
void main()  
{  
	int i,j,k = 0;
	unsigned long int BUF[256][256];//将图像存放在这个数组中
	//unsigned char *pBmpBuf_zoom2;
	char writePath[]="放大.BMP";//图片处理后再存储 
	char writePath1[]="减法.BMP";//图片处理后再存储 
	char writePath2[]="双线性插值.BMP";//图片处理后再存储 
	char writePath3[]="均值滤波.BMP";//图片处理后再存储 
	char writePath4[]="中值滤波.BMP";//图片处理后再存储 
    //读入指定BMP文件进内存  
    char readPath[]="C:\\Users\\Administrator\\Documents\\Visual Studio 2010\\Projects\\read a bmp_new\\rice.bmp";  //待处理文件
    char readPath_lena[]="C:\\Users\\Administrator\\Documents\\Visual Studio 2010\\Projects\\read a bmp_new\\lena.bmp";//待处理文件
	char readPath_lena_salt[]="C:\\Users\\Administrator\\Documents\\Visual Studio 2010\\Projects\\read a bmp_new\\salt_0.05.bmp";//待处理文件
	char readPath_lena_gaussian[]="C:\\Users\\Administrator\\Documents\\Visual Studio 2010\\Projects\\read a bmp_new\\gaussian_0.05.bmp";//待处理文件
	
	
	//读入第一幅图像
    readBmp(readPath);  
    //输出第一幅图像的信息  
	cout<<"rice图像信息:"<  
using namespace std;  
void quickSort(int a[],int,int);  
  /*
  l = 0;
  len=sizeof(array)/sizeof(int);  

  */
void quickSort(int s[], int l, int r)  
{  
    if (l< r)  
    {        
        int i = l, j = r, x = s[l];  
        while (i < j)  
        {  
            while(i < j && s[j]>= x) // 从右向左找第一个小于x的数  
                j--;   
            if(i < j)  
                s[i++] = s[j];  
            while(i < j && s[i]< x) // 从左向右找第一个大于等于x的数  
                i++;   
            if(i < j)  
                s[j--] = s[i];  
        }  
        s[i] = x;  
        quickSort(s, l, i - 1); // 递归调用  
        quickSort(s, i + 1, r);  
    }  
} 

读取和保存参考了下面这篇博客:

http://blog.csdn.net/lyy289065406/article/details/6717679


#include "stdafx.h" #include #include #include #include #include #include #include #include #include #include"quicksort.h" using namespace std; //--------------------------------------------------------------------------------------- #define DOUBLE 2 //读å¥å¾åæ°æ®çæé unsigned char *pBmpBuf; unsigned char *pBmpBuf_r; unsigned char *pBmpBuf_l; unsigned char *pBmpBuf_plus;//åæ³å¾å unsigned char *pBmpBuf_zoom;//尺度åæ¢å¾å unsigned char *pBmpBuf_zoom2; unsigned char *pBmpBuf_gaussian; unsigned char *pBmpBuf_salt; int bmpWidth;//å¾åç宽 int bmpHeight;//å¾åçé« RGBQUAD *pColorTable;//é¢è²è¡¨æé int biBitCount;//å¾åç±»åï¼æ¯åç´ ä½æ° void quickSort(int s[], int l, int r); //------------------------------------------------------------------------------------------- //读å¾åçä½å¾æ°æ®ã宽ãé«ãé¢è²è¡¨åæ¯åç´ ä½æ°ç­æ°æ®è¿åå­ï¼å­æ¾å¨ç¸åºçå¨å±åé中 bool readBmp(char *bmpName) { FILE *fp=fopen(bmpName,"rb");//äºè¿å¶è¯»æ¹å¼æå¼æå®çå¾åæ件 if(fp==0) return 0; //è·³è¿ä½å¾æ件头ç»æBITMAPFILEHEADER fseek(fp, sizeof(BITMAPFILEHEADER),0); //cout<255) BUF_BIG[ii][jj] = 255; } } /*ååæéèµç»å¨å±åé*æépBmpBuf_zoom2*/ //bmpBUF = (unsigned char*)&BUF_BIG[0][0]; //strcpy((char*)bmpBUF , (char*)BUF_BIG[0][0]); kk = 0; for( ii = 0;ii < height*DOUBLE;ii++) { for(jj = 0;jj < width*DOUBLE ;jj++) { *(bmpBUF + kk) = BUF_BIG[ii][jj] ; kk++; } } delete []BUF_BIG;//æ¸é¤ç³è¯·çç©ºé´ return(bmpBUF); } void filter_average(unsigned long int Buf[256][256], int width, int height, RGBQUAD *pColorTable) { int i,j,kk; /*è¾¹çåç´ å¼ä¸å¨*/ /* 3*3模ç滤波 */ for(i = 1;i < height-1;i++) { for(j = 1;j < width-1;j++) { Buf[i][j] = (Buf[i-1][j-1]+Buf[i-1][j]+Buf[i-1][j+1]+Buf[i][j-1]+Buf[i][j]+Buf[i][j+1]+Buf[i+1][j-1]\ +Buf[i+1][j]+Buf[i+1][j+1])/9; } } kk = 0; for( i = 0;i < height;i++) { for(j = 0;j < width ;j++) { *(pBmpBuf_gaussian + kk) = Buf[i][j] ; kk++; } } } void filter_middle(unsigned long int Buf[256][256], int width, int height, RGBQUAD *pColorTable) { int i,j,k=0; int kk = 0; int s[9]; //è¾¹çå¼ä¸å¨ï¼éç¨3*3模ç滤波 for(i = 1 ; i < height - 1 ; i++) { for(j = 1 ; j < width - 1 ; j++) { s[k++] = Buf[i-1][j-1]; s[k++] = Buf[i-1][j]; s[k++] = Buf[i-1][j+1]; s[k++] = Buf[i][j-1]; s[k++] = Buf[i][j]; s[k++] = Buf[i][j+1]; s[k++] = Buf[i+1][j-1]; s[k++] = Buf[i+1][j]; s[k++] = Buf[i+1][j+1]; k=0; quickSort( s, 0, sizeof(s)/sizeof(int)-1) ; Buf[i][j] = s[4]; } } for( i = 0;i < height;i++) { for(j = 0;j < width ;j++) { *(pBmpBuf_salt + kk) = Buf[i][j] ; kk++; } } } void shuzu(unsigned char *imgBuf, int width, int height, int biBitCount, RGBQUAD *pColorTable)//å®éªç¨åºï¼ä¸åä¸ä¸»å½æ°ã { int i,j,k = 0; unsigned int BUF[256][256]; unsigned char *pBmpBuf_zoom_shuzu; int lineByte=(width * biBitCount)/32*4; pBmpBuf_zoom_shuzu=new unsigned char[lineByte * height]; // for( i = 0;i<256;i++) { for(j = 0;j < 256 ;j++) { BUF[i][j] = *(imgBuf + k); k++; } } k = 0; pBmpBuf_zoom_shuzu = new unsigned char[width*height]; for( i = 0;i < 256;i++) { for(j = 0;j < 256 ;j++) { *(pBmpBuf_zoom_shuzu + k) = BUF[i][j] ; k++; } } char writePath[]="åæ°ç»å­å¨.BMP";//å¾çå¤çååå­å¨ saveBmp(writePath, pBmpBuf_zoom_shuzu,256, 256, 8, pColorTable); // } void main() { int i,j,k = 0; unsigned long int BUF[256][256];//å°å¾åå­æ¾å¨è¿ä¸ªæ°ç»ä¸­ //unsigned char *pBmpBuf_zoom2; char writePath[]="æ¾å¤§.BMP";//å¾çå¤çååå­å¨ char writePath1[]="åæ³.BMP";//å¾çå¤çååå­å¨ char writePath2[]="å线æ§æå¼.BMP";//å¾çå¤çååå­å¨ char writePath3[]="åå¼æ»¤æ³¢.BMP";//å¾çå¤çååå­å¨ char writePath4[]="中å¼æ»¤æ³¢.BMP";//å¾çå¤çååå­å¨ //读å¥æå®BMPæ件è¿åå­ char readPath[]="C:\\Users\\Administrator\\Documents\\Visual Studio 2010\\Projects\\read a bmp_new\\rice.bmp"; //å¾å¤çæ件 char readPath_lena[]="C:\\Users\\Administrator\\Documents\\Visual Studio 2010\\Projects\\read a bmp_new\\lena.bmp";//å¾å¤çæ件 char readPath_lena_salt[]="C:\\Users\\Administrator\\Documents\\Visual Studio 2010\\Projects\\read a bmp_new\\salt_0.05.bmp";//å¾å¤çæ件 char readPath_lena_gaussian[]="C:\\Users\\Administrator\\Documents\\Visual Studio 2010\\Projects\\read a bmp_new\\gaussian_0.05.bmp";//å¾å¤çæ件 //读å¥ç¬¬ä¸å¹å¾å readBmp(readPath); //è¾åºç¬¬ä¸å¹å¾åçä¿¡æ¯ cout<<"riceå¾åä¿¡æ¯ï¼"< using namespace std; void quickSort(int a[],int,int); /* l = 0; len=sizeof(array)/sizeof(int); */ void quickSort(int s[], int l, int r) { if (l< r) { int i = l, j = r, x = s[l]; while (i < j) { while(i < j && s[j]>= x) // ä»å³åå·¦æ¾ç¬¬ä¸ä¸ªå°äºxçæ° j--; if(i < j) s[i++] = s[j]; while(i < j && s[i]< x) // ä»å·¦åå³æ¾ç¬¬ä¸ä¸ªå¤§äºç­äºxçæ° i++; if(i < j) s[j--] = s[i]; } s[i] = x; quickSort(s, l, i - 1); // éå½è°ç¨ quickSort(s, i + 1, r); } }

其中读入的待处理图像为

c++编写的图像读取、保存、相减、放大(最近邻插值。双线性插值)、均值滤波、中值滤波_第1张图片

c++编写的图像读取、保存、相减、放大(最近邻插值。双线性插值)、均值滤波、中值滤波_第2张图片

c++编写的图像读取、保存、相减、放大(最近邻插值。双线性插值)、均值滤波、中值滤波_第3张图片

c++编写的图像读取、保存、相减、放大(最近邻插值。双线性插值)、均值滤波、中值滤波_第4张图片

结果图像为

c++编写的图像读取、保存、相减、放大(最近邻插值。双线性插值)、均值滤波、中值滤波_第5张图片

c++编写的图像读取、保存、相减、放大(最近邻插值。双线性插值)、均值滤波、中值滤波_第6张图片

c++编写的图像读取、保存、相减、放大(最近邻插值。双线性插值)、均值滤波、中值滤波_第7张图片

均值滤波:

c++编写的图像读取、保存、相减、放大(最近邻插值。双线性插值)、均值滤波、中值滤波_第8张图片

中值滤波:

c++编写的图像读取、保存、相减、放大(最近邻插值。双线性插值)、均值滤波、中值滤波_第9张图片

你可能感兴趣的:(c++编写的图像读取、保存、相减、放大(最近邻插值。双线性插值)、均值滤波、中值滤波)