VC6.0图形处理7--边缘检测
2011-09-01 13:20 libing64 阅读(211) 评论(0) 编辑 收藏 举报源码下载:http://download.csdn.net/detail/renshengrumenglibing/3875522
//边缘检测主要的思想是利用灰度变化率,找出变化大的地方,大多采用Sobel算子,Prewitt算子,和LapLacian算子等方式,只是算子不同,其他的均一样
void CBMPViewerDoc::OnMenuitem32793() //Sobel算子
{
// TODO: Add your command handler code here
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 xsum ,ysum ,sum;
int x_template[9] = {-1, 0, 1 ,-2 ,0,2 , -1, 0,1 };
int y_template[9] = {1,2,1,0 , 0 , 0 ,-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{
xsum = 0 ;
ysum = 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;
xsum += (*lpScr) * x_template[3*(m - i +1) + n - j +1];
ysum += (*lpScr) * y_template[3*(m - i +1) + n - j +1];
}
}
sum = (int)sqrt(pow(xsum, 2) + pow(ysum , 2));
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::OnMenuitem32794() //Prewitt算子
{
// TODO: Add your command handler code here
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 xsum ,ysum ,sum;
int x_template[9] = {-1, 0, 1 ,-1 ,0,1 , -1, 0,1 };
int y_template[9] = {1,1,1,0 , 0 , 0 ,-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{
xsum = 0 ;
ysum = 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;
xsum += (*lpScr) * x_template[3*(m - i +1) + n - j +1];
ysum += (*lpScr) * y_template[3*(m - i +1) + n - j +1];
}
}
sum = (int)sqrt(pow(xsum, 2) + pow(ysum , 2));
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::OnMenuitem32795() //Laplacian 算子
{
// TODO: Add your command handler code here
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 xsum ,ysum ,sum;
int x_template[9] = {0, -1, 0 ,-1 ,4,-1 , 0,-1,0 };
int y_template[9] = {-1,-1,-1,-1 , 8 , -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{
xsum = 0 ;
ysum = 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;
xsum += (*lpScr) * x_template[3*(m - i +1) + n - j +1];
ysum += (*lpScr) * y_template[3*(m - i +1) + n - j +1];
}
}
sum = (int)sqrt(pow(xsum, 2) + pow(ysum , 2));
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);
}
//未完待续