10.1国庆后,知名博主:laviewpbt  http://www.cnblogs.com/Imageshop/

发起了一个优化3x3中值模糊的小活动。

俺也参加其中,今天博主laviewpbt  共享了一份不错的CLAHE代码。

free精神,真心为其点赞。

故俺也分享这份最快的3x3中值模糊的代码。

///	编写者: laviewpbt,  编写时间: 2015.10.16, 联系QQ: 33184777

/// <summary>
/// 快速的实现3*3大小的中值模糊,边缘1像素未做处理。(2015.10.12日)
/// </summary>
/// <param name="Src">原始数据。</param>
/// <param name="Dest">目标数据。</param>
/// 百度: Fast median search_ an ANSI C implementation.html
/// 优化版本: 落羽の殇 联系QQ:200759103 static unsigned int* pixGreater = NULL;
static unsigned int* pixLess = NULL;
static unsigned int cmpTable[256 * 256 * 2] = { 0 };
class autoCmpTable
{
public:
autoCmpTable() {
unsigned int x, y, p;
unsigned int tableLength = 256 * 256;
pixGreater = cmpTable;
pixLess = cmpTable + tableLength;
for (x = 0; x < 256; x++) {
for (y = 0; y < 256; y++) {
p = x + y * 256;
if (x > y)
{
pixLess[p] = x;
pixGreater[p] = y;
}
else
{
pixGreater[p] = x;
pixLess[p] = y;
}
}
}
}
};
static autoCmpTable initCmpTable; IS_RET __stdcall Fastest33MedianBlur(TMatrix *Src, TMatrix *Dest)
{
if (Src == NULL || Dest == NULL) return IS_RET_ERR_NULLREFERENCE;
if (Src->Data == NULL || Dest->Data == NULL) return IS_RET_ERR_NULLREFERENCE;
if (Src->Width != Dest->Width || Src->Height != Dest->Height || Src->Channel != Dest->Channel || Src->Depth != Dest->Depth || Src->WidthStep != Dest->WidthStep) return IS_RET_ERR_PARAMISMATCH;
if (Src->Depth != IS_DEPTH_8U || Dest->Depth != IS_DEPTH_8U) return IS_RET_ERR_NOTSUPPORTED;
IS_RET Ret = IS_RET_OK; if (!pixGreater&&!pixLess)
{
return IS_RET_ERR_NOTSUPPORTED;
}
if (Src->Data == Dest->Data)
{
TMatrix *Clone = NULL;
Ret = IS_CloneMatrix(Src, &Clone);
if (Ret != IS_RET_OK) return Ret;
Ret = Fastest33MedianBlur(Clone, Dest);
IS_FreeMatrix(&Clone);
return Ret;
}
unsigned int X, Y, Width = Src->Width, Height = Src->Height;
unsigned char *LineP0, *LineP1, *LineP2, *LinePD;
unsigned int srcWidthStep = Src->WidthStep;
unsigned int dstWidthStep = Dest->WidthStep;
unsigned int srcChannel = Src->Channel;
unsigned int dstChannel = Dest->Channel;
unsigned char* SrcData = Src->Data;
unsigned char* DstData = Dest->Data; if (srcChannel == 1)
{
unsigned int Gray0, Gray1, Gray2, Gray3, Gray4, Gray5, Gray6, Gray7, Gray8, pos;
for (Y = 1; Y < Height - 1; Y++)
{
LineP0 = SrcData + (Y - 1) * srcWidthStep + 1;
LineP1 = LineP0 + srcWidthStep;
LineP2 = LineP1 + srcWidthStep;
LinePD = DstData + Y *dstWidthStep + 1;
for (X = 1; X < Width - 1; X++)
{
Gray0 = LineP0[X - 1];
Gray1 = LineP0[X];
Gray2 = LineP0[X + 1];
Gray3 = LineP1[X - 1];
Gray4 = LineP1[X];
Gray5 = LineP2[X + 1];
Gray6 = LineP2[X - 1];
Gray7 = LineP2[X];
Gray8 = LineP2[X + 1];
/* if (Gray1 > Gray2) Swap(Gray1, Gray2);*/
pos = Gray1 + Gray2 * 256;
Gray2 = pixLess[pos];
Gray1 = pixGreater[pos];
/* if (Gray4 > Gray5) Swap(Gray4, Gray5);*/
pos = Gray4 + Gray5 * 256;
Gray5 = pixLess[pos];
Gray4 = pixGreater[pos];
/* if (Gray7 > Gray8) Swap(Gray7, Gray8);*/
pos = Gray7 + Gray8 * 256;
Gray8 = pixLess[pos];
Gray7 = pixGreater[pos];
/* if (Gray0 > Gray1) Swap(Gray0, Gray1);*/
pos = Gray0 + Gray1 * 256;
Gray1 = pixLess[pos];
Gray0 = pixGreater[pos];
/* if (Gray3 > Gray4) Swap(Gray3, Gray4);*/
pos = Gray3 + Gray4 * 256;
Gray4 = pixLess[pos];
Gray3 = pixGreater[pos];
/* if (Gray6 > Gray7) Swap(Gray6, Gray7);*/
pos = Gray6 + Gray7 * 256;
Gray7 = pixLess[pos];
Gray6 = pixGreater[pos];
/* if (Gray1 > Gray2) Swap(Gray1, Gray2);*/
pos = Gray1 + Gray2 * 256;
Gray2 = pixLess[pos];
Gray1 = pixGreater[pos];
/* if (Gray4 > Gray5) Swap(Gray4, Gray5);*/
pos = Gray4 + Gray5 * 256;
Gray5 = pixLess[pos];
Gray4 = pixGreater[pos];
/* if (Gray7 > Gray8) Swap(Gray7, Gray8);*/
pos = Gray7 + Gray8 * 256;
Gray8 = pixLess[pos];
Gray7 = pixGreater[pos];
/* if (Gray0 > Gray3) Swap(Gray0, Gray3);*/
pos = Gray0 + Gray3 * 256;
Gray3 = pixLess[pos];
Gray0 = pixGreater[pos];
/* if (Gray5 > Gray8) Swap(Gray5, Gray8);*/
pos = Gray5 + Gray8 * 256;
Gray8 = pixLess[pos];
Gray5 = pixGreater[pos];
/* if (Gray4 > Gray7) Swap(Gray4, Gray7);*/
pos = Gray4 + Gray7 * 256;
Gray7 = pixLess[pos];
Gray4 = pixGreater[pos];
/* if (Gray3 > Gray6) Swap(Gray3, Gray6);*/
pos = Gray3 + Gray6 * 256;
Gray6 = pixLess[pos];
Gray3 = pixGreater[pos];
/* if (Gray1 > Gray4) Swap(Gray1, Gray4);*/
pos = Gray1 + Gray4 * 256;
Gray4 = pixLess[pos];
Gray1 = pixGreater[pos];
/* if (Gray2 > Gray5) Swap(Gray2, Gray5);*/
pos = Gray2 + Gray5 * 256;
Gray5 = pixLess[pos];
Gray2 = pixGreater[pos];
/* if (Gray4 > Gray7) Swap(Gray4, Gray7);*/
pos = Gray4 + Gray7 * 256;
Gray7 = pixLess[pos];
Gray4 = pixGreater[pos];
/* if (Gray4 > Gray2) Swap(Gray4, Gray2);*/
pos = Gray4 + Gray2 * 256;
Gray2 = pixLess[pos];
Gray4 = pixGreater[pos];
/* if (Gray6 > Gray4) Swap(Gray6, Gray4);*/
pos = Gray6 + Gray4 * 256;
Gray4 = pixLess[pos];
Gray6 = pixGreater[pos];
/* if (Gray4 > Gray2) Swap(Gray4, Gray2); */
pos = Gray4 + Gray2 * 256;
Gray2 = pixLess[pos];
Gray4 = pixGreater[pos]; LinePD[1] = Gray4;
LinePD++;
}
}
return Ret;
}
else
{
TMatrix *Blue = NULL, *Green = NULL, *Red = NULL, *Alpha = NULL; // 由于C变量如果不初始化,其值是随机值,可能会导致释放时的错误。
IS_RET Ret = SplitRGBA(Src, &Blue, &Green, &Red, &Alpha);
if (Ret != IS_RET_OK) goto Done24;
#pragma omp parallel num_threads(3)
{
#pragma omp sections
{
#pragma omp section
{
Ret = Fastest33MedianBlur(Blue, Blue);
}
#pragma omp section
{
Ret = Fastest33MedianBlur(Green, Green);
}
#pragma omp section
{
Ret = Fastest33MedianBlur(Red, Red);
} }
}
Ret = CombineRGBA(Dest, Blue, Green, Red, Alpha);
Done24:
IS_FreeMatrix(&Blue);
IS_FreeMatrix(&Green);
IS_FreeMatrix(&Red);
IS_FreeMatrix(&Alpha);
return Ret;
} return Ret;
}

  关于交换法短值快速排序的参考资料见:https://github.com/afniHQ/AFNI/blob/ab8ca253b784ae71401927df88da2bc6e16d07c1/src/cs_qsort_small.h

最快的3x3中值模糊的更多相关文章

  1. 【算法随记三】小半径中值模糊的急速实现(16MB图7.5ms实现) + Photoshop中蒙尘和划痕算法解读。

    在本人的博客里,分享了有关中值模糊的O(1)算法,详见:任意半径中值滤波(扩展至百分比滤波器)O(1)时间复杂度算法的原理.实现及效果 ,这里的算法的执行时间和参数是无关的.整体来说,虽然速度也很快, ...

  2. Emgu-WPF学习使用-中值模糊

    原文:Emgu-WPF学习使用-中值模糊 实现效果: 实现途径: 前提:Image File-> System.Drawing.Bitmap->Image<Bgr, byte> ...

  3. OpenCV笔记(1)(图片读取与现实、色彩空间、基础运算、均值方差、逻辑运算、泛洪填充、均值中值及自定义平滑)

    一.图片读取和显示 import cv2 as cv # 图片读取cv.imread(img_path) car_img = cv.imread("car1.png") # 图片显 ...

  4. opencv-11-中值滤波及自适应中值滤波

    开始之前 在上一篇我们实现了读取噪声图像, 然后 进行三种形式的均值滤波得到结果, 由于我们自己写的均值滤波未作边缘处理, 所以效果有一定的下降, 但是总体来说, 我们得到的结果能够说明我们的算法执行 ...

  5. OpenCV计算机视觉学习(4)——图像平滑处理(均值滤波,高斯滤波,中值滤波,双边滤波)

    如果需要处理的原图及代码,请移步小编的GitHub地址 传送门:请点击我 如果点击有误:https://github.com/LeBron-Jian/ComputerVisionPractice &q ...

  6. Atitit   图像处理 平滑 也称 模糊, 归一化块滤波、高斯滤波、中值滤波、双边滤波)

    Atitit   图像处理 平滑 也称 模糊, 归一化块滤波.高斯滤波.中值滤波.双边滤波) 是一项简单且使用频率很高的图像处理方法 用途 去噪 去雾 各种线性滤波器对图像进行平滑处理,相关OpenC ...

  7. opencv3 图片模糊操作-均值滤波 高斯滤波 中值滤波 双边滤波

    #include <iostream>#include <opencv2/opencv.hpp> using namespace std;using namespace cv; ...

  8. 学习 opencv---(8)非线性滤波:中值滤波,双边滤波

    正如我们上一篇文章中讲到的,线性滤波可以实现很多种不同的图像变换.然而非线性滤波,如中值滤波器和双边滤波器,有时可以达到更好的实现效果. 邻域算子的其他一些例子还有对 二值图像进行操作的形态学算子,用 ...

  9. 基于FPGA的中值滤波算法实现

    在这一篇开篇之前,我需要解决一个问题,上一篇我们实现了基于FPGA的均值滤波算法的实现,最后的显示效果图上发现有一些黑白色的斑点,我以为是椒盐噪声,然后在做基于FPGA的中值滤波算法的实验时,我发现黑 ...

随机推荐

  1. ZJOI2018游记

    我是一只普及组的菜鸡,我很菜 我参加 \(ZJOI\) 只是来试试水(水好深啊~),看看大佬(差距好大啊~),以后要好好学习 \(day0\) 下午2:00,颁奖 还以为要到很晚,还是挺快的 \(da ...

  2. C语言博客作业指针

    一.PTA实验作业 题目1:统计大于等于平均分人数 本题PTA提交列表 设计思路 定义形参s存放s的首地址,n存放人数m的值,aver存放平均分aver的地址 定义i控制循环,count用来统计高于等 ...

  3. python的Virtualenv

    Virtualenv 虚拟的 Python 环境(简称 venv) 是一个能帮助你在本地目录安装不同版本的 Python 模块的 Python 环境,你可以不再需要在你系统中安装所有东西就能开发并测试 ...

  4. memmove 和 memcpy的区别以及处理内存重叠问题

    区别: memcpy和memmove()都是C语言中的库函数,在头文件string.h中,作用是拷贝一定长度的内存的内容,原型分别如下: void *memcpy(void *dst, const v ...

  5. 《高级软件测试》Windows平台Jira的配置

    昨天完成了Jira的下载,很开心地去睡觉等明天天亮秒配环境愉快进行使用,撰写文档,开始徜徉于软件管理测试实践,早日走向代码巅峰. 我们把安装和配置的过程来走一遍. 安装完成汤姆猫长这样子: 安装Jir ...

  6. phpadmin增加使得项目能连接数据库

    感谢:http://jingyan.baidu.com/article/e4511cf332b9832b845eaf27.html 值得注意: 1.phpadmin的目录:D:\developsoft ...

  7. AngularJS1.X学习笔记12-Ajax

    说到Ajax,你一定是思绪万千,想到XMLHttpRequest,$.ajax(),跨域,异步之类的.本文将探讨一下AngularJS的Ajax. 一.一个简单的例子 <!DOCTYPE htm ...

  8. Mego开发文档 - 索引

    Mego 开发文档 Mego 快速概述 主要特性 获取Mego 使用流程 模型 查询 保存数据 入门 Mego 快速开始 创建项目 安装Nuget包 创建连接字符串 创建模型及数据上下文(添加引用) ...

  9. 文本分类学习(三) 特征权重(TF/IDF)和特征提取

    上一篇中,主要说的就是词袋模型.回顾一下,在进行文本分类之前,我们需要把待分类文本先用词袋模型进行文本表示.首先是将训练集中的所有单词经过去停用词之后组合成一个词袋,或者叫做字典,实际上一个维度很大的 ...

  10. HTTP协议的消息头:Content-Type和Accept的作用

    一.背景知识 1.概述 Http报头分为通用报头,请求报头,响应报头和实体报头. 请求方的http报头结构:通用报头|请求报头|实体报头 响应方的http报头结构:通用报头|响应报头|实体报头 Acc ...