📄 bmpchangeview.cpp
字号:
delete []m_cpBuffer;
if(U!=NULL)
delete []U;
}
}
void CBMPChangeView::OnRetrorseFilter()
{
if(m_bCanConv==true)
{
m_bCanConv=false;
m_cpBuffer=new unsigned char [m_dwFileLen];
m_fFile.Read(m_cpBuffer,m_dwFileLen);
if(m_cpBuffer[28]!=24)
{
AfxMessageBox("请打开24位BMP位图!");
m_fFile.Close();
if(m_cpBuffer!=NULL)
delete []m_cpBuffer;
return;
}
CDSP dsp;
Complex* U;
int WM=(int)(log(m_nWidth)/log(2)+1.0f);
int HM=(int)(log(m_nHeight)/log(2)+1.0f);
WM=HM=max(WM,HM);
int WN=(int)pow(2,WM);
int HN=(int)pow(2,HM);
U=new Complex[WN*HN*3];
dsp.DFT_2D_FFT(m_cpBuffer+54,m_nWidth,m_nHeight,U);
float D0=250.0f;
float D1;
for(int i=0;i<HN;i++)
{
for(int j=0;j<WN*3;j++)
{
int k=(int)(j/3);
D1=(float)sqrt(i*i+k*k);
float H=1.0f/(1+(D1/D0)*(D1/D0));
if(H>0.45f)
{
U[i*3*WN+j].Re/=H;
U[i*3*WN+j].Im/=H;
}
else
{
U[i*3*WN+j].Re*=0.6f;
U[i*3*WN+j].Im*=0.6f;
}
}
}
dsp.DFT_2D_IFFT(m_cpBuffer+54,m_nWidth,m_nHeight,U);
m_fFile.SeekToBegin();
m_fFile.Write(m_cpBuffer,m_dwFileLen);
m_fFile.Close();
if(m_cpBuffer!=NULL)
delete []m_cpBuffer;
if(U!=NULL)
delete []U;
}
}
void CBMPChangeView::OnWienerFilter()
{
if(m_bCanConv==true)
{
m_bCanConv=false;
m_cpBuffer=new unsigned char [m_dwFileLen];
m_fFile.Read(m_cpBuffer,m_dwFileLen);
if(m_cpBuffer[28]!=24)
{
AfxMessageBox("请打开24位BMP位图!");
m_fFile.Close();
if(m_cpBuffer!=NULL)
delete []m_cpBuffer;
return;
}
CDSP dsp;
Complex* U;
int WM=(int)(log(m_nWidth)/log(2)+1.0f);
int HM=(int)(log(m_nHeight)/log(2)+1.0f);
WM=HM=max(WM,HM);
int WN=(int)pow(2,WM);
int HN=(int)pow(2,HM);
U=new Complex[WN*HN*3];
dsp.DFT_2D_FFT(m_cpBuffer+54,m_nWidth,m_nHeight,U);
float D0=250.0f;
float D1;
float K=0.05f;
for(int i=0;i<HN;i++)
{
for(int j=0;j<WN*3;j++)
{
int k=(int)(j/3);
D1=(float)sqrt(i*i+k*k);
float H=1.0f/(1+(D1/D0)*(D1/D0));
U[i*3*WN+j].Re=(U[i*3*WN+j].Re*H)/(H*H+K);
U[i*3*WN+j].Im=(U[i*3*WN+j].Im*H)/(H*H+K);
}
}
dsp.DFT_2D_IFFT(m_cpBuffer+54,m_nWidth,m_nHeight,U);
m_fFile.SeekToBegin();
m_fFile.Write(m_cpBuffer,m_dwFileLen);
m_fFile.Close();
if(m_cpBuffer!=NULL)
delete []m_cpBuffer;
if(U!=NULL)
delete []U;
}
}
void CBMPChangeView::OnSobel()
{
if(m_bCanConv==true)
{
m_bCanConv=false;
m_cpBuffer=new unsigned char [m_dwFileLen];
m_fFile.Read(m_cpBuffer,m_dwFileLen);
if(m_cpBuffer[28]!=24)
{
AfxMessageBox("请打开24位BMP位图!");
m_fFile.Close();
if(m_cpBuffer!=NULL)
delete []m_cpBuffer;
return;
}
//不能用char,也不能用::strcpy()
unsigned char* m_temp;
int v_r_v,v_g_v,v_b_v,g_v;
int v_r_h,v_g_h,v_b_h,g_h;
m_temp=new unsigned char [m_dwFileLen-55];
for(int p=55;p<m_dwFileLen;p++)
m_temp[p-55]=m_cpBuffer[p];
//3X3 模版
for(int i=0;i<m_nWidth;i++) //被处理像素在i列
{
for(int j=0;j<m_nHeight;j++) //被处理像素在j行
{
v_r_v=v_g_v=v_b_v=v_r_h=v_g_h=v_b_h=0;
for(int k=i-1;k<i+2;k++) //3*3模版
{
for(int l=j-1;l<j+2;l++)
{
if(k==i-1)
{
if(l==j)
g_v=-2;
else
g_v=-1;
}
if(k==i+1)
{
if(l==j)
g_v=2;
else
g_v=1;
}
if(k==i)
g_v=0;
if(l==j-1)
{
if(k==i)
g_h=2;
else
g_h=1;
}
if(l==j+1)
{
if(k==i)
g_h=-2;
else
g_h=-1;
}
if(l==j)
g_h=0;
if(m_cpBuffer[55+l*m_nWidth*3+k*3]<256 || m_cpBuffer[55+l*m_nWidth*3+k*3]>0)
{
v_r_v+=g_v*m_cpBuffer[55+l*m_nWidth*3+k*3];
v_r_h+=g_h*m_cpBuffer[55+l*m_nWidth*3+k*3];
}
if(m_cpBuffer[55+l*m_nWidth*3+k*3+1]<256 || m_cpBuffer[55+l*m_nWidth*3+k*3+1]>0)
{
v_g_v+=g_v*m_cpBuffer[55+l*m_nWidth*3+k*3+1];
v_g_h+=g_h*m_cpBuffer[55+l*m_nWidth*3+k*3+1];
}
if(m_cpBuffer[55+l*m_nWidth*3+k*3+2]<256 || m_cpBuffer[55+l*m_nWidth*3+k*3+2]>0)
{
v_b_v+=g_v*m_cpBuffer[55+l*m_nWidth*3+k*3+2];
v_b_h+=g_h*m_cpBuffer[55+l*m_nWidth*3+k*3+2];
}
}
}
m_temp[j*m_nWidth*3+i*3]=(int)(sqrt(v_r_v*v_r_v+v_r_h*v_r_h)/9);
m_temp[j*m_nWidth*3+i*3+1]=(int)(sqrt(v_g_v*v_g_v+v_g_h*v_g_h)/9);
m_temp[j*m_nWidth*3+i*3+2]=(int)(sqrt(v_b_v*v_b_v+v_b_h*v_b_h)/9);
}
}
for(p=55;p<m_dwFileLen;p++)
m_cpBuffer[p]=m_temp[p-55];
m_fFile.SeekToBegin();
m_fFile.Write(m_cpBuffer,m_dwFileLen);
m_fFile.Close();
if(m_cpBuffer!=NULL)
delete []m_cpBuffer;
if(m_temp!=NULL)
delete []m_temp;
}
//需要再进行直方图均衡化处理
}
void CBMPChangeView::OnPrewitt()
{
if(m_bCanConv==true)
{
m_bCanConv=false;
m_cpBuffer=new unsigned char [m_dwFileLen];
m_fFile.Read(m_cpBuffer,m_dwFileLen);
if(m_cpBuffer[28]!=24)
{
AfxMessageBox("请打开24位BMP位图!");
m_fFile.Close();
if(m_cpBuffer!=NULL)
delete []m_cpBuffer;
return;
}
//不能用char,也不能用::strcpy()
unsigned char* m_temp;
int v_r_v,v_g_v,v_b_v,g_v;
int v_r_h,v_g_h,v_b_h,g_h;
m_temp=new unsigned char [m_dwFileLen-55];
for(int p=55;p<m_dwFileLen;p++)
m_temp[p-55]=m_cpBuffer[p];
//3X3 模版
for(int i=0;i<m_nWidth;i++) //被处理像素在i列
{
for(int j=0;j<m_nHeight;j++) //被处理像素在j行
{
v_r_v=v_g_v=v_b_v=v_r_h=v_g_h=v_b_h=0;
for(int k=i-1;k<i+2;k++) //3*3模版
{
for(int l=j-1;l<j+2;l++)
{
if(k==i-1)
g_v=-1;
if(k==i+1)
g_v=1;
if(k==i)
g_v=0;
if(l==j-1)
g_h=1;
if(l==j+1)
g_h=-1;
if(l==j)
g_h=0;
if(m_cpBuffer[55+l*m_nWidth*3+k*3]<256 || m_cpBuffer[55+l*m_nWidth*3+k*3]>0)
{
v_r_v+=g_v*m_cpBuffer[55+l*m_nWidth*3+k*3];
v_r_h+=g_h*m_cpBuffer[55+l*m_nWidth*3+k*3];
}
if(m_cpBuffer[55+l*m_nWidth*3+k*3+1]<256 || m_cpBuffer[55+l*m_nWidth*3+k*3+1]>0)
{
v_g_v+=g_v*m_cpBuffer[55+l*m_nWidth*3+k*3+1];
v_g_h+=g_h*m_cpBuffer[55+l*m_nWidth*3+k*3+1];
}
if(m_cpBuffer[55+l*m_nWidth*3+k*3+2]<256 || m_cpBuffer[55+l*m_nWidth*3+k*3+2]>0)
{
v_b_v+=g_v*m_cpBuffer[55+l*m_nWidth*3+k*3+2];
v_b_h+=g_h*m_cpBuffer[55+l*m_nWidth*3+k*3+2];
}
}
}
m_temp[j*m_nWidth*3+i*3]=(int)(sqrt(v_r_v*v_r_v+v_r_h*v_r_h)/9);
m_temp[j*m_nWidth*3+i*3+1]=(int)(sqrt(v_g_v*v_g_v+v_g_h*v_g_h)/9);
m_temp[j*m_nWidth*3+i*3+2]=(int)(sqrt(v_b_v*v_b_v+v_b_h*v_b_h)/9);
}
}
for(p=55;p<m_dwFileLen;p++)
m_cpBuffer[p]=m_temp[p-55];
m_fFile.SeekToBegin();
m_fFile.Write(m_cpBuffer,m_dwFileLen);
m_fFile.Close();
if(m_cpBuffer!=NULL)
delete []m_cpBuffer;
if(m_temp!=NULL)
delete []m_temp;
}
//需要再进行直方图均衡化处理
}
void CBMPChangeView::OnRoberts()
{
if(m_bCanConv==true)
{
m_bCanConv=false;
m_cpBuffer=new unsigned char [m_dwFileLen];
m_fFile.Read(m_cpBuffer,m_dwFileLen);
if(m_cpBuffer[28]!=24)
{
AfxMessageBox("请打开24位BMP位图!");
m_fFile.Close();
if(m_cpBuffer!=NULL)
delete []m_cpBuffer;
return;
}
//不能用char,也不能用::strcpy()
unsigned char* m_temp;
int v_r_v,v_g_v,v_b_v,g_v;
int v_r_h,v_g_h,v_b_h,g_h;
m_temp=new unsigned char [m_dwFileLen-55];
for(int p=55;p<m_dwFileLen;p++)
m_temp[p-55]=m_cpBuffer[p];
//2X2 模版
for(int i=0;i<m_nWidth;i++) //被处理像素在i列
{
for(int j=0;j<m_nHeight;j++) //被处理像素在j行
{
v_r_v=v_g_v=v_b_v=v_r_h=v_g_h=v_b_h=0;
for(int k=i-1;k<i+1;k++) //2*2模版
{
for(int l=j-1;l<j+1;l++)
{
if(k==i-1 && l==j-1)
g_v=1;
else if(k==i && l==j)
g_v=-1;
else
g_v=0;
if(k==i-1 && l==j)
g_h=-1;
else if(k==i && l==j-1)
g_h=1;
else
g_h=0;
if(m_cpBuffer[55+l*m_nWidth*3+k*3]<256 || m_cpBuffer[55+l*m_nWidth*3+k*3]>0)
{
v_r_v+=g_v*m_cpBuffer[55+l*m_nWidth*3+k*3];
v_r_h+=g_h*m_cpBuffer[55+l*m_nWidth*3+k*3];
}
if(m_cpBuffer[55+l*m_nWidth*3+k*3+1]<256 || m_cpBuffer[55+l*m_nWidth*3+k*3+1]>0)
{
v_g_v+=g_v*m_cpBuffer[55+l*m_nWidth*3+k*3+1];
v_g_h+=g_h*m_cpBuffer[55+l*m_nWidth*3+k*3+1];
}
if(m_cpBuffer[55+l*m_nWidth*3+k*3+2]<256 || m_cpBuffer[55+l*m_nWidth*3+k*3+2]>0)
{
v_b_v+=g_v*m_cpBuffer[55+l*m_nWidth*3+k*3+2];
v_b_h+=g_h*m_cpBuffer[55+l*m_nWidth*3+k*3+2];
}
}
}
m_temp[j*m_nWidth*3+i*3]=(int)(sqrt(v_r_v*v_r_v+v_r_h*v_r_h)/4);
m_temp[j*m_nWidth*3+i*3+1]=(int)(sqrt(v_g_v*v_g_v+v_g_h*v_g_h)/4);
m_temp[j*m_nWidth*3+i*3+2]=(int)(sqrt(v_b_v*v_b_v+v_b_h*v_b_h)/4);
}
}
for(p=55;p<m_dwFileLen;p++)
m_cpBuffer[p]=m_temp[p-55];
m_fFile.SeekToBegin();
m_fFile.Write(m_cpBuffer,m_dwFileLen);
m_fFile.Close();
if(m_cpBuffer!=NULL)
delete []m_cpBuffer;
if(m_temp!=NULL)
delete []m_temp;
}
//需要再进行直方图均衡化处理
}
void CBMPChangeView::OnLaplacian()
{
if(m_bCanConv==true)
{
m_bCanConv=false;
m_cpBuffer=new unsigned char [m_dwFileLen];
m_fFile.Read(m_cpBuffer,m_dwFileLen);
if(m_cpBuffer[28]!=24)
{
AfxMessageBox("请打开24位BMP位图!");
m_fFile.Close();
if(m_cpBuffer!=NULL)
delete []m_cpBuffer;
return;
}
//不能用char,也不能用::strcpy()
unsigned char* m_temp;
int v_r,v_g,v_b,p_g;
m_temp=new unsigned char [m_dwFileLen-55];
int g[9]={0,-1,0,-1,4,-1,0,-1,0};
//3X3 模版
for(int i=0;i<m_nWidth;i++) //被处理像素在i列
{
for(int j=0;j<m_nHeight;j++) //被处理像素在j行
{
v_r=v_g=v_b=p_g=0;
for(int k=i-1;k<i+2;k++) //3*3模版
{
for(int l=j-1;l<j+2;l++)
{
if(m_cpBuffer[55+l*m_nWidth*3+k*3]<256 || m_cpBuffer[55+l*m_nWidth*3+k*3]>0)
v_r+=g[p_g]*m_cpBuffer[55+l*m_nWidth*3+k*3];
if(m_cpBuffer[55+l*m_nWidth*3+k*3+1]<256 || m_cpBuffer[55+l*m_nWidth*3+k*3+1]>0)
v_g+=g[p_g]*m_cpBuffer[55+l*m_nWidth*3+k*3+1];
if(m_cpBuffer[55+l*m_nWidth*3+k*3+2]<256 || m_cpBuffer[55+l*m_nWidth*3+k*3+2]>0)
v_b+=g[p_g]*m_cpBuffer[55+l*m_nWidth*3+k*3+2];
p_g++;
}
}
v_r/=9;
v_g/=9;
v_b/=9;
if(v_r<0)
v_r=0;
if(v_g<0)
v_g=0;
if(v_b<0)
v_b=0;
m_temp[j*m_nWidth*3+i*3]=v_r;
m_temp[j*m_nWidth*3+i*3+1]=v_g;
m_temp[j*m_nWidth*3+i*3+2]=v_b;
}
}
for(int p=55;p<m_dwFileLen;p++)
m_cpBuffer[p]=m_temp[p-55];
m_fFile.SeekToBegin();
m_fFile.Write(m_cpBuffer,m_dwFileLen);
m_fFile.Close();
if(m_cpBuffer!=NULL)
delete []m_cpBuffer;
if(m_temp!=NULL)
delete []m_temp;
}
//需要再进行直方图均衡化处理
}
void CBMPChangeView::OnGuassLaplacian()
{
if(m_bCanConv==true)
{
m_bCanConv=false;
m_cpBuffer=new unsigned char [m_dwFileLen];
m_fFile.Read(m_cpBuffer,m_dwFileLen);
if(m_cpBuffer[28]!=24)
{
AfxMessageBox("请打开24位BMP位图!");
m_fFile.Close();
if(m_cpBuffer!=NULL)
delete []m_cpBuffer;
return;
}
//不能用char,也不能用::strcpy()
unsigned char* m_temp;
int v_r,v_g,v_b,p_g;
m_temp=new unsigned char [m_dwFileLen-55];
int g[25]={-2,-4,-4,-4,-2,
-4, 0, 8, 0,-4,
-4, 8,24, 8,-4,
-4, 0, 8, 0,-4,
-2,-4,-4,-4,-2};
//5X5 模版
for(int i=0;i<m_nWidth;i++) //被处理像素在i列
{
for(int j=0;j<m_nHeight;j++) //被处理像素在j行
{
v_r=v_g=v_b=p_g=0;
for(int k=i-2;k<i+3;k++) //5*5模版
{
for(int l=j-2;l<j+3;l++)
{
if(m_cpBuffer[55+l*m_nWidth*3+k*3]<256 || m_cpBuffer[55+l*m_nWidth*3+k*3]>0)
v_r+=g[p_g]*m_cpBuffer[55+l*m_nWidth*3+k*3];
if(m_cpBuffer[55+l*m_nWidth*3+k*3+1]<256 || m_cpBuffer[55+l*m_nWidth*3+k*3+1]>0)
v_g+=g[p_g]*m_cpBuffer[55+l*m_nWidth*3+k*3+1];
if(m_cpBuffer[55+l*m_nWidth*3+k*3+2]<256 || m_cpBuffer[55+l*m_nWidth*3+k*3+2]>0)
v_b+=g[p_g]*m_cpBuffer[55+l*m_nWidth*3+k*3+2];
p_g++;
}
}
v_r/=25;
v_g/=25;
v_b/=25;
if(v_r<0)
v_r=0;
if(v_g<0)
v_g=0;
if(v_b<0)
v_b=0;
m_temp[j*m_nWidth*3+i*3]=v_r;
m_temp[j*m_nWidth*3+i*3+1]=v_g;
m_temp[j*m_nWidth*3+i*3+2]=v_b;
}
}
for(int p=55;p<m_dwFileLen;p++)
m_cpBuffer[p]=m_temp[p-55];
m_fFile.SeekToBegin();
m_fFile.Write(m_cpBuffer,m_dwFileLen);
m_fFile.Close();
if(m_cpBuffer!=NULL)
delete []m_cpBuffer;
if(m_temp!=NULL)
delete []m_temp;
}
//需要再进行直方图均衡化处理
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -