📄 imageprocessingview.cpp
字号:
for(j=0;j<w;j++)
FD[j*h+i]/=h;
//zhuanzhi & save////////////////////////////////////////
for(i=0;i<w;i++)
for(j=0;j<h;j++)
TD[j*w+i]=FD[i*h+j];//zhuan zhi
//SAVE
pDoc->m_hDFT=::GlobalAlloc(GHND,sizeof(complex<double>)*w*h);
complex<double>* pDFT=(complex<double>*)::GlobalLock(pDoc->m_hDFT);
memcpy(pDFT,TD,sizeof(complex<double>)*w*h);
::GlobalUnlock(pDoc->m_hDFT);
/////////////////////////////////////////////////////////
double dTemp;
//////////////////////////////////////////////////
memcpy(TD,pDFT,sizeof(complex<double>)*w*h);
::GlobalUnlock(pDoc->m_hDFT);
//////////////////////////////////
//first, change pinyu, display pinpu;
//ButterWorth ButterWorth ButterWorth ButterWorth ButterWorth ButterWorth
double PI=4.0*atan(1.0);
double a=0.071,b=0.071;
complex<double> htemp;
for(j=0;j<h;j++)
for(i=0;i<w;i++)
// for(j=0;j<h;j++)
{
if(a*(j-newHeight/2.0)+b*(i-newWidth/2.0)==0)
htemp=complex<double>(1,0);
else
{
// htemp=complex<double>(cos(PI*(b*(j-newHeight/2.0)+a*(i-newWidth/2.0))),-sin(PI*(b*(j-newHeight/2.0)+a*(i-newWidth/2.0))));
// htemp*=(sin(PI*(b*(j-newHeight/2.0)+a*(i-newWidth/2.0))))/(PI*(b*(j-newHeight/2.0)+a*(i-newWidth/2.0)));
htemp=complex<double>(cos(PI*(a*(j-newHeight/2.0)+b*(i-newWidth/2.0))),sin(PI*(a*(j-newHeight/2.0)+b*(i-newWidth/2.0))));
htemp*=(sin(PI*(a*(j-newHeight/2.0)+b*(i-newWidth/2.0))))/(PI*(a*(j-newHeight/2.0)+b*(i-newWidth/2.0)));
}
//下面两个表达式将产生不同的运动效果,
// TD[j*w+i]*=htemp;
TD[(h-1-j)*w+i]*=htemp;
}
//second, IDFT
//////////////////////////////////begin to IDFT
//1. GONG/E
for(i=0;i<w;i++)
for(j=0;j<h;j++)
TD[j*w+i]=complex<double>(TD[j*w+i].real(),-TD[j*w+i].imag());
//2.
for(j=0;j<h;j++)
FFT(&TD[j*w],&FD[j*w],wp);//yan shuping fangxiang fft
for(i=0;i<w;i++)
for(j=0;j<h;j++)
TD[j*w+i]=FD[i*h+j];//zhuan zhi
for(j=0;j<w;j++)
FFT(&TD[j*h],&FD[j*h],hp);//yan shuping fangxiang fft
//now, w hang, h lie
for(i=0;i<h;i++)
for(j=0;j<w;j++)
{
TD[i*w+j]=FD[j*h+i];//zhuan zhi
TD[i*w+j]/=h;
}
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
HDIB hDIBb=(HDIB)::GlobalAlloc(GHND,height*LineBytes+*(LPDWORD)pDIB+256*sizeof(RGBQUAD));
LPSTR pDIB2=(LPSTR)::GlobalLock((HGLOBAL)hDIBb);
memcpy(pDIB2,pDIB,*(LPDWORD)pDIB+sizeof(RGBQUAD)*256);
LPSTR pBits=pDIB2+*(LPDWORD)pDIB+256*sizeof(RGBQUAD);
LPBITMAPINFOHEADER pbmi=(LPBITMAPINFOHEADER)pDIB2;
pbmi->biWidth=width;
pbmi->biHeight=height;
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
for(i=0;i<h;i++)
for(j=0;j<w;j++)
{
//dTemp=abs(TD[i*w+j].real());
dTemp=abs(TD[(h-1-i)*w+j].real());
if(dTemp>255)
dTemp=255;
////////////////////////////////
if(i<height&&j<width)
*(pBits+(height-1-i)*LineBytes+j)=dTemp;
//*(pBits+i*LineBytes+j)=dTemp;
}
delete TD;
delete FD;
////////////////////////////////////
////////////////////////////////////
::GlobalUnlock((HGLOBAL)pDoc->m_hDIB);
::GlobalFree((HGLOBAL)pDoc->m_hDIB);
pDoc->m_hDIB=hDIBb;
pDoc->m_sizeDoc=CSize((int)width,(int)height);
SetScrollSizes(MM_TEXT,pDoc->m_sizeDoc);
/* pDoc->m_sizeDoc=CSize((int)newWidth,(int)newHeight);
SetScrollSizes(MM_TEXT,pDoc->m_sizeDoc);
*/ ////////////////////////////////////////
////////////////////////////////////////
pDoc->UpdateAllViews(NULL);
::GlobalUnlock((HGLOBAL)pDoc->m_hDIB);
}
void CImageProcessingView::OnZft()
{
// TODO: Add your command handler code here
CImageProcessingDoc *pDoc=GetDocument();
if(pDoc->m_hDIB!=0)
{
LPSTR lpDIB;
LPSTR lpDIBBits;
lpDIB=(LPSTR)::GlobalLock((HGLOBAL)pDoc->GetHDIB());
lpDIBBits=::FindDIBBits(lpDIB);
if(::DIBNumColors(lpDIB)!=256)
{
CString strMsg="目前只支持256色位图灰度直方图!";
::MessageBox(NULL,strMsg,"Visual C++图像增强处理",MB_ICONINFORMATION|MB_OK);
::GlobalUnlock((HGLOBAL)pDoc->GetHDIB());
return;
}
BeginWaitCursor();
CZFTDialog dlg;
dlg.m_pDIBBits=lpDIBBits;
dlg.m_nWidth=::DIBWidth(lpDIB);
dlg.m_nHeight=::DIBHeight(lpDIB);
dlg.m_nLow=0;
dlg.m_nUp=255;
if(dlg.DoModal()!=IDOK)
return;
::GlobalUnlock((HGLOBAL)pDoc->GetHDIB());
EndWaitCursor();
}
}
void CImageProcessingView::OnGaussianNoise()
{
// TODO: Add your command handler code here
CImageProcessingDoc* pDoc=GetDocument();
if(pDoc->m_hDIB==0)
return;
LONG width,height,LineBytes;
unsigned char* pBits;
width=(pDoc->m_sizeDoc.cx*8+31)/32*4;
LineBytes=(width*8+31)/32*4;
height=pDoc->m_sizeDoc.cy;
LPSTR pDIB=(LPSTR)::GlobalLock((HGLOBAL)pDoc->m_hDIB);
pBits=(unsigned char*)pDIB+sizeof(BITMAPINFOHEADER)+256*sizeof(RGBQUAD);
double gaiLv[256],stdDeviation,variance;
variance=650;
stdDeviation=sqrt(variance);
int i,j,k;
double temp;
double pi=4.0*atan(1.0);
double sum=0;
for(k=0;k<256;k++)
{
temp=double(k-127);
gaiLv[k]=exp(-(temp*temp/(2.0*variance)));
gaiLv[k]/=(sqrt(2.0*pi)*stdDeviation);
sum+=gaiLv[k];
}
for(k=0;k<256;k++)
{
gaiLv[k]/=sum;
if(k!=0)
gaiLv[k]+=gaiLv[k-1];
}
int *lpNewDIBBits=new int[height*LineBytes];
int max,min;
///////////////////////////////////////////////
//将max,min在循环外赋值,也可以在循环内赋值,见下面的注释
max=min=int(*((unsigned char*)pBits));
srand((unsigned)(time(NULL)));
for(i=0;i<height;i++)
for(j=0;j<width;j++)
{
double num=double(rand()%8092);
double possibility;
possibility=num/8092.0;
for(k=0;k<256&&possibility>gaiLv[k];k++);
k-=1;
lpNewDIBBits[i*LineBytes+j]=(int)pBits[i*LineBytes+j]+k-127;
/////////////////////////////////////////////////////////////////////
//如果将max,min在循环内赋值,则必须用如下if~else形式,否则每次重新循环时,max,min
//都被赋值为lpNewDIBBits[0],程序每次运行都得到不同的结果
// if(i==0&&j==0)
// max=min=lpNewDIBBits[i*LineBytes+j];
// max=min=lpNewDIBBits[0];
// else
{
if(lpNewDIBBits[i*LineBytes+j]>max)
max=lpNewDIBBits[i*LineBytes+j];
if(lpNewDIBBits[i*LineBytes+j]<min)
min=lpNewDIBBits[i*LineBytes+j];
}
}
max-=min;
for(i=0;i<height;i++)
for(j=0;j<width;j++)
{
pBits[i*LineBytes+j]=(BYTE)((lpNewDIBBits[i*LineBytes+j]-min)*255/max);
}
Invalidate();
::GlobalUnlock((HGLOBAL)pDoc->m_hDIB);
}
void CImageProcessingView::OnRGBFanSe()
{
// TODO: Add your command handler code here
CImageProcessingDoc *pDoc=GetDocument();
if(pDoc->m_hDIB!=0)
{
LPSTR lpDIB;
LPSTR lpDIBBits;
lpDIB=(LPSTR)::GlobalLock((HGLOBAL)pDoc->GetHDIB());
LPBITMAPINFOHEADER lpbmi=LPBITMAPINFOHEADER (lpDIB);
lpDIBBits=lpDIB+sizeof(BITMAPINFOHEADER);
if(lpbmi->biBitCount==24)
{
LONG width,height,LineBytes;
width=lpbmi->biWidth;
LineBytes=(width*8+31)/32*4;
height=lpbmi->biHeight;
int i,j;
for(i=0;i<height;i++)
for(j=0;j<width;j++)//注意,width不能写成LineBytes,以下同样如此
{
lpDIBBits[3*(i*width+j)]
=255-lpDIBBits[3*(i*width+j)];
lpDIBBits[3*(i*width+j)+1]
=255-lpDIBBits[3*(i*width+j)+1];
lpDIBBits[3*(i*width+j)+2]
=255-lpDIBBits[3*(i*width+j)+2];
}
}
else
return;
Invalidate();
::GlobalUnlock((HGLOBAL)pDoc->m_hDIB);
}
}
void CImageProcessingView::OnHSIFanSe()
{
// TODO: Add your command handler code here
CImageProcessingDoc *pDoc=GetDocument();
if(pDoc->m_hDIB!=0)
{
LPSTR lpDIB;
LPSTR lpDIBBit;
lpDIB=(LPSTR)::GlobalLock((HGLOBAL)pDoc->GetHDIB());
LPBITMAPINFOHEADER lpbmi=LPBITMAPINFOHEADER(lpDIB);
lpDIBBit=lpDIB+sizeof(BITMAPINFOHEADER);
if(lpbmi->biBitCount==24)
{
LONG width,height,LineBytes;
width=lpbmi->biWidth;
LineBytes=(width*8+31)/32*4;
height=lpbmi->biHeight;
BYTE* lpDIBBits=new BYTE[3*height*LineBytes];
memcpy(lpDIBBits,lpDIBBit,3*height*LineBytes);
int i,j;
double pi=4.0*atan(1.0);
double m_r,m_g,m_b,m_h,m_s,m_i;
for(j=0;j<height;j++)
{
for(i=0;i<width;i++)
{
m_b=(double)lpDIBBits[3*(j*width+i)];
m_b/=255.0;
m_g=(double)lpDIBBits[3*(j*width+i)+1];
m_g/=255.0;
m_r=(double)lpDIBBits[3*(j*width+i)+2];
m_r/=255.0;
////////////////////////////////////////////////////////////////////
//利用式6.2-2求H
if((m_r==m_g)&&(m_b==m_g))
{
// m_h=45.0;
m_h=acos(0.5)/pi*180.0;
}
else
{
m_h=acos(0.5*(m_r+m_r-m_g-m_b)
/sqrt((m_r-m_g)*(m_r-m_g)+(m_r-m_b)*(m_g-m_b)));
m_h=m_h*180.0/pi;
}
if(m_b>m_g)
m_h=360.0-m_h;
//利用式6.2-3求S
if((m_r+m_g+m_b)!=0)
{
m_s=m_g;
if(m_r<m_g)
m_s=m_r;
if(m_b<m_s)
m_s=m_b;
m_s=1.0-3.0*m_s/(m_r+m_g+m_b);
}
else
m_s=0.0;
//利用式6.2-4求I
m_i=(m_r+m_g+m_b)/3.0;
/////////////////////////////////////////////////////////////////
//m_i=1.0-(m_r+m_g+m_b)/3.0;
///////////////////////////////////////////////////
//对HSI图像求补,S不变,H取值0-360
m_i=1.0-m_i;
if(m_h<=180)
m_h+=180.0;
else
m_h-=180.0;
////////////////////////////////////////////////////////////
if(m_h<120.0&&m_h>=0.0)
{
m_h=m_h*pi/180.0;
m_b=m_i*(1.0-m_s);
m_r=m_i*(1.0+m_s*cos(m_h)/cos(pi/3.0-m_h));
m_g=3.0*m_i-m_r-m_b;
}
else if(m_h>=120.0&&m_h<240.0)
{
m_h=(m_h-120.0)*pi/180.0;
m_r=m_i*(1.0-m_s);
m_g=m_i*(1.0+m_s*cos(m_h)/cos(pi/3.0-m_h));
m_b=3.0*m_i-m_r-m_g;
}
else
{
m_h=(m_h-240.0)*pi/180.0;
m_g=m_i*(1.0-m_s);
m_b=m_i*(1.0+m_s*cos(m_h)/cos(pi/3.0-m_h));
m_r=3.0*m_i-m_g-m_b;
}
if(m_r>1)
m_r=1;
if(m_g>1)
m_g=1;
if(m_b>1)
m_b=1;
if(m_r<0)
m_r=0;
if(m_g<0)
m_g=0;
if(m_b<0)
m_b=0;
//////////////////////////////////////////////////////////
lpDIBBit[3*(j*width+i)+0]=(BYTE)(m_b*255.0);
lpDIBBit[3*(j*width+i)+1]=(BYTE)(m_g*255.0);
lpDIBBit[3*(j*width+i)+2]=(BYTE)(m_r*255.0);
}
}
}
else
return;
Invalidate();
::GlobalUnlock((HGLOBAL)pDoc->m_hDIB);
}
}
void CImageProcessingView::OnTextureSeg()
{
// TODO: Add your command handler code here
CImageProcessingDoc *pDoc=GetDocument();
if(pDoc->m_hDIB!=0)
{
LPSTR lpDIB;
LPSTR lpDIBBits;
lpDIB=(LPSTR)::GlobalLock((HGLOBAL)pDoc->GetHDIB());
LPBITMAPINFOHEADER lpbmi=LPBITMAPINFOHEADER(lpDIB);
lpDIBBits=lpDIB+256*sizeof(RGBQUAD)+sizeof(BITMAPINFOHEADER);
LONG width,height,LineBytes;
width=lpbmi->biWidth;
LineBytes=(width*8+31)/32*4;
height=lpbmi->biHeight;
MorphologicalClose(lpDIBBits,width,height);
MorphologicalOpen(lpDIBBits,width,height);
/* /////////////////////////////////////////////////
LONG i,j;
int m_count;
for(j=0;j<height;j++)
{
m_count=0;
for(i=0;i<width-3;i++)
{
if(abs(lpDIBBits[j*LineBytes+i]-lpDIBBits[j*LineBytes+i+3])>20&&m_count==0)
{
//m_base.m_lpbit[j*m_width2+i-1]=255;
lpDIBBits[j*LineBytes+i]=255;
lpDIBBits[j*LineBytes+i-1]=255;
m_count++;
}
else
lpDIBBits[j*LineBytes+i]=m_base.m_lpbit2[j*LineBytes+i];
}
}
for(j=0;j<height;j++)
{
for(i=width-3;i<width;i++)
{
lpDIBBits[j*LineBytes+i]=m_base.m_lpbit2[j*LineBytes+i];
}
}
*/ /////////////////////////////////////////////////
pDoc->UpdateAllViews(NULL);
}
::GlobalUnlock((HGLOBAL)pDoc->m_hDIB);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -