VC6.0图形处理6--图像增强
2011-09-01 12:49 libing64 阅读(315) 评论(0) 编辑 收藏 举报源码下载:http://download.csdn.net/detail/renshengrumenglibing/3875522
//本节包含高斯平滑,中值滤波和锐化三个部分,三者均为模板操作,只是模板不同,算法均相同,希望读者仔细研读其中之一,其他两个自然就明白了
void CBMPViewerDoc::OnMenuitem32790() //高斯平滑
{
// TODO: Add your command handler code here
//模板1/16*[1 2 1 ; 2 ,4 ,2 ; 1, 2,1]
int linewidth;
linewidth=(pbi->bmiHeader.biWidth*pbi->bmiHeader.biBitCount+31)/32*4;
HLOCAL hTemp;
hTemp = LocalAlloc(LHND ,linewidth * bi.biHeight );
LPSTR lpTemp;
lpTemp = (char*)LocalLock(hTemp);
unsigned char *lpScr;
unsigned char * lpDest;
int sum ;
int Gauss[9] = {1, 2, 1 , 2 ,4 ,2 , 1, 2,1 };
// TODO: Add your command handler code here
for(int i = 0 ; i< bi.biHeight ; i++){
for(int j = 0 ; j< bi.biWidth ; j++){
lpDest = (unsigned char *)lpTemp + linewidth *(bi.biHeight - i -1) + j;
if((i == 0 ) || (j ==0) || (i == bi.biHeight) || (j == bi.biWidth) ){
lpScr = (unsigned char*)lpBuf + linewidth*(bi.biHeight - i -1) + j;
*lpDest = *lpScr;
}
else{
sum = 0 ;
for(int m = i -1 ; m <= i+1 ;m++){
for(int n = j-1; n<=j+1; n++){
lpScr = (unsigned char*)lpBuf + linewidth*(bi.biHeight - m -1) + n;
sum += (*lpScr) * Gauss[3*(m - i +1) + n - j +1];
}
}
sum/= 16;
if((sum >= 0 )&&(sum <= 255))
{
*lpDest = sum;
}
else if(sum > 255) *lpDest = 255;
else *lpDest= 0;
}
}
}
memcpy(lpBuf, lpTemp, linewidth * bi.biHeight);
// Invalidata(TRUE);
UpdateAllViews(NULL,0,NULL);
}
void CBMPViewerDoc::OnMenuitem32791() //锐化处理
{
//模板1/16*[1 1 1 ; 1 ,1 ,1 ; 1, 1,1]
int linewidth;
linewidth=(pbi->bmiHeader.biWidth*pbi->bmiHeader.biBitCount+31)/32*4;
HLOCAL hTemp;
hTemp = LocalAlloc(LHND ,linewidth * bi.biHeight );
LPSTR lpTemp;
lpTemp = (char*)LocalLock(hTemp);
unsigned char *lpScr;
unsigned char * lpDest;
int sum ;
int m_template[9] = {0, -1, 0 , -1 ,5 ,-1 , 0, -1,0 };
// TODO: Add your command handler code here
for(int i = 0 ; i< bi.biHeight ; i++){
for(int j = 0 ; j< bi.biWidth ; j++){
lpDest = (unsigned char *)lpTemp + linewidth *(bi.biHeight - i -1) + j;
if((i == 0 ) || (j ==0) || (i == bi.biHeight) || (j == bi.biWidth) ){
lpScr = (unsigned char*)lpBuf + linewidth*(bi.biHeight - i -1) + j;
*lpDest = *lpScr;
}
else{
sum = 0 ;
for(int m = i -1 ; m <= i+1 ;m++){
for(int n = j-1; n<=j+1; n++){
lpScr = (unsigned char*)lpBuf + linewidth*(bi.biHeight - m -1) + n;
sum += (*lpScr) * m_template[3*(m - i +1) + n - j +1];
}
}
if((sum >= 0 )&&(sum <= 255))
{
*lpDest = sum;
}
else if(sum > 255) *lpDest = 255;
else *lpDest= 0;
}
}
}
memcpy(lpBuf, lpTemp, linewidth * bi.biHeight);
// Invalidata(TRUE);
UpdateAllViews(NULL,0,NULL);
}
void CBMPViewerDoc::OnMenuitem32792() //中值滤波
{
//模板1/16*[1 1 1 ; 1 ,1 ,1 ; 1, 1,1]
int linewidth;
linewidth=(pbi->bmiHeader.biWidth*pbi->bmiHeader.biBitCount+31)/32*4;
HLOCAL hTemp;
hTemp = LocalAlloc(LHND ,linewidth * bi.biHeight );
LPSTR lpTemp;
lpTemp = (char*)LocalLock(hTemp);
unsigned char *lpScr;
unsigned char * lpDest;
int sum ;
int m_template[9] = {1, 1, 1 , 1 ,1 ,1 , 1, 1,1 };
// TODO: Add your command handler code here
for(int i = 0 ; i< bi.biHeight ; i++){
for(int j = 0 ; j< bi.biWidth ; j++){
lpDest = (unsigned char *)lpTemp + linewidth *(bi.biHeight - i -1) + j;
if((i == 0 ) || (j ==0) || (i == bi.biHeight) || (j == bi.biWidth) ){
lpScr = (unsigned char*)lpBuf + linewidth*(bi.biHeight - i -1) + j;
*lpDest = *lpScr;
}
else{
sum = 0 ;
for(int m = i -1 ; m <= i+1 ;m++){
for(int n = j-1; n<=j+1; n++){
lpScr = (unsigned char*)lpBuf + linewidth*(bi.biHeight - m -1) + n;
sum += (*lpScr) * m_template[3*(m - i +1) + n - j +1];
}
}
sum/= 9;
if((sum >= 0 )&&(sum <= 255))
{
*lpDest = sum;
}
else if(sum > 255) *lpDest = 255;
else *lpDest= 0;
}
}
}
memcpy(lpBuf, lpTemp, linewidth * bi.biHeight);
// Invalidata(TRUE);
UpdateAllViews(NULL,0,NULL);
}