?? improve.cpp
字號(hào):
#include "StdAfx.h"
#include "improve.h"
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#pragma warning ( disable : 4018)
int GetAsh(BYTE** imageBuf, int x, int y)
{
int clr;
clr = (imageBuf[y][x*4] + imageBuf[y][x*4+1]
+imageBuf[y][x*4+2]) / 3;
return clr;
}
/********************************************************
* 把線形存儲(chǔ)的像素轉(zhuǎn)化為二維數(shù)組形式
* 參數(shù): image 線形存儲(chǔ)的像素, width,height 圖象的長(zhǎng)寬
********************************************************/
BYTE** CreatImage(BYTE* image, unsigned int width, unsigned int height, int bt=4)
{
BYTE** imageBuf = (BYTE**)malloc(sizeof(BYTE*)*(height));
for(int y=0; y<height; y++)
{
//使imageBuf中每個(gè)指針分別指向其下標(biāo)表示的行的行首地址
imageBuf[y] = image+y*width*bt;
}
return imageBuf;
}
/**************************************************
* 功能: 設(shè)定指定位置的像素灰度
* 參數(shù): imageBuf為目標(biāo)圖像 x,y為要設(shè)定像素的坐標(biāo)
**************************************************/
void SetPixelXY(BYTE** imageBuf1, int x, int y, int a)
{
imageBuf1[y][x*4] = a;
imageBuf1[y][x*4+1] = a;
imageBuf1[y][x*4+2] = a;
imageBuf1[y][x*4+3]= 255;
}
/**************************************************
* 功能: 使用模板對(duì)彩色圖鄰域進(jìn)行運(yùn)算
* 參數(shù): imageBuf為目標(biāo)圖像 w、h為圖像大小
* templt為模板 tw為鄰域大小
* x,y為要取得像素的坐標(biāo)
* cn為顏色分量編號(hào) 0為藍(lán)色 1為綠色 2為紅色
**************************************************/
int TempltExcuteCl(BYTE** imageBuf0, int w, int h, int* templt, int tw, int x, int y, int cn)
{
int i,j; //循環(huán)變量
int m=0; //用來存放加權(quán)和
int px,py;
//依次對(duì)鄰域中每個(gè)像素進(jìn)行運(yùn)算
for(i=0; i<tw; i++)
{
for(j=0; j<tw; j++)
{
//計(jì)算對(duì)應(yīng)模板上位置的像素在原圖像中的位置
py=y-tw/2+i;
px=x-tw/2+j;
//加權(quán)求和
m+=imageBuf0[py][px*4+cn] * templt[i*tw+j];
}
}
return m; //返回結(jié)果
}
/*****************************************************************
* 功能: 使用模板對(duì)灰度圖鄰域進(jìn)行運(yùn)算
* 參數(shù): imageBuf為目標(biāo)圖像 w、h為圖像大小
* templt為模板 tw為鄰域大小
* x,y為要取得像素的坐標(biāo)
******************************************************************/
int TempltExcuteAsh(BYTE** imageBuf0, int w, int h,
int* templt, int tw, int x, int y)
{
int i,j; //循環(huán)變量
int m=0; //用來存放加權(quán)和
int px,py;
//依次對(duì)鄰域中每個(gè)像素進(jìn)行運(yùn)算
for(i=0; i<tw; i++)
{
for(j=0; j<tw; j++)
{
//計(jì)算對(duì)應(yīng)模板上位置的像素在原圖像中的位置
py=y-tw/2+i;
px=x-tw/2+j;
//加權(quán)求和
m+=GetAsh(imageBuf0,px,py) * templt[i*tw+j];
}
}
return m; //返回結(jié)果
}
/******************************************************************
* 功能: 灰度圖像的簡(jiǎn)單平滑處理
* 參數(shù): image0為原圖形,image1為平滑結(jié)果,
* w、h為圖象的寬和高
* size為進(jìn)行平滑的鄰域邊長(zhǎng)
******************************************************************/
void SmoothAsh(BYTE* image0, BYTE* image1,
unsigned int w, unsigned int h, unsigned int size)
{
//將圖像轉(zhuǎn)化為矩陣形式
BYTE** imageBuf0 = CreatImage(image0, w, h);
BYTE** imageBuf1 = CreatImage(image1, w, h);
//定義模板
int* templt;
int x,y;
int a;
int scale;
//根據(jù)鄰域大小設(shè)定模板
templt = new int[size * size];
for(x=0; x<size*size; x++)
{
templt[x]=1;
}
//設(shè)定衰減因子
scale = size*size;
//依次對(duì)原圖像的每個(gè)像素進(jìn)行處理
for(y=size/2; y<h-size/2; y++)
{
for(x=size/2; x<w-size/2; x++)
{
a=TempltExcuteAsh(imageBuf0,w,h,templt,size,x,y);
a/= scale;
//過限處理
a = a>255?255:a;
a = a<0?0:a;
SetPixelXY(imageBuf1,x,y,a);
}
}
//清理內(nèi)存
delete[] templt;
free(imageBuf0);
free(imageBuf1);
}
/******************************************************************
* 功能: 彩色圖像的簡(jiǎn)單平滑處理
* 參數(shù): image0為原圖形,image1平滑結(jié)果,
* w、h為圖象的寬和高
* size為進(jìn)行平滑的鄰域邊長(zhǎng)
******************************************************************/
void SmoothCl(BYTE* image0, BYTE* image1,
unsigned int w, unsigned int h, unsigned int size)
{
//將圖像轉(zhuǎn)化為矩陣形式
BYTE** imageBuf0 = CreatImage(image0, w, h);
BYTE** imageBuf1 = CreatImage(image1, w, h);
//定義模板
int* templt;
int x,y,c;
int a;
int scale;
//根據(jù)鄰域大小設(shè)定模板
templt = new int[size * size];
for(x=0; x<size*size; x++)
{
templt[x]=1;
}
//設(shè)定衰減因子
scale = size*size;
//依次對(duì)原圖像的每個(gè)像素進(jìn)行處理
for(y=size/2; y<h-size/2; y++)
{
for(x=size/2; x<w-size/2; x++)
{
//依次對(duì)彩色圖像的三個(gè)分量灰度進(jìn)行處理
for(c=0; c<3; c++)
{
a=TempltExcuteCl(imageBuf0,w,h,templt,size,x,y,c);
a/= scale;
//過限處理
a = a>255?255:a;
a = a<0?0:a;
imageBuf1[y][x*4+c]=a;
}
}
}
//清理內(nèi)存
delete[] templt;
free(imageBuf0);
free(imageBuf1);
}
/******************************************************************
* 功能: 彩色圖像的高斯平滑處理
* 參數(shù): image0為原圖形,image1平滑結(jié)果,
* w、h為圖像的寬和高
******************************************************************/
void SmoothGaussCl(BYTE* image0, BYTE* image1, unsigned int w, unsigned int h)
{
//將圖像轉(zhuǎn)化為矩陣形式
BYTE** imageBuf0 = CreatImage(image0, w, h);
BYTE** imageBuf1 = CreatImage(image1, w, h);
//設(shè)定模板
int templt[9]={1,2,1,2,4,2,1,2,1};
int x,y,c;
int a;
int scale;
//設(shè)定衰減因子
scale = 16;
//依次對(duì)原圖像的每個(gè)像素進(jìn)行處理
for(y=1; y<h-1; y++)
for(x=1; x<w-1; x++)
for(c=0; c<3; c++)
{
//利用高斯模板對(duì)鄰域進(jìn)行處理
a=TempltExcuteCl(imageBuf0,w,h,templt,3,x,y,c);
a/= scale;
//過限處理
a = a>255?255:a;
a = a<0?0:a;
imageBuf1[y][x*4+c]=a;
}
//清理內(nèi)存
free(imageBuf0);
free(imageBuf1);
}
/**************************************************
* 功能: 使用模板對(duì)灰度圖鄰域排序取中值
* 參數(shù): imageBuf為目標(biāo)圖像 w、h為圖像大小
* templt為模板 tw為鄰域大小
* x,y為當(dāng)前采樣窗口中心像素的坐標(biāo)
**************************************************/
int MedianValueAsh(BYTE** imageBuf0, int w, int h, int* templt, int tw, int x, int y)
{
int i,j,k;
int px,py,c;
int* value; //用來保存要排序的數(shù)值
int count; //用來保存采樣窗口的像素?cái)?shù)量
value= new int[tw*tw];
k=0;
//從采樣窗口中取得像素灰度
for(i=0; i<tw; i++)
{
for(j=0; j<tw; j++)
{
py=y-tw/2+i;
px=x-tw/2+j;
//如果該像素位于采樣窗口中
if(templt[i*tw+j]>0)
{
//保存像素灰度
value[k]=GetAsh(imageBuf0,px,py);
k++;
}
}
}
//記錄保存的像素個(gè)數(shù)
count=k;
//對(duì)保存的像素灰度數(shù)據(jù)進(jìn)行排序
for(i=0; i<count-1; i++)
{
k=i;
for(j=i+1; j<count; j++)
if(value[j]<value[k]) k=j;
c=value[i];
value[i]=value[k];
value[k]=c;
}
//保存中值
c=value[count/2];
//清理內(nèi)存
delete[] value;
return c;
}
/******************************************************************
* 功能: 灰度圖像的中值濾波平滑處理
* 參數(shù): image0為原圖形,image1平滑結(jié)果,
* w、h為圖象的寬和高
* size為進(jìn)行平滑的鄰域邊長(zhǎng)
******************************************************************/
void SmoothMedianAsh(BYTE* image0, BYTE* image1, unsigned int w, unsigned int h, unsigned int size)
{
//將圖像轉(zhuǎn)化為矩陣形式
BYTE** imageBuf0 = CreatImage(image0, w, h);
BYTE** imageBuf1 = CreatImage(image1, w, h);
//設(shè)定模板
int* templt;
int x,y;
int a;
int scale;
//根據(jù)鄰域大小設(shè)定模板
templt = new int[size * size];
for(x=0; x<size*size; x++) templt[x]=1;
//設(shè)定衰減因子
scale = 1;
//依次對(duì)原圖像的每個(gè)像素進(jìn)行處理
for(y=size/2; y<h-size/2; y++)
for(x=size/2; x<w-size/2; x++)
{
//取采樣窗口中像素灰度的中值
a=MedianValueAsh(imageBuf0,w,h,templt,size,x,y);
a/= scale;
//過限處理
a = a>255?255:a;
a = a<0?0:a;
SetPixelXY(imageBuf1,x,y,a);
}
//清理內(nèi)存
delete[] templt;
free(imageBuf0);
free(imageBuf1);
}
/**************************************************
* 功能: 使用模板對(duì)彩色圖鄰域排序取中值
* 參數(shù): imageBuf為目標(biāo)圖像 w、h為圖像大小
* templt為模板 tw為鄰域大小
* x,y為當(dāng)前采樣窗口中心像素的坐標(biāo)
* cn為顏色分量編號(hào) 0為藍(lán)色 1為綠色 2為紅色
**************************************************/
int MedianValueCl(BYTE** imageBuf0, int w, int h, int* templt, int tw, int x, int y, int cn)
{
int i,j,k;
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -