OpenCV实现USM锐化与测试
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.
最简单的方法:
- cv::GaussianBlur(frame, image, cv::Size(, ), );
- 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行代码
最终实现效果如下:
- double sigma = ;
- int threshold = ;
- float amount = ;
- imgsrc = imread("thankyou.jpg");
- 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)- lowcontrastmask = abs(imgsrc-imgblurred)<threshold;
- imgdst = imgsrc*(+amount)+imgblurred*(-amount);
- imgsrc.copyTo(imgdst, lowcontrastmask);
- imshow("SUM", imgdst);

- GaussianBlur(imgsrc, imgblurred, cv::size(5,5), sigma, sigma)的USM效果
- 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====
最复杂的方法:
- void UnsharpMask(const IplImage* src, IplImage* dst, float amount=, float radius=, uchar threshold=, intcontrast=)
- {
- if(!src)return ;
- int imagewidth = src->width;
- int imageheight = src->height;
- int channel = src->nChannels;
- IplImage* blurimage = cvCreateImage(cvSize(imagewidth,imageheight), src->depth, channel);
- IplImage* DiffImage = cvCreateImage(cvSize(imagewidth,imageheight), , channel);
- //
- IplImage* highcontrast = cvCreateImage(cvSize(imagewidth,imageheight), , channel);
- AdjustContrast(src, highcontrast, contrast);
- //
- cvSmooth(src, blurimage, CV_GAUSSIAN, radius);
- //
- for (int y=; y<imageheight; y++)
- {
- for (int x=; x<imagewidth; x++)
- {
- CvScalar ori = cvGet2D(src, y, x);
- CvScalar blur = cvGet2D(blurimage, y, x);
- CvScalar val;
- val.val[] = abs(ori.val[] - blur.val[]);
- val.val[] = abs(ori.val[] - blur.val[]);
- val.val[] = abs(ori.val[] - blur.val[]);
- cvSet2D(DiffImage, y, x, val);
- }
- }
- //
- for (int y=; y<imageheight; y++)
- {
- for (int x=; x<imagewidth; x++)
- {
- CvScalar hc = cvGet2D(highcontrast, y, x);
- CvScalar diff = cvGet2D(DiffImage, y, x);
- CvScalar ori = cvGet2D(src, y, x);
- CvScalar val;
- for (int k=; k<channel; k++)
- {
- if (diff.val[k] > threshold)
- {
- // = *(1-r) + *r
- val.val[k] = ori.val[k] *(-amount) + hc.val[k] *amount;
- val.val[k] /= ;
- }
- else
- {
- val.val[k] = ori.val[k];
- }
- }
- cvSet2D(dst, y, x, val);
- }
- }
- cvReleaseImage(&blurimage);
- cvReleaseImage(&DiffImage);
- }
- //?contrast[-255,255]
- void AdjustContrast(const IplImage* src, IplImage* dst, int contrast)
- {
- if (!src)return ;
- int imagewidth = src->width;
- int imageheight = src->height;
- int channel = src->nChannels;
- //
- CvScalar mean = {,,,};
- for (int y=; y<imageheight; y++)
- {
- for (int x=; x<imagewidth; x++)
- {
- for (int k=; k<channel; k++)
- {
- CvScalar ori = cvGet2D(src, y, x);
- for (int k=; k<channel; k++)
- {
- mean.val[k] += ori.val[k];
- }
- }
- }
- }
- for (int k=; k<channel; k++)
- {
- mean.val[k] /= imagewidth * imageheight;
- }
- //
- if (contrast <= -)
- {
- //-255???RGB??1??
- for (int y=; y<imageheight; y++)
- {
- for (int x=; x<imagewidth; x++)
- {
- cvSet2D(dst, y, x, mean);
- }
- }
- }
- else if(contrast > - && contrast <= )
- {
- //(1)nRGB = RGB + (RGB - Threshold) * Contrast / 255
- // -2550?
- //?nRGBR?G?B?RGBR?G?B?Threshold?Contrast?
- for (int y=; y<imageheight; y++)
- {
- for (int x=; x<imagewidth; x++)
- {
- CvScalar nRGB;
- CvScalar ori = cvGet2D(src, y, x);
- for (int k=; k<channel; k++)
- {
- nRGB.val[k] = ori.val[k] + (ori.val[k] - mean.val[k]) *contrast /;
- }
- cvSet2D(dst, y, x, nRGB);
- }
- }
- }
- else if(contrast > && contrast <)
- {
- //0255?(2)?(1)?
- //(2)?nContrast = 255 * 255 / (255 - Contrast) - 255
- //nContrast?Contrast?
- CvScalar nRGB;
- int nContrast = * /( - contrast) - ;
- for (int y=; y<imageheight; y++)
- {
- for (int x=; x<imagewidth; x++)
- {
- CvScalar ori = cvGet2D(src, y, x);
- for (int k=; k<channel; k++)
- {
- nRGB.val[k] = ori.val[k] + (ori.val[k] - mean.val[k]) *nContrast /;
- }
- cvSet2D(dst, y, x, nRGB);
- }
- }
- }
- else
- {
- // 255????8?
- //??????
- for (int y=; y<imageheight; y++)
- {
- for (int x=; x<imagewidth; x++)
- {
- CvScalar rgb;
- CvScalar ori = cvGet2D(src, y, x);
- for (int k=; k<channel; k++)
- {
- if (ori.val[k] > mean.val[k])
- {
- rgb.val[k] = ;
- }
- else
- {
- rgb.val[k] = ;
- }
- }
- cvSet2D(dst, y, x, rgb);
- }
- }
- }
- }
OpenCV实现USM锐化与测试的更多相关文章
- opencv:USM锐化
USM:unsharp mask 对小的细节干扰小,对大的细节进行锐化 Mat dst; Mat blur_image; GaussianBlur(src, blur_image, Size(3, 3 ...
- USM锐化之openCV实现,附赠调整对比度函数
源地址:http://www.cnblogs.com/easymind223/archive/2012/07/03/2575277.html 常用Photoshop的玩家都知道Unsharp Mask ...
- SSE图像算法优化系列十六:经典USM锐化中的分支判断语句SSE实现的几种方法尝试。
分支判断的语句一般来说是不太适合进行SSE优化的,因为他会破坏代码的并行性,但是也不是所有的都是这样的,在合适的场景中运用SSE还是能对分支预测进行一定的优化的,我们这里以某一个算法的部分代码为例进行 ...
- C#调用GDI+1.1中的函数实现高斯模糊、USM锐化等经典效果。
http://www.cnblogs.com/Imageshop/archive/2012/12/13/2815712.html 在GDI+1.1的版本中,MS加入不少新的特性,其中的特效类Effec ...
- Win10中用yolov3训练自己的数据集全过程(VS、CUDA、CUDNN、OpenCV配置,训练和测试)
在Windows系统的Linux系统中用yolo训练自己的数据集的配置差异很大,今天总结在win10中配置yolo并进行训练和测试的全过程. 提纲: 1.下载适用于Windows的darknet 2. ...
- opencv python3.6安装和测试
安装: 命令行 pip install D:\python3.6.1\Scriptsopencv_python-3.2.0-cp36-cp36m-win_amd64.whl 测试代码: import ...
- 【QT】【OpenCv】初始配置以及测试功能
#include "mainwindow.h" #include "ui_mainwindow.h" #include<opencv2/core/core ...
- Python: PS 滤镜--USM 锐化
本文用 Python 实现 PS 滤镜中的 USM 锐化效果,具体的算法原理和效果可以参考之前的博客: http://blog.csdn.net/matrix_space/article/detail ...
- OpenCV开发环境搭建-并测试一个图像灰度处理程序
转载地址:http://blog.csdn.net/sjz_iron/article/details/8614070
随机推荐
- Handler类和Handler,Loop,MessageQueue的工作原理
原文地址:http://blog.csdn.net/xiyangyang8/article/details/50754771 Handler类的作用主要有两种: 1.在新启动的线程中发送消息. 2.在 ...
- ExtJs 日期相加,Grid表格列可编辑
1.日期相加: Ext.Date.add(new Date(), Ext.Date.DAY, 15) 2.Grid表格列可编辑: { header : "实际已交货量", ...
- Ambarella SDK build 步骤解析
Make Target Options make命令如下: make <Tab> <Tab> /*列出所有支持的目标(命令行输入make, 再按两下Tab键)*/ make & ...
- 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 ...
- UESTC--1251--谕神的密码(贪心)
谕神的密码 Time Limit: 1000MS Memory Limit: 65535KB 64bit IO Format: %lld & %llu Submit Status ...
- Cosine Similarity of Two Vectors
#include <iostream>#include <vector>#include <cmath>#include <numeric> templ ...
- Candies(差分约束系统)
http://poj.org/problem?id=3159 思路:用O(V+ElogV)的Dijkstra算法求1到n的最短路.即用优先队列优化Dijkstra算法. #include <st ...
- 【区间DP】释放囚犯
貌似和石子合并差不多 可能是我见的题太少了,所以都差不多 OK 算法分析 首先不难看出这是一道区间DP,那么,按照本蒟蒻的意思 区间DP==三个循环 for(int len=2;len<=n;l ...
- HDU 1054 Hungary
Strategic Game Problem Description Bob enjoys playing computer games, especially strategic games, bu ...
- Java基础学习(二)——对象
类:是抽象的概念集合,表示的是一个共性的产物,类之中定义的是属性和行为(方法): 对象:对象是一种个性的表示,表示一个独立的个体,每个对象拥有自己独立的属性,依靠属性来区分不同对象. 对象=实例 对象 ...