OpenCV实现USM锐化

【转】http://www.programdevelop.com/4964391/

USM (Unsharp masking) is a common operation of image processing. From the Internet search a bit, there are basically three different ways. Only 2 lines of code, there are hundreds of the most complex line. These three methods below summary records for later use.

最简单的方法:

  1. cv::GaussianBlur(frame, image, cv::Size(, ), );
  2. cv::addWeighted(frame, 1.5, image, -0.5, , image);

Followed by the simple method, derived from "only want to hear a good story" programdevelop.com blog.

常用photoshop的一般都会用到usm (unsharp mask)锐化,它的原理非常简单,使用opencv进行实现只需要4行代码

最终实现效果如下:

  1. double sigma = ;
  2. int threshold = ;
  3. float amount = ;
  4. imgsrc = imread("thankyou.jpg");
  5. GaussianBlur(imgsrc, imgblurred, cv::size(0,0), sigma, sigma) #对于图形size(0,0)效果最好。why?看高斯滤波原理
    #GaussianBlur(imgsrc, imgblurred, cv::size(5,5), sigma, sigma)
    #GaussianBlur(imgsrc, imgblurred, size(), sigma, sigma)
  6. lowcontrastmask = abs(imgsrc-imgblurred)<threshold;
  7. imgdst = imgsrc*(+amount)+imgblurred*(-amount);
  8. imgsrc.copyTo(imgdst, lowcontrastmask);
  9. imshow("SUM", imgdst);
===================================================
  1. GaussianBlur(imgsrc, imgblurred, cv::size(5,5), sigma, sigma)的USM效果

  1. GaussianBlur(imgsrc, imgblurred, cv::size(0,0), sigma, sigma)的USM效果

==================================================

     

 原图像                                      锐化结果

使用photoshop进行处理的效果如下:
参数:数量131%   半径2.2像素   阈值0色阶

基本上效果还是类似的,通过调节参数可以达到基本一致的效果~~~哈哈
一个简单的usm算法~~~研究了好多天~~~~
不过看到满意的结果还是挺有成就感的
==========原文来自http://www.makaidong.com/%E5%8D%9A%E5%AE%A2%E5%9B%AD%E7%89%9B/4663.shtml====

最复杂的方法:

  1. void UnsharpMask(const IplImage* src, IplImage* dst, float amount=, float radius=, uchar threshold=, intcontrast=)
  2. {
  3. if(!src)return ;
  4.  
  5. int imagewidth = src->width;
  6. int imageheight = src->height;
  7. int channel = src->nChannels;
  8.  
  9. IplImage* blurimage = cvCreateImage(cvSize(imagewidth,imageheight), src->depth, channel);
  10. IplImage* DiffImage = cvCreateImage(cvSize(imagewidth,imageheight), , channel);
  11.  
  12. //
  13. IplImage* highcontrast = cvCreateImage(cvSize(imagewidth,imageheight), , channel);
  14. AdjustContrast(src, highcontrast, contrast);
  15.  
  16. //
  17. cvSmooth(src, blurimage, CV_GAUSSIAN, radius);
  18.  
  19. //
  20. for (int y=; y<imageheight; y++)
  21. {
  22. for (int x=; x<imagewidth; x++)
  23. {
  24. CvScalar ori = cvGet2D(src, y, x);
  25. CvScalar blur = cvGet2D(blurimage, y, x);
  26. CvScalar val;
  27. val.val[] = abs(ori.val[] - blur.val[]);
  28. val.val[] = abs(ori.val[] - blur.val[]);
  29. val.val[] = abs(ori.val[] - blur.val[]);
  30.  
  31. cvSet2D(DiffImage, y, x, val);
  32. }
  33. }
  34.  
  35. //
  36. for (int y=; y<imageheight; y++)
  37. {
  38. for (int x=; x<imagewidth; x++)
  39. {
  40. CvScalar hc = cvGet2D(highcontrast, y, x);
  41. CvScalar diff = cvGet2D(DiffImage, y, x);
  42. CvScalar ori = cvGet2D(src, y, x);
  43. CvScalar val;
  44.  
  45. for (int k=; k<channel; k++)
  46. {
  47. if (diff.val[k] > threshold)
  48. {
  49. // = *(1-r) + *r
  50. val.val[k] = ori.val[k] *(-amount) + hc.val[k] *amount;
  51. val.val[k] /= ;
  52. }
  53. else
  54. {
  55. val.val[k] = ori.val[k];
  56. }
  57. }
  58. cvSet2D(dst, y, x, val);
  59. }
  60. }
  61. cvReleaseImage(&blurimage);
  62. cvReleaseImage(&DiffImage);
  63. }
  64. //?contrast[-255,255]
  65. void AdjustContrast(const IplImage* src, IplImage* dst, int contrast)
  66. {
  67. if (!src)return ;
  68.  
  69. int imagewidth = src->width;
  70. int imageheight = src->height;
  71. int channel = src->nChannels;
  72.  
  73. //
  74. CvScalar mean = {,,,};
  75. for (int y=; y<imageheight; y++)
  76. {
  77. for (int x=; x<imagewidth; x++)
  78. {
  79. for (int k=; k<channel; k++)
  80. {
  81. CvScalar ori = cvGet2D(src, y, x);
  82. for (int k=; k<channel; k++)
  83. {
  84. mean.val[k] += ori.val[k];
  85. }
  86. }
  87. }
  88. }
  89. for (int k=; k<channel; k++)
  90. {
  91. mean.val[k] /= imagewidth * imageheight;
  92. }
  93.  
  94. //
  95. if (contrast <= -)
  96. {
  97. //-255???RGB??1??
  98. for (int y=; y<imageheight; y++)
  99. {
  100. for (int x=; x<imagewidth; x++)
  101. {
  102. cvSet2D(dst, y, x, mean);
  103. }
  104. }
  105. }
  106. else if(contrast > - && contrast <= )
  107. {
  108. //(1)nRGB = RGB + (RGB - Threshold) * Contrast / 255
  109. // -2550?
  110. //?nRGBR?G?B?RGBR?G?B?Threshold?Contrast?
  111. for (int y=; y<imageheight; y++)
  112. {
  113. for (int x=; x<imagewidth; x++)
  114. {
  115. CvScalar nRGB;
  116. CvScalar ori = cvGet2D(src, y, x);
  117. for (int k=; k<channel; k++)
  118. {
  119. nRGB.val[k] = ori.val[k] + (ori.val[k] - mean.val[k]) *contrast /;
  120. }
  121. cvSet2D(dst, y, x, nRGB);
  122. }
  123. }
  124. }
  125. else if(contrast > && contrast <)
  126. {
  127. //0255?(2)?(1)?
  128. //(2)?nContrast = 255 * 255 / (255 - Contrast) - 255
  129. //nContrast?Contrast?
  130.  
  131. CvScalar nRGB;
  132. int nContrast = * /( - contrast) - ;
  133.  
  134. for (int y=; y<imageheight; y++)
  135. {
  136. for (int x=; x<imagewidth; x++)
  137. {
  138. CvScalar ori = cvGet2D(src, y, x);
  139. for (int k=; k<channel; k++)
  140. {
  141. nRGB.val[k] = ori.val[k] + (ori.val[k] - mean.val[k]) *nContrast /;
  142. }
  143. cvSet2D(dst, y, x, nRGB);
  144. }
  145. }
  146. }
  147. else
  148. {
  149. // 255????8?
  150. //??????
  151. for (int y=; y<imageheight; y++)
  152. {
  153. for (int x=; x<imagewidth; x++)
  154. {
  155. CvScalar rgb;
  156. CvScalar ori = cvGet2D(src, y, x);
  157. for (int k=; k<channel; k++)
  158. {
  159. if (ori.val[k] > mean.val[k])
  160. {
  161. rgb.val[k] = ;
  162. }
  163. else
  164. {
  165. rgb.val[k] = ;
  166. }
  167. }
  168. cvSet2D(dst, y, x, rgb);
  169. }
  170. }
  171. }
  172. }

OpenCV实现USM锐化与测试的更多相关文章

  1. opencv:USM锐化

    USM:unsharp mask 对小的细节干扰小,对大的细节进行锐化 Mat dst; Mat blur_image; GaussianBlur(src, blur_image, Size(3, 3 ...

  2. USM锐化之openCV实现,附赠调整对比度函数

    源地址:http://www.cnblogs.com/easymind223/archive/2012/07/03/2575277.html 常用Photoshop的玩家都知道Unsharp Mask ...

  3. SSE图像算法优化系列十六:经典USM锐化中的分支判断语句SSE实现的几种方法尝试。

    分支判断的语句一般来说是不太适合进行SSE优化的,因为他会破坏代码的并行性,但是也不是所有的都是这样的,在合适的场景中运用SSE还是能对分支预测进行一定的优化的,我们这里以某一个算法的部分代码为例进行 ...

  4. C#调用GDI+1.1中的函数实现高斯模糊、USM锐化等经典效果。

    http://www.cnblogs.com/Imageshop/archive/2012/12/13/2815712.html 在GDI+1.1的版本中,MS加入不少新的特性,其中的特效类Effec ...

  5. Win10中用yolov3训练自己的数据集全过程(VS、CUDA、CUDNN、OpenCV配置,训练和测试)

    在Windows系统的Linux系统中用yolo训练自己的数据集的配置差异很大,今天总结在win10中配置yolo并进行训练和测试的全过程. 提纲: 1.下载适用于Windows的darknet 2. ...

  6. opencv python3.6安装和测试

    安装: 命令行  pip install D:\python3.6.1\Scriptsopencv_python-3.2.0-cp36-cp36m-win_amd64.whl 测试代码: import ...

  7. 【QT】【OpenCv】初始配置以及测试功能

    #include "mainwindow.h" #include "ui_mainwindow.h" #include<opencv2/core/core ...

  8. Python: PS 滤镜--USM 锐化

    本文用 Python 实现 PS 滤镜中的 USM 锐化效果,具体的算法原理和效果可以参考之前的博客: http://blog.csdn.net/matrix_space/article/detail ...

  9. OpenCV开发环境搭建-并测试一个图像灰度处理程序

    转载地址:http://blog.csdn.net/sjz_iron/article/details/8614070

随机推荐

  1. Handler类和Handler,Loop,MessageQueue的工作原理

    原文地址:http://blog.csdn.net/xiyangyang8/article/details/50754771 Handler类的作用主要有两种: 1.在新启动的线程中发送消息. 2.在 ...

  2. ExtJs 日期相加,Grid表格列可编辑

    1.日期相加: Ext.Date.add(new Date(), Ext.Date.DAY, 15) 2.Grid表格列可编辑: {    header : "实际已交货量",   ...

  3. Ambarella SDK build 步骤解析

    Make Target Options make命令如下: make <Tab> <Tab> /*列出所有支持的目标(命令行输入make, 再按两下Tab键)*/ make & ...

  4. Getting Installation aborted (Status 7) ApplyParsePerms: lsetfilecon of /syst...【转】

    OTA升级失败:原文http://en.miui.com/thread-112197-1-1.html Do you get this "Status 7" error in Re ...

  5. UESTC--1251--谕神的密码(贪心)

     谕神的密码 Time Limit: 1000MS   Memory Limit: 65535KB   64bit IO Format: %lld & %llu Submit Status ...

  6. Cosine Similarity of Two Vectors

    #include <iostream>#include <vector>#include <cmath>#include <numeric> templ ...

  7. Candies(差分约束系统)

    http://poj.org/problem?id=3159 思路:用O(V+ElogV)的Dijkstra算法求1到n的最短路.即用优先队列优化Dijkstra算法. #include <st ...

  8. 【区间DP】释放囚犯

    貌似和石子合并差不多 可能是我见的题太少了,所以都差不多 OK 算法分析 首先不难看出这是一道区间DP,那么,按照本蒟蒻的意思 区间DP==三个循环 for(int len=2;len<=n;l ...

  9. HDU 1054 Hungary

    Strategic Game Problem Description Bob enjoys playing computer games, especially strategic games, bu ...

  10. Java基础学习(二)——对象

    类:是抽象的概念集合,表示的是一个共性的产物,类之中定义的是属性和行为(方法): 对象:对象是一种个性的表示,表示一个独立的个体,每个对象拥有自己独立的属性,依靠属性来区分不同对象. 对象=实例 对象 ...