拉普拉斯锐化处理
2016-04-22 09:08
260 查看
/************************************************** * 功能: 设定指定位置的像素灰度 * 参数: imageBuf为目标图像 x,y为要设定像素的坐标 **************************************************/ void SetPixelXY(uchar** imageBuf1, int x, int y, int nc, int a) { if (nc == 1) { imageBuf1[y][x *nc] = a; } else if (nc == 3) { imageBuf1[y][x *nc] = a; imageBuf1[y][x *nc + 1] = a; imageBuf1[y][x *nc + 2] = a; } else if (nc == 4) { imageBuf1[y][x * nc] = a; imageBuf1[y][x * nc + 1] = a; imageBuf1[y][x * nc + 2] = a; imageBuf1[y][x * nc + 3] = 255; } } int GetAsh(uchar** imageBuf, int x, int y, int nc) { int clr = 0; if (nc == 1) { clr = imageBuf[y][x * nc]; } else { clr = (imageBuf[y][x * nc] + imageBuf[y][x * nc + 1] + imageBuf[y][x * nc + 2]) / 3; } return clr; } /******************************************************** * 把线形存储的像素转化为二维数组形式 * 参数: image 线形存储的像素, width,height 图象的长宽 ********************************************************/ uchar** CreatImage(uchar * image, unsigned int width, unsigned int height, int nc) { int imgWidthStep = ((width*nc + 3) / 4) * 4; uchar** imageBuf = (uchar**)malloc(sizeof(uchar*)*(height)); for (int y = 0; y < height; y++) { //使imageBuf中每个指针分别指向其下标表示的行的行首地址 imageBuf[y] = image + y*imgWidthStep; } return imageBuf; } /************************************************** * 功能: 使用模板对彩色图邻域进行运算 * 参数: imageBuf为目标图像 w、h为图像大小 * templt为模板 tw为邻域大小 * x,y为要取得像素的坐标 * cn为颜色分量编号 0为蓝色 1为绿色 2为红色 **************************************************/ int TempltExcuteCl(uchar** imageBuf0, int w, int h, int nc, int* templt, int tw, int x, int y, int cn) { int i, j; //循环变量 int m = 0; //用来存放加权和 int px, py; //依次对邻域中每个像素进行运算 for (i = 0; i < tw; i++) { for (j = 0; j < tw; j++) { //计算对应模板上位置的像素在原图像中的位置 py = y - tw / 2 + i; px = x - tw / 2 + j; //加权求和 m += imageBuf0[py][px * nc + cn] * templt[i*tw + j]; } } return m; //返回结果 } /****************************************************************** * 功能: 彩色图像的拉普拉斯锐化处理(scale = 3) * 参数: image0为原图形,image1锐化结果, * w、h为图象的宽和高 ******************************************************************/ void SharpLaplacianCl(uchar* image0, uchar* image1, unsigned int w, unsigned int h, int nc, float factor = 1.0) { //将图像转化为矩阵形式 uchar** imageBuf0 = CreatImage(image0, w, h, nc); uchar** imageBuf1 = CreatImage(image1, w, h, nc); //设定模板 //int templt[9] = { -1, -1, -1, -1, 8, -1, -1, -1, -1 }; int templt[25] = { 0, 0, -1, 0, 0, 0, -1, -2, -1, 0, -1, -2, 16, -2, -1, 0, -1, -2, -1, 0, 0, 0, -1, 0, 0 }; int x, y, c; int a; //设定衰减因子 //依次对原图像的每个像素进行处理 for (y = 2; y < h - 2; y++) { for (x = 2; x < w/2 - 2; x++) { for (c = 0; c < nc; c++) { //利用拉普拉斯模板对邻域进行处理 a = TempltExcuteCl(imageBuf0, w, h, nc, templt, 5, x, y, c); a = (int)((float)a / factor); //对中心像素进行增强 a = imageBuf0[y][x * nc + c] + a; //过限处理 a = a > 255 ? 255 : a; a = a < 0 ? 0 : a; imageBuf1[y][x * nc + c] = a; } } } //清理内存 free(imageBuf0); free(imageBuf1); }
相关文章推荐
- iOS开发系列--UITableView全面解析
- 第十八篇【测试数据准备的那些事儿--后话】
- 动画animation的三个应用(漂浮的白云、旋转的星球、正方体合成)
- jquery智能弹出层,自己主动推断位置
- (操作系统)实验二 作业调度
- Swift 代码调试-善用XCode工具(UI调试,五种断点,预览UIImage...)
- 小代码 算法小题目
- 广义表
- 简单数据结构---数组实现栈
- 隐写术-隐藏明文的另类办法
- RCNN
- CC2640 multi_role 工程作为主机无法接收从机的notify
- PHP—字符串编码
- ant 打包项目class文件没有更新
- ASP.NET 中ImageMap控件的用法
- Java hashcode函数作用
- SVN 忽略获取和提交obj、bin文件夹
- Atitit.软件guibuttonand面板---os区-----linux windows搜索文件 目录
- linux串口
- 影院售票系统