下面是关于C++ Sobel算子的程序代码 相关知识了解 语句看不懂 希望大神能帮我注释下 方便我理解 谢谢
//Sobel算子BOOLSySobel(HDIBhDib){if(hDib==NULL)returnFALSE;LPBYTElpBits=FindDIBBits((LP...
//Sobel算子
BOOL SySobel(HDIB hDib)
{
if(hDib==NULL)return FALSE;
LPBYTE lpBits=FindDIBBits((LPBYTE)GlobalLock(hDib));
BITMAPINFO *bmi = (BITMAPINFO *)GlobalLock(hDib);
LPBITMAPINFOHEADER lpbi = (LPBITMAPINFOHEADER)bmi;
int nx,ny;
int height,width;
// get color number
WORD wNumColors = DIBNumColors((LPBYTE)bmi);
height=bmi->bmiHeader.biHeight;
width=bmi->bmiHeader.biWidth;
double gray;
double grayl;
double grayr;
BYTE **logImg;//二维缓冲区
logImg=new BYTE *[height];
for(ny=0;ny<height;ny++)
logImg[ny]=new BYTE [width];
BYTE **logImg1;//二维缓冲区
logImg1=new BYTE *[height];
for(ny=0;ny<height;ny++)
logImg1[ny]=new BYTE [width];
BYTE **logImg2;//二维缓冲区
logImg2=new BYTE *[height];
for(ny=0;ny<height;ny++)
logImg2[ny]=new BYTE [width];
lpBits=(LPBYTE)lpbi+lpbi->biSize+(int)wNumColors*sizeof(RGBQUAD);//获取数据矩阵指针
int nDelta = WIDTHBYTES(lpbi->biBitCount*lpbi->biWidth) - lpbi->biWidth*lpbi->biBitCount/8;
for ( ny=0; ny<height; ny++)
{
for ( nx=0; nx<width; nx++)
{
gray=*(lpBits);
logImg[ny][nx]=(BYTE)gray;
logImg1[ny][nx]=(BYTE)gray;
logImg2[ny][nx]=(BYTE)gray;
lpBits++;
}
lpBits += nDelta;
}
for( ny=1; ny<(height-1); ny++)
{
for( nx=1; nx<(width-1); nx++)
{
//gray=(BYTE)sqrt(grayl*grayl+grayr*grayr);
grayr=fabs(grayr);
grayl=fabs(grayl);
if (grayr>255)
logImg1[ny][nx]=255;
else
logImg1[ny][nx]=(BYTE)grayr;
if (grayl>255)
logImg2[ny][nx]=255;
else
logImg2[ny][nx]=(BYTE)grayr;
}
}
lpBits=(LPBYTE)lpbi+lpbi->biSize+(int)wNumColors*sizeof(RGBQUAD);//获取数据矩阵指针
for( ny=1; ny<(height-1); ny++)
{
for( nx=1; nx<(width-1); nx++)
{
if(logImg2[ny][nx]>logImg1[ny][nx])
{
logImg1[ny][nx]=logImg2[ny][nx];
}
}
}
for ( ny=0; ny<lpbi->biHeight; ny++)
{
for ( nx=0; nx<lpbi->biWidth; nx++)
{
gray=logImg1[ny][nx];
*(lpBits)=(BYTE)gray;
lpBits++;
}
lpBits += nDelta;
}
for(ny=0;ny<height;ny++)
delete [] logImg[ny];
delete [] logImg;
for(ny=0;ny<height;ny++)
delete [] logImg1[ny];
delete [] logImg1;
for(ny=0;ny<height;ny++)
delete [] logImg2[ny];
delete [] logImg2;
GlobalUnlock(hDib);
//WaitCursorEnd();
return TRUE;
} 展开
BOOL SySobel(HDIB hDib)
{
if(hDib==NULL)return FALSE;
LPBYTE lpBits=FindDIBBits((LPBYTE)GlobalLock(hDib));
BITMAPINFO *bmi = (BITMAPINFO *)GlobalLock(hDib);
LPBITMAPINFOHEADER lpbi = (LPBITMAPINFOHEADER)bmi;
int nx,ny;
int height,width;
// get color number
WORD wNumColors = DIBNumColors((LPBYTE)bmi);
height=bmi->bmiHeader.biHeight;
width=bmi->bmiHeader.biWidth;
double gray;
double grayl;
double grayr;
BYTE **logImg;//二维缓冲区
logImg=new BYTE *[height];
for(ny=0;ny<height;ny++)
logImg[ny]=new BYTE [width];
BYTE **logImg1;//二维缓冲区
logImg1=new BYTE *[height];
for(ny=0;ny<height;ny++)
logImg1[ny]=new BYTE [width];
BYTE **logImg2;//二维缓冲区
logImg2=new BYTE *[height];
for(ny=0;ny<height;ny++)
logImg2[ny]=new BYTE [width];
lpBits=(LPBYTE)lpbi+lpbi->biSize+(int)wNumColors*sizeof(RGBQUAD);//获取数据矩阵指针
int nDelta = WIDTHBYTES(lpbi->biBitCount*lpbi->biWidth) - lpbi->biWidth*lpbi->biBitCount/8;
for ( ny=0; ny<height; ny++)
{
for ( nx=0; nx<width; nx++)
{
gray=*(lpBits);
logImg[ny][nx]=(BYTE)gray;
logImg1[ny][nx]=(BYTE)gray;
logImg2[ny][nx]=(BYTE)gray;
lpBits++;
}
lpBits += nDelta;
}
for( ny=1; ny<(height-1); ny++)
{
for( nx=1; nx<(width-1); nx++)
{
//gray=(BYTE)sqrt(grayl*grayl+grayr*grayr);
grayr=fabs(grayr);
grayl=fabs(grayl);
if (grayr>255)
logImg1[ny][nx]=255;
else
logImg1[ny][nx]=(BYTE)grayr;
if (grayl>255)
logImg2[ny][nx]=255;
else
logImg2[ny][nx]=(BYTE)grayr;
}
}
lpBits=(LPBYTE)lpbi+lpbi->biSize+(int)wNumColors*sizeof(RGBQUAD);//获取数据矩阵指针
for( ny=1; ny<(height-1); ny++)
{
for( nx=1; nx<(width-1); nx++)
{
if(logImg2[ny][nx]>logImg1[ny][nx])
{
logImg1[ny][nx]=logImg2[ny][nx];
}
}
}
for ( ny=0; ny<lpbi->biHeight; ny++)
{
for ( nx=0; nx<lpbi->biWidth; nx++)
{
gray=logImg1[ny][nx];
*(lpBits)=(BYTE)gray;
lpBits++;
}
lpBits += nDelta;
}
for(ny=0;ny<height;ny++)
delete [] logImg[ny];
delete [] logImg;
for(ny=0;ny<height;ny++)
delete [] logImg1[ny];
delete [] logImg1;
for(ny=0;ny<height;ny++)
delete [] logImg2[ny];
delete [] logImg2;
GlobalUnlock(hDib);
//WaitCursorEnd();
return TRUE;
} 展开
1个回答
展开全部
//Sobel算子
BOOL SySobel(HDIB hDib)
{
if(hDib==NULL)return FALSE;
LPBYTE lpBits=FindDIBBits((LPBYTE)GlobalLock(hDib));
BITMAPINFO *bmi = (BITMAPINFO *)GlobalLock(hDib);
LPBITMAPINFOHEADER lpbi = (LPBITMAPINFOHEADER)bmi;
int nx,ny;
int height,width;
// get color number
WORD wNumColors = DIBNumColors((LPBYTE)bmi);
height=bmi->bmiHeader.biHeight;
width=bmi->bmiHeader.biWidth;
double gray;
double grayl;
double grayr;
BYTE **logImg;//二维缓冲区
logImg=new BYTE *[height];
for(ny=0;ny<height;ny++)
logImg[ny]=new BYTE [width];
BYTE **logImg1;//二维缓冲区
logImg1=new BYTE *[height];
for(ny=0;ny<height;ny++)
logImg1[ny]=new BYTE [width];
BYTE **logImg2;//二维缓冲区
logImg2=new BYTE *[height];
for(ny=0;ny<height;ny++)
logImg2[ny]=new BYTE [width];
lpBits=(LPBYTE)lpbi+lpbi->biSize+(int)wNumColors*sizeof(RGBQUAD);//获取数据矩阵指针
int nDelta = WIDTHBYTES(lpbi->biBitCount*lpbi->biWidth) - lpbi->biWidth*lpbi->biBitCount/8;
for ( ny=0; ny<height; ny++)
{
for ( nx=0; nx<width; nx++)
{
gray=*(lpBits);
logImg[ny][nx]=(BYTE)gray;
logImg1[ny][nx]=(BYTE)gray;
logImg2[ny][nx]=(BYTE)gray;
lpBits++;
}
lpBits += nDelta;
}
for( ny=1; ny<(height-1); ny++)
{
for( nx=1; nx<(width-1); nx++)
{
//gray=(BYTE)sqrt(grayl*grayl+grayr*grayr);
grayr=fabs(grayr);
grayl=fabs(grayl);
if (grayr>255)
logImg1[ny][nx]=255;
else
logImg1[ny][nx]=(BYTE)grayr;
if (grayl>255)
logImg2[ny][nx]=255;
else
logImg2[ny][nx]=(BYTE)grayr;
}
}lpBits=(LPBYTE)lpbi+lpbi->biSize+(int)wNumColors*sizeof(RGBQUAD);//获取数据矩阵指针
for( ny=1; ny<(height-1); ny++)
{
for( nx=1; nx<(width-1); nx++)
{
if(logImg2[ny][nx]>logImg1[ny][nx])
{
logImg1[ny][nx]=logImg2[ny][nx];
}
}
}
for ( ny=0; ny<lpbi->biHeight; ny++)
{
for ( nx=0; nx<lpbi->biWidth; nx++)
{
gray=logImg1[ny][nx];
*(lpBits)=(BYTE)gray;
lpBits++;
}
lpBits += nDelta;
}
for(ny=0;ny<height;ny++)
delete [] logImg[ny];
delete [] logImg;
for(ny=0;ny<height;ny++)
delete [] logImg1[ny];
delete [] logImg1;
for(ny=0;ny<height;ny++)
delete [] logImg2[ny];
delete [] logImg2;
GlobalUnlock(hDib);
//WaitCursorEnd();
return TRUE;
}
BOOL SySobel(HDIB hDib)
{
if(hDib==NULL)return FALSE;
LPBYTE lpBits=FindDIBBits((LPBYTE)GlobalLock(hDib));
BITMAPINFO *bmi = (BITMAPINFO *)GlobalLock(hDib);
LPBITMAPINFOHEADER lpbi = (LPBITMAPINFOHEADER)bmi;
int nx,ny;
int height,width;
// get color number
WORD wNumColors = DIBNumColors((LPBYTE)bmi);
height=bmi->bmiHeader.biHeight;
width=bmi->bmiHeader.biWidth;
double gray;
double grayl;
double grayr;
BYTE **logImg;//二维缓冲区
logImg=new BYTE *[height];
for(ny=0;ny<height;ny++)
logImg[ny]=new BYTE [width];
BYTE **logImg1;//二维缓冲区
logImg1=new BYTE *[height];
for(ny=0;ny<height;ny++)
logImg1[ny]=new BYTE [width];
BYTE **logImg2;//二维缓冲区
logImg2=new BYTE *[height];
for(ny=0;ny<height;ny++)
logImg2[ny]=new BYTE [width];
lpBits=(LPBYTE)lpbi+lpbi->biSize+(int)wNumColors*sizeof(RGBQUAD);//获取数据矩阵指针
int nDelta = WIDTHBYTES(lpbi->biBitCount*lpbi->biWidth) - lpbi->biWidth*lpbi->biBitCount/8;
for ( ny=0; ny<height; ny++)
{
for ( nx=0; nx<width; nx++)
{
gray=*(lpBits);
logImg[ny][nx]=(BYTE)gray;
logImg1[ny][nx]=(BYTE)gray;
logImg2[ny][nx]=(BYTE)gray;
lpBits++;
}
lpBits += nDelta;
}
for( ny=1; ny<(height-1); ny++)
{
for( nx=1; nx<(width-1); nx++)
{
//gray=(BYTE)sqrt(grayl*grayl+grayr*grayr);
grayr=fabs(grayr);
grayl=fabs(grayl);
if (grayr>255)
logImg1[ny][nx]=255;
else
logImg1[ny][nx]=(BYTE)grayr;
if (grayl>255)
logImg2[ny][nx]=255;
else
logImg2[ny][nx]=(BYTE)grayr;
}
}lpBits=(LPBYTE)lpbi+lpbi->biSize+(int)wNumColors*sizeof(RGBQUAD);//获取数据矩阵指针
for( ny=1; ny<(height-1); ny++)
{
for( nx=1; nx<(width-1); nx++)
{
if(logImg2[ny][nx]>logImg1[ny][nx])
{
logImg1[ny][nx]=logImg2[ny][nx];
}
}
}
for ( ny=0; ny<lpbi->biHeight; ny++)
{
for ( nx=0; nx<lpbi->biWidth; nx++)
{
gray=logImg1[ny][nx];
*(lpBits)=(BYTE)gray;
lpBits++;
}
lpBits += nDelta;
}
for(ny=0;ny<height;ny++)
delete [] logImg[ny];
delete [] logImg;
for(ny=0;ny<height;ny++)
delete [] logImg1[ny];
delete [] logImg1;
for(ny=0;ny<height;ny++)
delete [] logImg2[ny];
delete [] logImg2;
GlobalUnlock(hDib);
//WaitCursorEnd();
return TRUE;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询