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

时间:2023-03-08 22:08:11
图像处理------Mean Shift滤波(边缘保留的低通滤波)

一: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找来的转换公式截屏:

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

三:程序效果

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

滤镜源代码:

[java] view plaincopy
  1. package com.gloomyfish.filter.study;
  2. import java.awt.image.BufferedImage;
  3. public class MeanShiftFilter extends AbstractBufferedImageOp {
  4. private int radius;
  5. private float colorDistance;
  6. public MeanShiftFilter() {
  7. radius = 3; // default shift radius
  8. colorDistance = 25; // default color distance
  9. }
  10. public int getRadius() {
  11. return radius;
  12. }
  13. public void setRadius(int radius) {
  14. this.radius = radius;
  15. }
  16. public float getColorDistance() {
  17. return colorDistance;
  18. }
  19. public void setColorDistance(float colorDistance) {
  20. this.colorDistance = colorDistance;
  21. }
  22. @Override
  23. public BufferedImage filter(BufferedImage src, BufferedImage dest) {
  24. int width = src.getWidth();
  25. int height = src.getHeight();
  26. if ( dest == null )
  27. dest = createCompatibleDestImage( src, null );
  28. int[] inPixels = new int[width*height];
  29. int[] outPixels = new int[width*height];
  30. getRGB( src, 0, 0, width, height, inPixels);
  31. // convert RGB color space to YIQ color space
  32. float[][] pixelsf = new float[width*height][3];
  33. for(int i=0; i<inPixels.length; i++) {
  34. int argb = inPixels[i];
  35. int r = (argb >> 16) & 0xff;
  36. int g = (argb >>  8) & 0xff;
  37. int b = (argb) & 0xff;
  38. pixelsf[i][0] = 0.299f  *r + 0.587f *g + 0.114f  *b; // Y
  39. pixelsf[i][1] = 0.5957f *r - 0.2744f*g - 0.3212f *b; // I
  40. pixelsf[i][2] = 0.2114f *r - 0.5226f*g + 0.3111f *b; // Q
  41. }
  42. int index = 0;
  43. float shift = 0;
  44. float repetition = 0;
  45. float radius2 = radius * radius;
  46. float dis2 = colorDistance * colorDistance;
  47. for(int row=0; row<height; row++) {
  48. int ta = 255, tr = 0, tg = 0, tb = 0;
  49. for(int col=0; col<width; col++) {
  50. int xc = col;
  51. int yc = row;
  52. int xcOld, ycOld;
  53. float YcOld, IcOld, QcOld;
  54. index = row*width + col;
  55. float[] yiq = pixelsf[index];
  56. float Yc = yiq[0];
  57. float Ic = yiq[1];
  58. float Qc = yiq[2];
  59. repetition = 0;
  60. do {
  61. xcOld = xc;
  62. ycOld = yc;
  63. YcOld = Yc;
  64. IcOld = Ic;
  65. QcOld = Qc;
  66. float mx = 0;
  67. float my = 0;
  68. float mY = 0;
  69. float mI = 0;
  70. float mQ = 0;
  71. int num=0;
  72. for (int ry=-radius; ry <= radius; ry++) {
  73. int y2 = yc + ry;
  74. if (y2 >= 0 && y2 < height) {
  75. for (int rx=-radius; rx <= radius; rx++) {
  76. int x2 = xc + rx;
  77. if (x2 >= 0 && x2 < width) {
  78. if (ry*ry + rx*rx <= radius2) {
  79. yiq = pixelsf[y2*width + x2];
  80. float Y2 = yiq[0];
  81. float I2 = yiq[1];
  82. float Q2 = yiq[2];
  83. float dY = Yc - Y2;
  84. float dI = Ic - I2;
  85. float dQ = Qc - Q2;
  86. if (dY*dY+dI*dI+dQ*dQ <= dis2) {
  87. mx += x2;
  88. my += y2;
  89. mY += Y2;
  90. mI += I2;
  91. mQ += Q2;
  92. num++;
  93. }
  94. }
  95. }
  96. }
  97. }
  98. }
  99. float num_ = 1f/num;
  100. Yc = mY*num_;
  101. Ic = mI*num_;
  102. Qc = mQ*num_;
  103. xc = (int) (mx*num_+0.5);
  104. yc = (int) (my*num_+0.5);
  105. int dx = xc-xcOld;
  106. int dy = yc-ycOld;
  107. float dY = Yc-YcOld;
  108. float dI = Ic-IcOld;
  109. float dQ = Qc-QcOld;
  110. shift = dx*dx+dy*dy+dY*dY+dI*dI+dQ*dQ;
  111. repetition++;
  112. }
  113. while (shift > 3 && repetition < 100);
  114. tr = (int)(Yc + 0.9563f*Ic + 0.6210f*Qc);
  115. tg = (int)(Yc - 0.2721f*Ic - 0.6473f*Qc);
  116. tb = (int)(Yc - 1.1070f*Ic + 1.7046f*Qc);
  117. outPixels[index] = (ta << 24) | (tr << 16) | (tg << 8) | tb;
  118. }
  119. }
  120. setRGB( dest, 0, 0, width, height, outPixels );
  121. return dest;
  122. }
  123. public String toString() {
  124. System.out.println("Mean Shift Filter...");
  125. return "MeanShiftFilter";
  126. }
  127. }