?? huidubianhuandib.cpp
字號(hào):
#include "stdafx.h"
#include "windowsx.h"
#include "math.h"
#include "HuiDuBianHuanDib.h"
#include "MainFrm.h"
#include "DynSplitView2.h"
HuiDuBianHuanDib::HuiDuBianHuanDib()
{
}
HuiDuBianHuanDib::~HuiDuBianHuanDib()
{
}
///***************************************************************/
/*函數(shù)名稱:FanSeBianHuan()
/*函數(shù)類型:void
/*功能:對(duì)圖像進(jìn)行反色變換
/***************************************************************/
void HuiDuBianHuanDib::FanSeBianHuan()//對(duì)圖像進(jìn)行反色變換
{
LPBYTE p_data; //原圖數(shù)據(jù)區(qū)指針
int wide,height; //原圖長(zhǎng)、寬
p_data=this->GetData (); //取得原圖的數(shù)據(jù)區(qū)指針
if(m_pBitmapInfoHeader->biBitCount<9)
wide=this->GetWidth ();
else
wide=this->GetDibWidthBytes();
height=this->GetHeight ();
for(int i=0;i<height*wide;i++)
{
unsigned char temp;
temp=*(p_data+i);
*(p_data+i)=255- temp;
}
}
///***************************************************************/
/*函數(shù)名稱:Fei0() */
/*函數(shù)類型:void */
/*功能:對(duì)圖像進(jìn)行非零取一運(yùn)算。 */
/****************************************************************/
void HuiDuBianHuanDib::Fei0()//對(duì)圖像進(jìn)行非零取一運(yùn)算
{
LPBYTE p_data; //原圖數(shù)據(jù)區(qū)指針
int wide,height; //原圖長(zhǎng)、寬
p_data=this->GetData (); //取得原圖的數(shù)據(jù)區(qū)指針
if(m_pBitmapInfoHeader->biBitCount<9)
wide=this->GetWidth ();
else
wide=this->GetDibWidthBytes();
height=this->GetHeight ();
for(int j=0;j<height;j++)
{
for(int i=0;i<wide;i++) //所有像素依次循環(huán)
{
if(*p_data!=0) //若像素值不為0
*p_data=255; //將其置為255
p_data++;
}
}
}
/***************************************************************/
/*函數(shù)名稱:GuDing(int YuZhi) */
/*函數(shù)類型:void */
/*參數(shù)說(shuō)明:YuZhi 給定閾值 */
/*功能:對(duì)圖像進(jìn)行固定閥值運(yùn)算。 */
/***************************************************************/
void HuiDuBianHuanDib::GuDing(int YuZhi)//對(duì)圖像進(jìn)行固定閥值運(yùn)算
{
LPBYTE p_data; //原圖數(shù)據(jù)區(qū)指針
int wide,height; //原圖長(zhǎng)、寬
p_data=this->GetData (); //取得原圖的數(shù)據(jù)區(qū)指針
if(m_pBitmapInfoHeader->biBitCount<9)
wide=this->GetWidth ();
else
wide=this->GetDibWidthBytes();
height=this->GetHeight ();
for(int j=0;j<height;j++)
{
for(int i=0;i<wide;i++)
{
if(*p_data>YuZhi) //灰度值大于給定閾值,置為255
*p_data=255;
else
*p_data=0; //不大于置為0
p_data++;
}
}
}
/***************************************************************/
/*函數(shù)名稱:ShuangYu(int YuZhi1,int YuZhi2,int mode) */
/*參數(shù):int YuZhi1:閾值1;int YuZhi2:閾值2;int mode:處理方式*/
/*函數(shù)類型:void */
/*功能:對(duì)圖像使用雙固定閾值法進(jìn)行二值化。 */
/***************************************************************/
void HuiDuBianHuanDib::ShuangYu(int YuZhi1,int YuZhi2,int mode)
{
LPBYTE p_data; //原圖數(shù)據(jù)區(qū)指針
int wide,height; //原圖長(zhǎng)、寬
p_data=this->GetData ();
if(m_pBitmapInfoHeader->biBitCount<9)
wide=this->GetWidth ();
else
wide=this->GetDibWidthBytes();
height=this->GetHeight ();
if(mode==0) //0-255-0型
{
for(int j=0;j<height;j++)
for(int i=0;i<wide;i++)
{
//若該像素的灰度值介于兩個(gè)閾值之間,這將其置為255
if(*p_data>=YuZhi1&&*p_data<=YuZhi2)
*p_data=255;
else
*p_data=0; //否則置0
p_data++;
}
}
if(mode==1) //255-0-255型
{
for(int j=0;j<height;j++)
for(int i=0;i<wide;i++)
{
//若該像素的灰度值介于兩個(gè)閾值之間,這將其置為0
if(*p_data>=YuZhi1&&*p_data<=YuZhi2)
*p_data=0;
else
*p_data=255; //否則置255
p_data++;
}
}
}
///***************************************************************/
/*函數(shù)名稱:ZhiFangTu(float *tongji)
/*函數(shù)類型:void
/*變量說(shuō)明:tongji 灰度分布密度統(tǒng)計(jì)
/*功能:對(duì)圖像進(jìn)行灰度直方圖統(tǒng)計(jì)。
/***************************************************************/
void HuiDuBianHuanDib::ZhiFangTu(float *tongji)
{
// 循環(huán)變量
int i;
int j;
// 灰度計(jì)數(shù)
int huidu[256];
int wide,height; //原圖長(zhǎng)、寬
wide=this->GetWidth ();
height=this->GetHeight ();
// 變量初始化
memset(huidu,0,sizeof(huidu));
LPBYTE temp1=new BYTE[wide*height]; //新圖像緩沖區(qū)
//拷貝原圖像到緩存圖像
memcpy(temp1,m_pData,wide*height );
// 對(duì)各像素進(jìn)行灰度統(tǒng)計(jì)
for (j = 0; j < height; j ++)
{
for (i = 0; i <wide; i ++)
{
unsigned char temp = temp1[wide* j + i] ;
// 灰度統(tǒng)計(jì)計(jì)數(shù)
huidu[temp]++;
}
}
// 計(jì)算灰度分布密度
for(i=0;i<256;i++)
tongji[i] = huidu[i] / (height * wide *1.0f);
}
///***************************************************************/
/*函數(shù)名稱:Zhexianbianhuan(BYTE bX1,BYTE bY1,BYTE bX2,BYTE bY2) */
/*函數(shù)類型:void */
/*變量說(shuō)明:bX1折點(diǎn)一的原始灰度;bX2bY1折點(diǎn)二的原始灰度
/* bY1折點(diǎn)一的變換灰度;bY2折點(diǎn)二的變換灰度
/*功能:對(duì)圖像進(jìn)行分段線性變換。 */
/***************************************************************/
void HuiDuBianHuanDib::Zhexianbianhuan(BYTE bX1, BYTE bY1, BYTE bX2, BYTE bY2)
{
// 循環(huán)變量
int i;
int j;
int wide;
int height;
LPBYTE p_data;
p_data=this->GetData (); //取得原圖的數(shù)據(jù)區(qū)指針
wide=this->GetWidth ();
height=this->GetHeight ();
LPBYTE temp=new BYTE[wide*height]; //新圖像緩沖區(qū)
//拷貝原圖像到緩存圖像
memcpy(temp,p_data,wide*height);
// 灰度映射表
BYTE bMap[256];
// 計(jì)算灰度映射表
for (i = 0; i <= bX1; i++) //[0 —— X1]
{
// 判斷bX1是否大于0(防止分母為0)
if (bX1 > 0)
{
// 線性變換
bMap[i] = (BYTE) bY1 * i / bX1;
}
else
{
// 直接賦值為0
bMap[i] = 0;
}
}
for (; i <= bX2; i++) //(X1 —— X2]
{
// 判斷bX1是否等于bX2(防止分母為0)
if (bX2 != bX1)
{
// 線性變換
bMap[i] = bY1 + (BYTE) ((bY2 - bY1) * (i - bX1) / (bX2 - bX1));
}
else
{
// 直接賦值為bY1
bMap[i] = bY1;
}
}
for (; i < 256; i++) //(X2 —— 256)
{
// 判斷bX2是否等于255(防止分母為0)
if (bX2 != 255)
{
// 線性變換
bMap[i] = bY2 + (BYTE) ((255 - bY2) * (i - bX2) / (255 - bX2));
}
else
{
// 直接賦值為255
bMap[i] = 255;
}
}
if (m_pBitmapInfoHeader->biBitCount<9) //灰度圖像
{
// 對(duì)各像素進(jìn)行灰度轉(zhuǎn)換
for (i = 0; i < height; i ++)
{
for (j = 0; j < wide; j ++)
{
// 對(duì)像素進(jìn)行灰度映射處理
unsigned char T = temp[ wide * i + j];
temp[ wide * i + j] = bMap[T];
}
}
memcpy(p_data, temp,height*wide);
}
else //24位彩色
{
wide=this->GetDibWidthBytes();
temp=new BYTE[wide*height];
memcpy(temp,p_data,wide*height);
// 對(duì)各像素進(jìn)行灰度轉(zhuǎn)換
for (i = 0; i < height; i ++)
{
for (j = 0; j < wide; j ++)
{
// 對(duì)像素進(jìn)行灰度映射處理
unsigned char T = temp[ wide * i + j];
temp[ wide * i + j] = bMap[T];
}
}
memcpy(p_data, temp,height*wide);
}
}
///***************************************************************/
/*函數(shù)名稱:Chuangkoubianhuan(BYTE blow,BYTE bup)
/*函數(shù)類型:void
/*變量說(shuō)明:blow窗口下界 bup窗口上界
/*功能:對(duì)圖像進(jìn)行窗口變換。
/***************************************************************/
void HuiDuBianHuanDib::Chuangkoubianhuan(BYTE bLow, BYTE bUp)
{
// 循環(huán)變量
int i;
int j;
int wide,height; //原圖長(zhǎng)、寬
// 指向源圖像的指針
LPBYTE lpSrc;
// 指向DIB象素指針
LPBYTE p_data;
p_data=this->GetData (); //取得原圖的數(shù)據(jù)區(qū)指針
if (m_pBitmapInfoHeader->biBitCount<9)
wide=this->GetWidth ();
else
wide=this->GetDibWidthBytes();
height=this->GetHeight ();
// 對(duì)各像素進(jìn)行灰度轉(zhuǎn)換
for(i = 0; i < height; i++)
{
// 每列
for(j = 0; j < wide; j++)
{
// 指向DIB第i行,第j個(gè)象素的指針
lpSrc = (LPBYTE)p_data + wide * (height- 1 - i) + j;
// 判斷是否超出范圍
if ((*lpSrc) < bLow)
{
// 直接賦值為0
*lpSrc = 0;
}
else if ((*lpSrc) > bUp)
{
// 直接賦值為255
*lpSrc = 255;
}
}
}
}
///***************************************************************/
/*函數(shù)名稱:Fenbujunhenghua()
/*函數(shù)類型:void
/*變量說(shuō)明:無(wú)
/*功能:對(duì)圖像進(jìn)行灰度分布均衡化處理。
/***************************************************************/
void HuiDuBianHuanDib::Fenbujunhenghua( )
{
// 循環(huán)變量
LONG i;
LONG j;
//圖像的寬和高
LONG wide;
LONG height;
// 灰度分布密度
float fPs_R[256],fPs_G[256],fPs_B[256];
// 中間變量
float temp_r[256],temp_g[256],temp_b[256];
int nNs_R[256],nNs_G[256],nNs_B[256];
// 初始化
memset(temp_r, 0, sizeof(temp_r));
memset(temp_g, 0, sizeof(temp_g));
memset(temp_b, 0, sizeof(temp_b));
// 指向DIB象素指針
LPBYTE p_data;
// 找到DIB圖像象素起始位置
p_data = this->GetData();
wide=this->GetWidth ();
// DIB的高度
height = GetHeight();
if(m_pBitmapInfoHeader->biBitCount<9) //灰度圖像
{
// 獲取圖像的灰度分布密度
ZhiFangTu(fPs_R);
// 進(jìn)行均衡化處理
for(i = 0; i < 256; i++)
{
if(i == 0)
{
temp_r[0] = fPs_R[0];
}
else
{
temp_r[i] = temp_r[i-1] + fPs_R[i];
}
nNs_R[i] = (int)(255.0f * temp_r[i] + 0.5f);
}
}
else //24位彩色
{
// 獲取圖像的灰度分布密度
ZhiFangTu(fPs_R,fPs_G,fPs_B);
// 進(jìn)行均衡化處理
for(i = 0; i < 256; i++)
{
if(i == 0)
{
temp_r[0] = fPs_R[0];
temp_g[0] = fPs_G[0];
temp_b[0] = fPs_B[0];
}
else
{
temp_r[i] = temp_r[i-1] + fPs_R[i];
temp_g[i] = temp_g[i-1] + fPs_G[i];
temp_b[i] = temp_b[i-1] + fPs_B[i];
}
nNs_R[i] = (int)(255.0f * temp_r[i] + 0.5f);
nNs_G[i] = (int)(255.0f * temp_g[i] + 0.5f);
nNs_B[i] = (int)(255.0f * temp_b[i] + 0.5f);
}
}
if (m_pBitmapInfoHeader->biBitCount<9) //灰度圖像
{
// 對(duì)各像素進(jìn)行灰度轉(zhuǎn)換
for (j = 0; j < height; j ++)
{
for (i = 0; i < wide; i ++)
{
// 將轉(zhuǎn)換后的灰度分布寫(xiě)入DIB圖像
unsigned char temp = *((unsigned char *)p_data + wide * j + i);
*((unsigned char *)p_data + wide * j + i) = nNs_R[temp];
}
}
}
else //24位彩色
{
// 對(duì)各像素進(jìn)行灰度轉(zhuǎn)換
for (j = 0; j < height; j ++)
{
for (i = 0; i < wide; i ++)
{
// 將轉(zhuǎn)換后的灰度分布寫(xiě)入DIB圖像
unsigned char temp1= *((unsigned char *)p_data + wide * j*3 + i*3);
*((unsigned char *)p_data + wide * j*3 + i*3) = nNs_R[temp1];
unsigned char temp2 = *((unsigned char *)p_data + wide * j*3 + i*3+1);
*((unsigned char *)p_data + wide * j*3 + i*3+1) = nNs_G[temp2];
unsigned char temp3 = *((unsigned char *)p_data + wide * j*3 + i*3+2);
*((unsigned char *)p_data + wide * j*3 + i*3+2) = nNs_B[temp3];
}
}
}
}
int HuiDuBianHuanDib::GetWidth()
{
return CDib::GetWidth();
}
///***************************************************************/
/*函數(shù)名稱:Pipeibianhuan(BYTE bNum,int *npNu,float*fpPu) */
/*函數(shù)類型:void */
/*變量說(shuō)明:bNum規(guī)定直方圖灰度級(jí) npNu 直方圖映射關(guān)系
/* fpPu灰度分布概率
/*功能:對(duì)圖像進(jìn)行灰度匹配變換。 */
/***************************************************************/
void HuiDuBianHuanDib::Pipeibianhuan(BYTE bNum, int *npNu, float *fpPu)
{
// 循環(huán)變量
LONG i;
LONG j;
LONG wide;
LONG height;
// 灰度分布密度
int midu[256],midu2[256],midu3[256];
// 灰度分布概率
float gailu[256],gailu2[256],gailu3[256];
// 中間臨時(shí)變量
float temp[256],temp2[256],temp3[256];
// 指向DIB象素指針
LPBYTE p_data;
// 找到DIB圖像象素起始位置
p_data = this->GetData();
wide=this->GetWidth ();
// DIB的高度
height = GetHeight();
if (m_pBitmapInfoHeader->biBitCount<9) //灰度圖像
{
// 對(duì)灰度密度分布進(jìn)行統(tǒng)計(jì)
Midufenbu(midu);
// 對(duì)灰度分布概率進(jìn)行統(tǒng)計(jì)
ZhiFangTu(gailu);
// 計(jì)算原始累計(jì)直方圖
for (i = 0; i < 256; i++)
{
if (i == 0)
{
temp[0] = gailu[0];
}
else
{
temp[i] = temp[i-1] + gailu[i];
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -