最小值滤波 (C 语言实现)

遇到最小值滤波的问题,小白不知道。一个程序写了三天,最终今天傍晚出来了。

。。

非常easy的for循环。可是没有理解最小值滤波。怎么写都是错啊~

这是我见过做好的描写叙述,关于最小值滤波:

3*3的像素点阵,对于中心点做最小值滤波的话,它的值将从77变换到0

处理结果图:

我一直支持也坚持开源分享的原则。为大家更好的相互学习,给出源码

  1. /******************************************************************
  2. code writer : EOF
  3. code date : 2014.08.07
  4. e-mail : jasonleaster@gmail.com jasonleaster@163.com
  5.  
  6. code purpose:
  7. This demo is coded for mininum value filter.
  8. If you find something wrong with my code, please touch me by e-mail.
  9. Thank you.
  10.  
  11. *******************************************************************/
  12.  
  13. #include "opencv2/highgui/highgui_c.h"
  14. #include "opencv2/imgproc/imgproc_c.h"
  15.  
  16. #include <stdio.h>
  17.  
  18. /*------------------------------------------------------------------------------
  19.  
  20. This two Macro are used for debugging, if you are begginer with OpenCV,
  21. it will help you to know and test what inside of the data struture in OpenCV
  22.  
  23. -------------------------------------------------------------------------------*/
  24.  
  25. //#define RGB_TEST_DEBUG
  26. //#define CHANNEL_TEST_DEBUG
  27.  
  28. /* the offset of three channel RGB */
  29. #define RED_BIT 2
  30. #define GREEN_BIT 1
  31. #define BLUE_BIT 0
  32.  
  33. #define SQUARE_LENGTH 15
  34.  
  35. int get_dark_imagine(IplImage* const img_origin,IplImage* const img_win_dark);
  36.  
  37. int main(int argc,char* argv[])
  38. {
  39. char* win_name_bf = "Before Processing";
  40. char* win_name_af = "After Processing";
  41.  
  42. CvSize size;
  43.  
  44. IplImage* img_origin = cvLoadImage(argv[1],CV_LOAD_IMAGE_COLOR);
  45.  
  46. size.height = img_origin->height;
  47. size.width = img_origin->width;
  48.  
  49. IplImage* img_win_dark = cvCreateImage(size,IPL_DEPTH_8U,1);//single channel
  50.  
  51. get_dark_imagine(img_origin,img_win_dark);
  52.  
  53. cvNamedWindow(win_name_bf,CV_WINDOW_AUTOSIZE);
  54. //cvNamedWindow is a function which would help you to creat a window.
  55.  
  56. cvShowImage(win_name_bf,img_origin);
  57. //Obviously, show the picture that you inputed.
  58.  
  59. cvNamedWindow(win_name_af,CV_WINDOW_AUTOSIZE);
  60. //cvNamedWindow is a function which would help you to creat a window.
  61.  
  62. cvShowImage(win_name_af,img_win_dark);
  63. //Obviously, show the picture that you inputed.
  64.  
  65. cvWaitKey(0);
  66. //pause and let the user see the picture.
  67.  
  68. cvReleaseImage(&img_origin);
  69. cvReleaseImage(&img_win_dark);
  70. //Finally, release the struture, otherwise, memory leak !
  71.  
  72. return 0;
  73. }
  74.  
  75. int get_dark_imagine(IplImage* const img_origin,IplImage* const img_win_dark)
  76. {
  77. /*
  78. Varible description:
  79.  
  80. @img_origin : A pointer which point to the original picture's IplImage-structure.
  81. @img_win_dark: A pointer which point to the dark-window's IplImage-structure.
  82. */
  83.  
  84. if(img_origin == NULL || img_win_dark == NULL)
  85. {
  86. printf("Error! img_origin or img_win_dark is NULL\n");
  87.  
  88. return 1;
  89. }
  90.  
  91. int height_origin = img_origin->height ;
  92. int width_origin = img_origin->width ;//the search band width.
  93.  
  94. unsigned char * const ptr_array_origin = (unsigned char*)img_origin->imageData;
  95. unsigned char * const ptr_array_win_dark = (unsigned char*)img_win_dark->imageData;
  96.  
  97. unsigned char* ptr_header_origin = NULL;
  98.  
  99. int row = 0;
  100. int col = 0;
  101. int square_row = 0;
  102. int square_col = 0;
  103.  
  104. int min = 0;
  105. int T_min = 0;
  106. int temp_R = 0;
  107. int temp_G = 0;
  108. int temp_B = 0;
  109. int temp = 0;
  110.  
  111. int search_win_start = SQUARE_LENGTH/2;
  112.  
  113. /*
  114. define two varible -- height_origin & width_origin for up band-width of the search-window
  115. */
  116.  
  117. int search_win_height_end = img_win_dark->height - SQUARE_LENGTH/2;
  118. int search_win_width_end = img_win_dark->width - SQUARE_LENGTH/2;
  119.  
  120. //initializition of the picture's data that 'ptr_array_win_dark' point to.
  121. for(row = 0; row < height_origin; row++)
  122. {
  123. for(col = 0; col < width_origin ;col++)
  124. {
  125. *(ptr_array_win_dark + col + row*(img_win_dark->widthStep)) = 255;
  126.  
  127. }
  128. }
  129.  
  130. //Mininum value filter
  131. for(row = search_win_start; row < search_win_height_end; row++)
  132. {
  133.  
  134. for(col = search_win_start; col < search_win_width_end ;col++)
  135. {
  136.  
  137. ptr_header_origin = ptr_array_origin + (row)*(img_origin->widthStep) + (col)*3;
  138.  
  139. temp_B = *(ptr_header_origin + BLUE_BIT );
  140. temp_G = *(ptr_header_origin + GREEN_BIT );
  141. temp_R = *(ptr_header_origin + RED_BIT );
  142.  
  143. min = (temp_G < temp_B) ?
  144.  
  145. temp_G : temp_B;
  146. min = (min < temp_R) ? min : temp_R;
  147.  
  148. T_min = min;
  149.  
  150. for(square_row = (row - search_win_start); square_row < (row + search_win_start + 1);square_row++)
  151. {
  152. for(square_col = (col - search_win_start); square_col < (col+search_win_start + 1);square_col++)
  153. {
  154. min = *(ptr_array_win_dark + square_col + square_row*(img_win_dark->widthStep));
  155.  
  156. if (min > T_min)
  157. {
  158. *(ptr_array_win_dark + square_col + square_row*(img_win_dark->widthStep)) = (T_min);
  159. }
  160. }
  161. }
  162. }
  163. }
  164.  
  165. return 0;
  166. }

如有错误。欢迎交流指正

—— EOF

update : 2014.10.05

写了一个matlab版本号的最小滤波算法框架

  1. Img_filted = dark_channel;
  2. for row = 1 : height
  3. for col = 1 : width
  4.  
  5. min_value = dark_channel(row,col);
  6. for patch_row = (row -floor(search_win_height/2)) : (row + floor(search_win_height/2))
  7. for patch_col = (col - floor(search_win_width/2)) : (col + floor(search_win_width/2))
  8.  
  9. if patch_row > 0 && patch_col > 0 && patch_row <= height && patch_col <= width
  10. if min_value < Img_filted(patch_row,patch_col)
  11. Img_filted(patch_row,patch_col) = min_value;
  12. end
  13. end
  14. end
  15. end
  16. end
  17. end

最小值滤波 (C 语言实现)的更多相关文章

  1. 中值滤波C语言优化

    中值滤波C语言优化 图像平滑是图像预处理的基本操作,本文首先用不同的方法对一张图片做预处理比较它们效果的不同,然后针对中值滤波,实现了一种快速实现.(其实是copy的opencv实现,呵呵).因为op ...

  2. OpenCV之邻域运算之最值滤波

    写了一段小程序,分享给大家! //==================================================================== // 作者 : quarry ...

  3. 基于R语言的数据分析和挖掘方法总结——描述性统计

    1.1 方法简介 描述性统计包含多种基本描述统计量,让用户对于数据结构可以有一个初步的认识.在此所提供之统计量包含: 基本信息:样本数.总和 集中趋势:均值.中位数.众数 离散趋势:方差(标准差).变 ...

  4. Opencv中常见的滤波方法

    滤波(模糊)的概念和作用: 图像滤波增强处理实质上就是运用滤波技术来增强图像的某些空间频率特征,以改善地物目标与领域或背景之间的灰度反差. 遥感系统成像过程中可能产生的”模糊”作用,常使遥感图像上某些 ...

  5. 《Single Image Haze Removal Using Dark Channel Prior》一文中图像去雾算法的原理、实现、效果(速度可实时)

    最新的效果见 :http://video.sina.com.cn/v/b/124538950-1254492273.html 可处理视频的示例:视频去雾效果 在图像去雾这个领域,几乎没有人不知道< ...

  6. paper 105: 《Single Image Haze Removal Using Dark Channel Prior》一文中图像去雾算法的原理、实现、效果及其他

    在图像去雾这个领域,几乎没有人不知道<Single Image Haze Removal Using Dark Channel Prior>这篇文章,该文是2009年CVPR最佳论文.作者 ...

  7. Single Image Haze Removal Using Dark Channel Prior

    <Single Image Haze Removal Using Dark Channel Prior>一文中图像去雾算法的原理.实现.效果及其他. Posted on 2013-08-2 ...

  8. MinFilter(MaxFilter)快速算法C++实现

    目录 1.算法简述 1.1.MinFilter(MaxFilter) 算法简述 1.2.MinFilter(MaxFilter) 快速算法简述 2.实现代码 2.1.MinFilterOneRow 单 ...

  9. 任意半径局部直方图类算法在PC中快速实现的框架。

    在图像处理中,局部算法一般来说,在很大程度上会获得比全局算法更为好的效果,因为他考虑到了图像领域像素的信息,而很多局部算法可以借助于直方图获得加速.同时,一些常规的算法,比如中值滤波.最大值滤波.最小 ...

随机推荐

  1. ThinkPHP第十七天(隐藏index.php和简短路径配置)

    1.路由设置,让路径中不显示index.php方法: 第一步:在apache中的httpd.conf中查找: LoadModule rewrite_module modules/mod_rewrite ...

  2. codeforces 451E. Devu and Flowers 容斥原理+lucas

    题目链接 给n个盒子, 每个盒子里面有f[i]个小球, 然后一共可以取sum个小球.问有多少种取法, 同一个盒子里的小球相同, 不同盒子的不同. 首先我们知道, n个盒子放sum个小球的方式一共有C( ...

  3. LintCode-两个字符串是变位词

    题目描述: 写出一个函数 anagram(s, t) 去判断两个字符串是否是颠倒字母顺序构成的 样例 给出 s="abcd",t="dcab",返回 true ...

  4. 64位linux下安装oracle10 64位 遇到 :ins_ctx.mk ;ins_emdb.mk

    http://blog.csdn.net/bamuta/article/details/10523835 http://www.cnblogs.com/kerrycode/p/3519446.html ...

  5. 基于Visual C++2013拆解世界五百强面试题--题12-进制转换

    编程实现,把十进制数(long型)分别以二进制和十六进制形式输出,不能使用printf系列库函数. 转换成二进制,直接循环移位依次取每一位,判断1或0然后将相应字符放入字符串缓冲区中. 对于十六进制, ...

  6. QT在ui文件上建立信号操机制会不会对后期维护产生影响 - love4Mario的专栏 - 博客频道 - CSDN.NETQT在ui文件上建立信号操机制会不会对后期维护产生影响 - love4Mario的专栏 - 博客频道 - CSDN.NET

    QT在ui文件上建立信号操机制会不会对后期维护产生影响 - love4Mario的专栏 - 博客频道 - CSDN.NET QT在ui文件上建立信号操机制会不会对后期维护产生影响 分类: 学习心得 2 ...

  7. Bug驱动开发(Bug-driven development)

    说实话,作为一个Domino开发者,像測试驱动开发(Test-driven development).功能驱动开发(Feature-driven development)之类软件开发的高大上的方法论( ...

  8. CRC32 vs Java.HashCode

    找了容量为27万中文词库进行试验    CRC32 中冲突率 < 0.01%    而 Java.HashCode 有 4%    hashCode 的速度 应该比 CRC 快 2-3 倍 CR ...

  9. CSS引入的方式有哪些? link和@import的区别是?转载

    CSS引入的方式有哪些? link和@import的区别是? HTML 中引入 CSS 的方式 有 4 种方式可以在 HTML 中引入 CSS.其中有 2 种方式是在 HTML 文件中直接添加 CSS ...

  10. JSP内置对象---application

    application 对象   服务器启动后,就产生了application 对象.当一个客户访问服务器上的一个JSP 页面时,JSP 引擎为该客户分配这个application 对象,  当客户在 ...