📄 tiduruihuadib.cpp
字号:
#include "stdafx.h"
#include "windowsx.h"
#include "math.h"
#include "TiDuRuiHuaDib.h"
#include "MainFrm.h"
#include "DynSplitView2.h"
#include "CDIB.h"
#include "DlgCanshu.h"
#include "DSplitDoc.h"
TiDuRuiHuaDib::TiDuRuiHuaDib()
{
}
TiDuRuiHuaDib::~TiDuRuiHuaDib()
{
}
///***************************************************************/
/*函数名称:Menxianruihua()
/*函数类型:void
/*功能:对图像进行门限梯度锐化。
/*****************************************************************/
void TiDuRuiHuaDib::Menxianruihua()
{
LPBYTE p_data; //原图数据区指针
int wide,height; //原图长、宽
p_data=this->GetData ();
wide=this->GetWidth ();
height=this->GetHeight ();
if (m_pBitmapInfoHeader->biBitCount<9) //灰度图像
{
LPBYTE p_temp=new BYTE [wide*height]; //开辟图像一缓冲区
memset(p_temp,255,wide*height); //初始化为255
int temp;
for(int j=1;j<height-1;j++)
for(int i=1;i<wide-1;i++)
{
//根据双向一次微分公式计算当前像素的灰度值
temp=(int)sqrt((p_data[wide*j+i]-p_data[wide*j+(i-1)])*
(p_data[wide*j+i]-p_data[wide*j+(i-1)])+(p_data[wide*j+i]-p_data[wide*(j-1)+i])*
(p_data[wide*j+i]-p_data[wide*(j-1)+i]));
if (temp>=30)
{
if((temp+100)>255)
p_temp[wide*j+i]=255;
else
p_temp[wide*j+i]=temp+100;
}
if (temp<30)
p_temp[wide*j+i]=p_data[wide*j+i];
}
//将缓冲区中的图像复制回原图数据区
memcpy(p_data,p_temp,wide*height);
//删除缓冲区
delete p_temp;
}
else //24位彩色
{
int DibWidth; //原图长、宽、字节宽
DibWidth=this->GetDibWidthBytes(); //取得原图的每行字节数
BYTE *p_temp=new BYTE[height*DibWidth];
for(int j=1;j<height-1;j++) // 每行
{
for(int i=3;i<DibWidth-3;i++) // 每列
{
int pby_pt1=0,pby_pt2=0,pby_pt=0;
pby_pt1=p_data[DibWidth*j+i]-p_data[DibWidth*j+(i-3)];
pby_pt2=p_data[DibWidth*j+i]-p_data[DibWidth*(j-1)+i];
pby_pt=(int)sqrt(pby_pt1*pby_pt1+pby_pt2*pby_pt2);
if (pby_pt>=30)
{
if((pby_pt+100)>255)
p_temp[DibWidth*j+i]=255;
else
p_temp[DibWidth*j+i]=pby_pt+100;
}
if (pby_pt<30)
p_temp[DibWidth*j+i]=p_data[DibWidth*j+i];
}
}
memcpy(p_data,p_temp,height*DibWidth); // 复制处理后的图像
delete []p_temp; //删除暂时分配内存
}
}
///***************************************************************/
/*函数名称:GuDingRuiHua()
/*函数类型:void
/*功能:给边缘规定一个特定的灰度级。
/***************************************************************/
void TiDuRuiHuaDib::GuDingRuiHua()
{
LPBYTE p_data; //原图数据区指针
int wide,height; //原图长、宽
p_data=this->GetData ();
wide=this->GetWidth ();
height=this->GetHeight ();
if (m_pBitmapInfoHeader->biBitCount<9) //灰度图像
{
LPBYTE p_temp=new BYTE [wide*height]; //开辟图像一缓冲区
memset(p_temp,255,wide*height); //初始化为255
int temp;
for(int j=1;j<height-1;j++)
for(int i=1;i<wide-1;i++)
{
//根据双向一次微分公式计算当前像素的灰度值
temp=(int)sqrt((p_data[wide*j+i]-p_data[wide*j+(i-1)])*(p_data[wide*j+i]-p_data[wide*j+(i-1)])+(p_data[wide*j+i]-p_data[wide*(j-1)+i])*(p_data[wide*j+i]-p_data[wide*(j-1)+i]));
if (temp>30)
{
p_temp[wide*j+i]=255;
}
else
p_temp[wide*j+i]=p_data[wide*j+i];
}
//将缓冲区中的图像复制回原图数据区
memcpy(p_data,p_temp,wide*height);
//删除缓冲区
delete p_temp;
}
else //24位彩色
{
int DibWidth; //原图长、宽、字节宽
DibWidth=this->GetDibWidthBytes(); //取得原图的每行字节数
BYTE *p_temp=new BYTE[height*DibWidth];
for(int j=1;j<height-1;j++) // 每行
{
for(int i=3;i<DibWidth-3;i++) // 每列
{
int pby_pt1=0,pby_pt2=0,pby_pt=0;
pby_pt1=*(p_data+(height-j-1)*DibWidth+i)-*(p_data+(height-j-1)*DibWidth+i-3);
pby_pt2=*(p_data+(height-j-1)*DibWidth+i)-*(p_data+(height-j)*DibWidth+i);
pby_pt=(int)sqrt(pby_pt1*pby_pt1+pby_pt2*pby_pt2);
if (pby_pt>30)
{
*(p_temp+(height-j-1)*DibWidth+i)=255;
}
else
*(p_temp+(height-j-1)*DibWidth+i)=*(p_data+(height-j-1)*DibWidth+i);
}
}
memcpy(p_data,p_temp,height*DibWidth); // 复制处理后的图像
delete []p_temp; //删除暂时分配内存
}
}
void TiDuRuiHuaDib::Zishiying()
{
//遍历图象的纵坐标
int y;
//遍历图象的横坐标
int x;
//图象的长宽大小
int height = this->GetHeight();
int wide = this->GetWidth();
//图像数据指针
LPBYTE P_data=this->GetData();
// 局部阈值
int nThd[2][2] ;
// 子图象的平均值
int nLocAvg ;
// 对左上图像逐点扫描:
nLocAvg = 0 ;
// y方向
for(y=0; y<height/2 ; y++ )
{
// x方向
for(x=0; x<wide/2 ; x++ )
{
nLocAvg += *(P_data+y*wide + x);
}
}
// 计算均值
nLocAvg /= ( (height/2) * (wide/2) ) ;
// 设置阈值为子图象的平均值
nThd[0][0] = nLocAvg ;
// 对左上图像逐点扫描进行分割:
// y方向
for(y=0; y<height/2 ; y++ )
{
// x方向
for(x=0; x<wide/2 ; x++ )
{
if(*(P_data+y*wide + x)>nThd[0][0])
*(P_data+y*wide + x) = 255 ;
else
{
*(P_data+y*wide + x) = 0 ;
}
}
}
// =============================================
// 对左下图像逐点扫描:
nLocAvg = 0 ;
// y方向
for(y=height/2; y<height ; y++ )
{
// x方向
for(x=0; x<wide/2 ; x++ )
{
nLocAvg += *(P_data+y*wide + x);
}
}
// 计算均值
nLocAvg /= ( (height - height/2) * (wide/2) ) ;
// 设置阈值为子图象的平均值
nThd[1][0] = nLocAvg ;
// 对左下图像逐点扫描进行分割:
// y方向
for(y=height/2; y<height ; y++ )
{
// x方向
for(x=0; x<wide/2 ; x++ )
{
if(*(P_data+y*wide + x)>nThd[1][0])
*(P_data+y*wide + x) = 255 ;
else
{
*(P_data+y*wide + x) = 0 ;
}
}
}
// =============================================
// 对右上图像逐点扫描:
nLocAvg = 0 ;
// y方向
for(y=0; y<height/2 ; y++ )
{
// x方向
for(x=wide/2; x<wide ; x++ )
{
nLocAvg += *(P_data+y*wide + x);
}
}
// 计算均值
nLocAvg /= ( (height/2) * (wide - wide/2) ) ;
// 设置阈值为子图象的平均值
nThd[0][1] = nLocAvg ;
// 对右上图像逐点扫描进行分割:
// y方向
for(y=0; y<height/2 ; y++ )
{
// x方向
for(x=wide/2; x<wide ; x++ )
{
if(*(P_data+y*wide + x)>nThd[0][1])
*(P_data+y*wide + x) = 255 ;
else
{
*(P_data+y*wide + x)= 0 ;
}
}
}
// =============================================
// 对右下图像逐点扫描:
nLocAvg = 0 ;
// y方向
for(y=height/2; y<height ; y++ )
{
// x方向
for(x=wide/2; x<wide ; x++ )
{
nLocAvg += *(P_data+y*wide + x);
}
}
// 计算均值
nLocAvg /= ( (height - height/2) * (wide - wide/2) ) ;
// 设置阈值为子图象的平均值
nThd[1][1] = nLocAvg ;
// 对右下图像逐点扫描进行分割:
// y方向
for(y=height/2; y<height ; y++ )
{
// x方向
for(x=wide/2; x<wide ; x++ )
{
if(*(P_data+y*wide + x)>nThd[1][1])
*(P_data+y*wide + x) = 255 ;
else
{
*(P_data+y*wide + x) = 0 ;
}
}
}
}
void TiDuRuiHuaDib::Diedaifenge(int *tongji)
{
// 循环变量
LONG i;
LONG j;
// 指向DIB象素指针
LPBYTE p_data;
// 找到DIB图像象素起始位置
p_data = GetData();
// DIB的宽度
LONG wide = this->GetDibWidthBytes();
// DIB的高度
LONG height = this->GetHeight();
// 迭代阀值
int T1, T2;
T1 = 127;
T2 = 0;
// 临时变量
int Temp0, Temp1, Temp2, Temp3;
Temp0 = Temp1 = Temp2 = Temp3 = 0;
while (true)
{
// 计算下一个迭代阀值
for (i = 0; i < T1 + 1; i++)
{
Temp0 += tongji[i] * i;
Temp1 += tongji[i];
}
for (i = T1 + 1; i < 256; i++)
{
Temp2 += tongji[i] * i;
Temp3 += tongji[i];
}
T2 = (Temp0 / Temp1 + Temp2 / Temp3) / 2;
// 看迭代结果是否已收敛
if (T1 == T2)
break;
else
T1 = T2;
}
// 对各像素进行灰度转换
for (j = 0; j < height; j ++)
{
for (i = 0; i < wide; i ++)
{
// 读取像素
unsigned char temp = *((unsigned char *)p_data + wide * j + i);
// 判断像素灰度值是否超出范围
if (temp < T1)
temp = 0;
else
temp = 255;
// 回写处理完的像素
*((unsigned char *)p_data + wide * j + i) = temp;
}
}
}
void TiDuRuiHuaDib::Zhifangtu(float *tongji)
{
// 循环变量
int i;
int j;
// 灰度计数
int huidu[256];
int wide,height; //原图长、宽
// 变量初始化
memset(huidu,0,sizeof(huidu));
wide=this->GetWidth ();
height=this->GetHeight ();
int width= (((wide*24) + 31) / 32 * 4) ;
LPBYTE temp1=new BYTE[wide*height+1024]; //新图像缓冲区
//拷贝原图像到缓存图像
memcpy( temp1,m_pData,wide*height );
// 对各像素进行灰度统计
for (i = 0; i < height; i ++)
{
for (j = 0; j <wide; j ++)
{
unsigned char temp = temp1[wide* i + j] ;
// 灰度统计计数
huidu[temp]++;
}
}
// 计算灰度分布密度
for(i=0;i<256;i++)
tongji[i] = huidu[i] / (height * wide *1.0f);
}
void TiDuRuiHuaDib::Fenbutongji(int *tongji)
{
// 循环变量
LONG i;
LONG j;
//变量初始化
memset(tongji,0,sizeof(int) * 256);
// 指向DIB象素指针
LPBYTE p_data;
// 找到DIB图像象素起始位置
p_data = this->GetData();
// DIB的宽度
LONG wide = GetDibWidthBytes();
// DIB的高度
LONG height = GetHeight();
// 对各像素进行灰度转换
for (j = 0; j < height; j ++)
{
for (i = 0; i <wide; i++)
{
// 对各像素进行灰度统计
unsigned char temp = *((unsigned char *)p_data + wide * j + i);
tongji[temp]++;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -