打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
图像锐化
  1. /************************************************** 
  2.  * 功能: 设定指定位置的像素灰度 
  3.  * 参数: imageBuf为目标图像 x,y为要设定像素的坐标 
  4.  **************************************************/  
  5.  void SetPixelXY(uchar** imageBuf1, int x, int y,int nc, int a)  
  6.  {  
  7.      if (nc ==1 )  
  8.      {  
  9.          imageBuf1[y][x *nc] = a;  
  10.      }  
  11.      else if (nc ==3)  
  12.      {  
  13.          imageBuf1[y][x *nc] = a;  
  14.          imageBuf1[y][x *nc + 1] = a;  
  15.          imageBuf1[y][x *nc + 2] = a;  
  16.      }  
  17.      else if (nc == 4)  
  18.      {  
  19.          imageBuf1[y][x * nc] = a;  
  20.          imageBuf1[y][x * nc + 1] = a;  
  21.          imageBuf1[y][x * nc + 2] = a;  
  22.          imageBuf1[y][x * nc + 3] = 255;  
  23.      }  
  24.  }  
  25.   
  26.  int GetAsh(uchar** imageBuf, int x, int y, int nc)  
  27.  {  
  28.      int clr = 0;  
  29.      if (nc == 1)  
  30.      {  
  31.          clr = imageBuf[y][x * nc] ;  
  32.      }  
  33.      else  
  34.      {  
  35.          clr = (imageBuf[y][x * nc] + imageBuf[y][x * nc + 1] + imageBuf[y][x * nc + 2]) / 3;  
  36.      }  
  37.   
  38.      return clr;  
  39.  }  
  40.   
  41.  /******************************************************** 
  42.  * 把线形存储的像素转化为二维数组形式 
  43.  * 参数: image 线形存储的像素, width,height 图象的长宽 
  44.  ********************************************************/  
  45.  uchar** CreatImage(uchar * image, unsigned int width, unsigned int height, int nc)  
  46.  {  
  47.      int imgWidthStep = ((width*nc + 3) / 4) * 4;  
  48.   
  49.      uchar** imageBuf = (uchar**)malloc(sizeof(uchar*)*(height));  
  50.      for (int y = 0; y < height; y++)  
  51.      {  
  52.          //使imageBuf中每个指针分别指向其下标表示的行的行首地址  
  53.          imageBuf[y] = image + y*imgWidthStep;  
  54.      }  
  55.      return imageBuf;  
  56.  }  
  57.   
  58.  /************************************************** 
  59.  * 功能: 使用模板对彩色图邻域进行运算 
  60.  * 参数: imageBuf为目标图像 w、h为图像大小 
  61.  *       templt为模板 tw为邻域大小 
  62.  *      x,y为要取得像素的坐标 
  63.  *       cn为颜色分量编号 0为蓝色 1为绿色 2为红色 
  64.  **************************************************/  
  65.  int TempltExcuteCl(uchar** imageBuf0, int w, int h, int nc,int* templt, int tw, int x, int y, int cn)  
  66.  {  
  67.     int i, j;                        //循环变量  
  68.     int m = 0;                      //用来存放加权和  
  69.     int px, py;  
  70.     //依次对邻域中每个像素进行运算  
  71.     for (i = 0; i < tw; i++)  
  72.     {  
  73.         for (j = 0; j < tw; j++)  
  74.         {  
  75.             //计算对应模板上位置的像素在原图像中的位置  
  76.             py = y - tw / 2 + i;  
  77.             px = x - tw / 2 + j;  
  78.             //加权求和  
  79.             m += imageBuf0[py][px * nc + cn] * templt[i*tw + j];  
  80.         }  
  81.     }  
  82.     return m;                     //返回结果  
  83.  }  
  84.   
  85.  /****************************************************************** 
  86.  * 功能: 彩色图像的拉普拉斯锐化处理(scale = 3) 
  87.  * 参数: image0为原图形,image1锐化结果, 
  88.  *      w、h为图象的宽和高 
  89.  ******************************************************************/  
  90.  void SharpLaplacianCl(uchar* image0, uchar* image1, unsigned int w, unsigned int h, int nc, float factor = 1.0)  
  91.  {  
  92.     //将图像转化为矩阵形式  
  93.     uchar** imageBuf0 = CreatImage(image0, w, h, nc);  
  94.     uchar** imageBuf1 = CreatImage(image1, w, h, nc);  
  95.     //设定模板  
  96.     //int templt[9] = { -1, -1, -1, -1, 8, -1, -1, -1, -1 };  
  97.   
  98.     int templt[25] = { 0, 0, -1, 0, 0,  
  99.         0, -1, -2, -1, 0,  
  100.         -1, -2, 16,-2, -1,  
  101.         0, -1, -2, -1, 0,  
  102.         0, 0, -1, 0, 0};  
  103.   
  104.     int x, y, c;  
  105.     int a;  
  106.     //设定衰减因子  
  107.    
  108.     //依次对原图像的每个像素进行处理  
  109.     for (y = 2; y < h - 2; y++)  
  110.     {  
  111.         for (x = 2; x < w/2 - 2; x++)  
  112.         {  
  113.             for (c = 0; c < nc; c++)  
  114.             {  
  115.                 //利用拉普拉斯模板对邻域进行处理  
  116.                 a = TempltExcuteCl(imageBuf0, w, h,nc, templt, 5, x, y, c);  
  117.                 a = (int)((float)a / factor);  
  118.                 //对中心像素进行增强  
  119.                 a = imageBuf0[y][x * nc + c] + a;  
  120.                 //过限处理  
  121.                 a = a > 255 ? 255 : a;  
  122.                 a = a < 0 ? 0 : a;  
  123.                 imageBuf1[y][x * nc + c] = a;  
  124.             }  
  125.         }  
  126.     }  
  127.     //清理内存  
  128.     free(imageBuf0);  
  129.     free(imageBuf1);  
  130.  }  

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
LBP特征原理
转:LBP特征分析
opencv:特征检测与提取
Sobel边缘检测和边缘细化
图像增强-3
提取图像的骨架(Skeleton)算法
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服