图像的梯度锐化vc代码

void CSDIELSView::OnSharpeningGradient()
 {
//程序编制:李立宗  lilizong@gmail.com
//2012-8-11
  if(myImage1.IsNull())
   OnOpenResourceFile();
  if(!myImage2.IsNull())
   myImage2.Destroy();
  if(myImage2.IsNull()){
   myImage2.Create(myImage1.GetWidth(),myImage1.GetHeight(),24,0);
  }
  //COLORREF pixel;
  int maxY = myImage1.GetHeight();
  int maxX=myImage1.GetWidth();
  byte* pRealData;
  byte* pRealData2;
  pRealData=(byte*)myImage1.GetBits();
  pRealData2=(byte*)myImage2.GetBits();
  int pit=myImage1.GetPitch();
  int pit2=myImage2.GetPitch();
  //需要注意,pit和pit2的值并不一样,所以如果使用一个值,会导致不同的结果出现
  //CString str;
  //str.Format(TEXT("%d"),pit);
  //MessageBox(str);
  //str.Format(TEXT("%d"),pit2);
  //MessageBox(str);
  int bitCount=myImage1.GetBPP()/8;
  int bitCount2=myImage2.GetBPP()/8;
  int tempR,tempG,tempB;
  int temp,tempX,tempY;
  //int M[3][3]={{1,2,1},{2,4,2},{1,2,1}};
  int t=100; //门限
  // tempR=tempG=tempG=0;
  //说明:将生产的图像作为24位图处理。
  for (int y=1; y<maxY-1; y++) {
   for (int x=1; x<maxX-1; x++) {
    temp=(int)sqrt((float)(
     (*(pRealData+pit*(y)+(x)*bitCount)-*(pRealData+pit*(y)+(x-1)*bitCount))*
     (*(pRealData+pit*(y)+(x)*bitCount)-*(pRealData+pit*(y)+(x-1)*bitCount))+
     (*(pRealData+pit*(y)+(x)*bitCount)-*(pRealData+pit*(y-1)+(x)*bitCount))*
     (*(pRealData+pit*(y)+(x)*bitCount)-*(pRealData+pit*(y-1)+(x)*bitCount))));
    if(temp>=t)
    {
     if(temp+100>255)
      tempR=255;
     else
      tempR=temp+100;
    }
    else
     tempR=*(pRealData+pit*(y)+(x)*bitCount);
    if(bitCount==1)
    {
     tempG=tempR;
     tempB=tempR;
    }
    else
    {
     temp=(int)sqrt((float)(
      (*(pRealData+pit*(y)+(x)*bitCount+1)-*(pRealData+pit*(y)+(x-1)*bitCount+1))*
      (*(pRealData+pit*(y)+(x)*bitCount+1)-*(pRealData+pit*(y)+(x-1)*bitCount+1))+
      (*(pRealData+pit*(y)+(x)*bitCount+1)-*(pRealData+pit*(y-1)+(x)*bitCount+1))*
      (*(pRealData+pit*(y)+(x)*bitCount+1)-*(pRealData+pit*(y-1)+(x)*bitCount+1))));
     if(temp>=t)
     {
      if(temp+100>255)
       tempG=255;
      else
       tempG=temp+100;
     }
     else
      tempG=*(pRealData+pit*(y)+(x)*bitCount+1);
     temp=(int)sqrt((float)(
      (*(pRealData+pit*(y)+(x)*bitCount+2)-*(pRealData+pit*(y)+(x-1)*bitCount+2))*
      (*(pRealData+pit*(y)+(x)*bitCount+2)-*(pRealData+pit*(y)+(x-1)*bitCount+2))+
      (*(pRealData+pit*(y)+(x)*bitCount+2)-*(pRealData+pit*(y-1)+(x)*bitCount+2))*
      (*(pRealData+pit*(y)+(x)*bitCount+2)-*(pRealData+pit*(y-1)+(x)*bitCount+2))));
     if(temp>=t)
     {
      if(temp+100>255)
       tempB=255;
      else
       tempB=temp+100;
     }
     else
      tempB=*(pRealData+pit*(y)+(x)*bitCount+2);
    }

    *(pRealData2+pit2*y+x*bitCount2)=tempR;
    *(pRealData2+pit2*y+x*bitCount2+1)=tempG;
    *(pRealData2+pit2*y+x*bitCount2+2)=tempB;
   }
  }
  Invalidate();
 }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值