一:Mean Shift算法介绍

Mean Shift是一种聚类算法,在数据挖掘,图像提取,视频对象跟踪中都有应用。本文

重要演示Mean Shift算法来实现图像的低通边缘保留滤波效果。其处理以后的图像有点

类似油画一样。Mean Shift算法的输入参数一般有三个:

1.      矩阵半径r,声明大小

2.      像素距离,常见为欧几里德距离或者曼哈顿距离

3.      像素差值value

算法大致的流程如下:

a.      输入像素点P(x, y)

b.      计算该点的像素值pixelv

c.      根据输入的半径r与差值value求出矩阵半径内满足差值像素平均值作为输出像素点值

d.      计算shift与repetition,如果满足条件

e.      继续c ~ d,直到条件不满足退出,得到最终的输出像素值

f.       对输入图像的每个像素重复a ~ e,得到图像输出像素数据

二:色彩空间转换

本文Mean Shift滤波在YIQ颜色空间上完成,关于RGB与YIQ颜色空间转换可以参考

这里:http://en.wikipedia.org/wiki/YIQ我google找来的转换公式截屏:

三:程序效果

滤镜源代码:

package com.gloomyfish.filter.study;import java.awt.image.BufferedImage;public class MeanShiftFilter extends AbstractBufferedImageOp {private int radius;private float colorDistance;public MeanShiftFilter() {radius = 3; // default shift radiuscolorDistance = 25; // default color distance}public int getRadius() {return radius;}public void setRadius(int radius) {this.radius = radius;}public float getColorDistance() {return colorDistance;}public void setColorDistance(float colorDistance) {this.colorDistance = colorDistance;}@Overridepublic BufferedImage filter(BufferedImage src, BufferedImage dest) {int width = src.getWidth();int height = src.getHeight();if ( dest == null )dest = createCompatibleDestImage( src, null );int[] inPixels = new int[width*height];int[] outPixels = new int[width*height];getRGB( src, 0, 0, width, height, inPixels);// convert RGB color space to YIQ color spacefloat[][] pixelsf = new float[width*height][3];for(int i=0; i<inPixels.length; i++) {int argb = inPixels[i];int r = (argb >> 16) & 0xff;int g = (argb >>  8) & 0xff;int b = (argb) & 0xff;pixelsf[i][0] = 0.299f  *r + 0.587f *g + 0.114f  *b; // Ypixelsf[i][1] = 0.5957f *r - 0.2744f*g - 0.3212f *b; // Ipixelsf[i][2] = 0.2114f *r - 0.5226f*g + 0.3111f *b; // Q}int index = 0;float shift = 0;float repetition = 0;float radius2 = radius * radius;float dis2 = colorDistance * colorDistance;for(int row=0; row<height; row++) {int ta = 255, tr = 0, tg = 0, tb = 0;for(int col=0; col<width; col++) {int xc = col;int yc = row;int xcOld, ycOld;float YcOld, IcOld, QcOld;index = row*width + col;float[] yiq = pixelsf[index];float Yc = yiq[0];float Ic = yiq[1];float Qc = yiq[2];repetition = 0;do {xcOld = xc;ycOld = yc;YcOld = Yc;IcOld = Ic;QcOld = Qc;float mx = 0;float my = 0;float mY = 0;float mI = 0;float mQ = 0;int num=0;for (int ry=-radius; ry <= radius; ry++) {int y2 = yc + ry; if (y2 >= 0 && y2 < height) {for (int rx=-radius; rx <= radius; rx++) {int x2 = xc + rx; if (x2 >= 0 && x2 < width) {if (ry*ry + rx*rx <= radius2) {yiq = pixelsf[y2*width + x2];float Y2 = yiq[0];float I2 = yiq[1];float Q2 = yiq[2];float dY = Yc - Y2;float dI = Ic - I2;float dQ = Qc - Q2;if (dY*dY+dI*dI+dQ*dQ <= dis2) {mx += x2;my += y2;mY += Y2;mI += I2;mQ += Q2;num++;}}}}}}float num_ = 1f/num;Yc = mY*num_;Ic = mI*num_;Qc = mQ*num_;xc = (int) (mx*num_+0.5);yc = (int) (my*num_+0.5);int dx = xc-xcOld;int dy = yc-ycOld;float dY = Yc-YcOld;float dI = Ic-IcOld;float dQ = Qc-QcOld;shift = dx*dx+dy*dy+dY*dY+dI*dI+dQ*dQ; repetition++;}while (shift > 3 && repetition < 100);tr = (int)(Yc + 0.9563f*Ic + 0.6210f*Qc);tg = (int)(Yc - 0.2721f*Ic - 0.6473f*Qc);tb = (int)(Yc - 1.1070f*Ic + 1.7046f*Qc);     outPixels[index] = (ta << 24) | (tr << 16) | (tg << 8) | tb;}}setRGB( dest, 0, 0, width, height, outPixels );return dest;}public String toString() {System.out.println("Mean Shift Filter...");return "MeanShiftFilter";}}

转载请注明

图像处理之Mean Shift滤波(边缘保留的低通滤波)相关推荐

  1. 图像处理------Mean Shift滤波(边缘保留的低通滤波)

    一:Mean Shift算法介绍 Mean Shift是一种聚类算法,在数据挖掘,图像提取,视频对象跟踪中都有应用.本文 重要演示Mean Shift算法来实现图像的低通边缘保留滤波效果.其处理以后的 ...

  2. python理想低通滤波、巴特沃斯低通滤波、高斯低通滤波实现

    代码 代码如下(示例): import numpy as np import cv2 as cv image = cv.imread('2.PNG') # print(image.shape) ima ...

  3. 用matlab编程实现数字图像理想低通滤波、高斯低通滤波和巴特沃斯低通滤波去噪算法

    1 理想低通滤波 %理想低通 I = imread('fig.png'); I=rgb2gray(I); figure(1); subplot(221),imshow(I); title('原图像') ...

  4. 数字图像处理学习笔记5:频率域滤波1(傅里叶频谱图,低通滤波-平滑,高通滤波-锐化)

    文章目录 前言 一.傅里叶变换:傅里叶频谱图 二.低通滤波 1.理想低通滤波 2.布特沃斯低通滤波 3.高斯低通滤波 4.小结 三.高通滤波 1.理想高通滤波 2.布特沃斯高通滤波 3.高斯高通滤波 ...

  5. c++ opencv数字图像处理:频率域滤波--低通滤波--理想低通滤波

    文章目录 前言 一.理想低通滤波器(ILPF) 二.代码 三.说明 前言 数字图像处理c++ opencv(VS2019 opencv4.53)持续更新 一.理想低通滤波器(ILPF) 通过设置频率半 ...

  6. 《OpenCv视觉之眼》Python图像处理七 :Opencv图像处理之高通滤波和低通滤波原理及构造

    本专栏主要介绍如果通过OpenCv-Python进行图像处理,通过原理理解OpenCv-Python的函数处理原型,在具体情况中,针对不同的图像进行不同等级的.不同方法的处理,以达到对图像进行去噪.锐 ...

  7. matalb 图像处理 低通滤波和高通滤波 (理想,巴特沃斯,高斯 含代码)

    低通滤波和高通滤波 主要类型和公式 主要效果图 各类型的函数代码 最终比较代码 主要类型和公式 1.低通滤波 主要分为理想低通滤波,巴特沃斯低通滤波,高斯低通滤波 理想低通滤波: 其中:对于大小为M* ...

  8. opencv 四 Mat的基本操作3(高通滤波、低通滤波、对比度调节)

    图像滤波分为高通滤波和低通滤波,高通滤波用于求图形的边缘,低通滤波用于图像去噪.图像模糊化等.这里的频是指变化(相邻像素值的变化),高通滤波是指使变化大也就是图像的边缘)的通过(低通滤波是指使变化小( ...

  9. python-opencv图像的高通滤波和低通滤波

    13.python-opencv图像的高通滤波和低通滤波 第一章 python-opencv-图片导入和显示 第二章 python-opencv图像简单处理 第三章 python-opencv图像ma ...

最新文章

  1. 朴素、Select、Poll和Epoll网络编程模型实现和分析——Poll、Epoll模型处理长连接性能比较
  2. python处理中文字符串_处理python字符串中的中文字符
  3. python怎么做软件界面_python – 如何自定义桌面应用程序的标题栏和窗口
  4. coursera 计算概论与程序设计基础(李戈)-第一题
  5. KNN分类python实现
  6. Jzoj4627 斐波那契数列
  7. 为什么现在的年轻人越来越不愿意结婚、生子了?
  8. 线程学习9——Mutex类
  9. 存数字,储未来——新华三2018存储瞄准闪存、海量、AI与超融合
  10. 听音扒谱app_掌握这些,你也可以轻松扒谱(下)
  11. MFC访问共享文件夹
  12. Goolgle10个搜索技巧
  13. 腾讯微信客服电话号码是多少
  14. 量子计算深化:大规模量子计算(相关论文108篇推荐)
  15. 5.1.6 守护进程daemon
  16. 笔记本电脑dns服务器没有响应怎么办,华硕笔记本重装系统后dns服务器未响应怎么办?...
  17. CAN接口测试工装研究
  18. 端口监控软件-Device Monitoring Studio
  19. UML统一建模(语言)和数据库建模
  20. 动力电池系统介绍(四)——电磁兼容介绍

热门文章

  1. Jenkins Mac本地环境搭建
  2. PHP字符串常用函数
  3. 在我附近的网吧的代理服务器iptables脚本
  4. linux刷新屏幕命令是什么,linux重启刷新桌面方法教程
  5. redis数据库指令
  6. 程序员撩妹指南-抖音爆火3D相册
  7. 程序员撩妹,你得看我教你的小技巧
  8. Dharma家族变体,.adobe后缀勒索病毒解密
  9. PE制作-001.UEFI和Legacy双启动U盘文件结构
  10. htm盒子模型与定位