求soble算子和prewitt算子源代码,用C语言编的!用于数字图像处理!
1个回答
展开全部
自己以前图像处理的时候写的,用的是C++, 不过处理流程一样的,可以参考一下
//Soble
void CBmp::RhSobel()
{
double temp[9];
DWORD m_Y=m_pInfo->bmiHeader.biHeight;
DWORD m_X=WIDTH((m_pInfo->bmiHeader.biWidth)*(m_pInfo->bmiHeader.biBitCount));
BYTE *m_B=(BYTE *) new char[m_Y*m_X];
for(int d=0;d<m_nPixels;d++)
{
m_B[d]=m_pPixels[d];
}
if((m_pInfo->bmiHeader.biBitCount)==24)
for(int i=1;i<m_Y-1;i++)
for(int j=3;j<(m_X-2);j+=3)
{
for(int n=0;n<9;n+=3)
{
temp[n]=(m_B[(i-1+n/3)*m_X+j-3]+m_B[(i-1+n/3)*m_X+j-2]+m_B[(i-1+n/3)*m_X+j-1])/3;
temp[n+1]=(m_B[(i-1+n/3)*m_X+j]+m_B[(i-1+n/3)*m_X+j+1]+m_B[(i-1+n/3)*m_X+j+2])/3;
temp[n+2]=(m_B[(i-1+n/3)*m_X+j+3]+m_B[(i-1+n/3)*m_X+j+4]+m_B[(i-1+n/3)*m_X+j+5])/3;
}
m_pPixels[i*m_X+j]=m_pPixels[i*m_X+j+1]=m_pPixels[i*m_X+j+2]=//
(BYTE((abs(temp[2]+2*temp[5]+temp[8]-//
temp[0]-2*temp[3]-temp[6])+
abs(temp[0]+2*temp[1]+temp[2]-//
temp[6]-2*temp[7]-temp[8]))));
}
else
for(int i=1;i<(m_Y-1);i++)
{
for(int j=1;j<(m_X-1);j++)
{
m_pPixels[i*m_X+j]=(abs(m_B[(i-1)*m_X+j+1]+(2*m_B[(i)*m_X+j+1])+m_B[(i+1)*m_X+j+1]-//
m_B[(i-1)*m_X+j-1]-(2*m_B[(i)*m_X+j-1])-m_B[(i+1)*m_X+j-1])+
abs(m_B[(i-1)*m_X+j-1]+(2*m_B[(i-1)*m_X+j])+m_B[(i-1)*m_X+j+1]-//
m_B[(i+1)*m_X+j-1]-(2*m_B[(i+1)*m_X+j])-m_B[(i+1)*m_X+j+1]));
}
}
}
//Prewitt
void CBmp::ByPrewitt()
{
double temp1,temp2;
DWORD m_Y=m_pInfo->bmiHeader.biHeight;
DWORD m_X=WIDTH((m_pInfo->bmiHeader.biWidth)*(m_pInfo->bmiHeader.biBitCount));
BYTE *m_B=(BYTE *) new char[m_Y*m_X];
for(int d=0;d<m_nPixels;d++)
{
m_B[d]=m_pPixels[d];
}
if(m_pInfo->bmiHeader.biBitCount==8)
for(int i=1;i<(m_Y-1);i++)
{
for(int j=1;j<(m_X-1);j++)
{
temp1=abs(m_B[(i-1)*m_X+j+1]-m_B[(i-1)*m_X+j-1]+m_B[i*m_X+j+1]-//
m_B[i*m_X+j-1]+m_B[(i+1)*m_X+j+1]-m_B[(i+1)*m_X+j-1]);
temp2=abs(m_B[(i-1)*m_X+j-1]+m_B[(i-1)*m_X+j]+m_B[(i-1)*m_X+j+1]-//
m_B[(i+1)*m_X+j-1]-m_B[(i+1)*m_X+j]-m_B[(i+1)*m_X+j+1]);
m_pPixels[i*m_X+j]=(temp1>temp2?temp1:temp2);
}
}
else
{
Huidu();
for(int i=1;i<(m_Y-1);i++)
{
for(int j=3;j<(m_X-5);j+=3)
{
temp1=abs(m_B[(i-1)*m_X+j+3]-m_B[(i-1)*m_X+j-3]+m_B[i*m_X+j+3]-//
m_B[i*m_X+j-3]+m_B[(i+1)*m_X+j+3]-m_B[(i+1)*m_X+j-3]);
temp2=abs(m_B[(i-1)*m_X+j-3]+m_B[(i-1)*m_X+j]+m_B[(i-1)*m_X+j+3]-//
m_B[(i+1)*m_X+j-3]-m_B[(i+1)*m_X+j]-m_B[(i+1)*m_X+j+3]);
m_pPixels[i*m_X+j]=m_pPixels[i*m_X+j+1]=m_pPixels[i*m_X+j+2]=(temp1>temp2?temp1:temp2);
}
}
}
}
//Soble
void CBmp::RhSobel()
{
double temp[9];
DWORD m_Y=m_pInfo->bmiHeader.biHeight;
DWORD m_X=WIDTH((m_pInfo->bmiHeader.biWidth)*(m_pInfo->bmiHeader.biBitCount));
BYTE *m_B=(BYTE *) new char[m_Y*m_X];
for(int d=0;d<m_nPixels;d++)
{
m_B[d]=m_pPixels[d];
}
if((m_pInfo->bmiHeader.biBitCount)==24)
for(int i=1;i<m_Y-1;i++)
for(int j=3;j<(m_X-2);j+=3)
{
for(int n=0;n<9;n+=3)
{
temp[n]=(m_B[(i-1+n/3)*m_X+j-3]+m_B[(i-1+n/3)*m_X+j-2]+m_B[(i-1+n/3)*m_X+j-1])/3;
temp[n+1]=(m_B[(i-1+n/3)*m_X+j]+m_B[(i-1+n/3)*m_X+j+1]+m_B[(i-1+n/3)*m_X+j+2])/3;
temp[n+2]=(m_B[(i-1+n/3)*m_X+j+3]+m_B[(i-1+n/3)*m_X+j+4]+m_B[(i-1+n/3)*m_X+j+5])/3;
}
m_pPixels[i*m_X+j]=m_pPixels[i*m_X+j+1]=m_pPixels[i*m_X+j+2]=//
(BYTE((abs(temp[2]+2*temp[5]+temp[8]-//
temp[0]-2*temp[3]-temp[6])+
abs(temp[0]+2*temp[1]+temp[2]-//
temp[6]-2*temp[7]-temp[8]))));
}
else
for(int i=1;i<(m_Y-1);i++)
{
for(int j=1;j<(m_X-1);j++)
{
m_pPixels[i*m_X+j]=(abs(m_B[(i-1)*m_X+j+1]+(2*m_B[(i)*m_X+j+1])+m_B[(i+1)*m_X+j+1]-//
m_B[(i-1)*m_X+j-1]-(2*m_B[(i)*m_X+j-1])-m_B[(i+1)*m_X+j-1])+
abs(m_B[(i-1)*m_X+j-1]+(2*m_B[(i-1)*m_X+j])+m_B[(i-1)*m_X+j+1]-//
m_B[(i+1)*m_X+j-1]-(2*m_B[(i+1)*m_X+j])-m_B[(i+1)*m_X+j+1]));
}
}
}
//Prewitt
void CBmp::ByPrewitt()
{
double temp1,temp2;
DWORD m_Y=m_pInfo->bmiHeader.biHeight;
DWORD m_X=WIDTH((m_pInfo->bmiHeader.biWidth)*(m_pInfo->bmiHeader.biBitCount));
BYTE *m_B=(BYTE *) new char[m_Y*m_X];
for(int d=0;d<m_nPixels;d++)
{
m_B[d]=m_pPixels[d];
}
if(m_pInfo->bmiHeader.biBitCount==8)
for(int i=1;i<(m_Y-1);i++)
{
for(int j=1;j<(m_X-1);j++)
{
temp1=abs(m_B[(i-1)*m_X+j+1]-m_B[(i-1)*m_X+j-1]+m_B[i*m_X+j+1]-//
m_B[i*m_X+j-1]+m_B[(i+1)*m_X+j+1]-m_B[(i+1)*m_X+j-1]);
temp2=abs(m_B[(i-1)*m_X+j-1]+m_B[(i-1)*m_X+j]+m_B[(i-1)*m_X+j+1]-//
m_B[(i+1)*m_X+j-1]-m_B[(i+1)*m_X+j]-m_B[(i+1)*m_X+j+1]);
m_pPixels[i*m_X+j]=(temp1>temp2?temp1:temp2);
}
}
else
{
Huidu();
for(int i=1;i<(m_Y-1);i++)
{
for(int j=3;j<(m_X-5);j+=3)
{
temp1=abs(m_B[(i-1)*m_X+j+3]-m_B[(i-1)*m_X+j-3]+m_B[i*m_X+j+3]-//
m_B[i*m_X+j-3]+m_B[(i+1)*m_X+j+3]-m_B[(i+1)*m_X+j-3]);
temp2=abs(m_B[(i-1)*m_X+j-3]+m_B[(i-1)*m_X+j]+m_B[(i-1)*m_X+j+3]-//
m_B[(i+1)*m_X+j-3]-m_B[(i+1)*m_X+j]-m_B[(i+1)*m_X+j+3]);
m_pPixels[i*m_X+j]=m_pPixels[i*m_X+j+1]=m_pPixels[i*m_X+j+2]=(temp1>temp2?temp1:temp2);
}
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询