图像处理之特殊灰度算法技巧

简介: 图像处理之特殊灰度算法技巧

图像处理之特殊灰度算法技巧


介绍几种特殊的灰度算法滤镜,将彩色图像转换为灰度图像。其中涉及到的有基于阈值的图

二值化,弗洛伊德.斯坦德伯格抖动算法,基于阈值的部分灰度化

 基础知识- 怎么把RGB转换为单色的[0 ~256]之间的灰度,最常用的转换公式如下:

Gray = 0.299 * red + 0.587 * green + 0.114 * blue;

 

1.       基于像素平均值的图像阈值二值化算法:

处理流程:

a.      首先将彩色图像转换为灰度图像

b.      计算灰度图像的算术平均值– M

c.      以M为阈值,完成对灰度图二值化( 大于阈值M,像素点赋值为白色,否则赋值为黑

色)

图像效果:

1332663553_3036.png

关键代码:

  public BufferedImage filter(BufferedImage src, BufferedImage dest) {
    int width = src.getWidth();
        int height = src.getHeight();
 
        if ( dest == null )
            dest = createCompatibleDestImage( src, null );
        src = super.filter(src, dest);
 
        int[] inPixels = new int[width*height];
        int[] outPixels = new int[width*height];
        getRGB(src, 0, 0, width, height, inPixels );
        
        // calculate means of pixel  
        int index = 0;  
        double redSum = 0, greenSum = 0, blueSum = 0;  
        double total = height * width;  
        for(int row=0; row<height; row++) {  
            int ta = 0, tr = 0, tg = 0, tb = 0;  
            for(int col=0; col<width; col++) {  
                index = row * width + col;  
                ta = (inPixels[index] >> 24) & 0xff;  
                tr = (inPixels[index] >> 16) & 0xff;  
                tg = (inPixels[index] >> 8) & 0xff;  
                tb = inPixels[index] & 0xff;  
                redSum += tr;  
                greenSum += tg;  
                blueSum +=tb;  
            }  
        }
        int means = (int)(redSum / total);
        System.out.println(" threshold average value = " + means);
        
        // dithering 
        for(int row=0; row<height; row++) {
          int ta = 0, tr = 0, tg = 0, tb = 0;
          for(int col=0; col<width; col++) {
            index = row * width + col;
            ta = (inPixels[index] >> 24) & 0xff;
                tr = (inPixels[index] >> 16) & 0xff;
                tg = (inPixels[index] >> 8) & 0xff;
                tb = inPixels[index] & 0xff;
                if(tr >=means) {
                  tr = tg = tb = 255;
                } else {
                  tr = tg = tb = 0;
                }
                outPixels[index] = (ta << 24) | (tr << 16) | (tg << 8) | tb;
                
          }
        }
        setRGB( dest, 0, 0, width, height, outPixels );
        return dest;
  }

2.       基于错误扩散的Floyd-Steinberg抖动算法

关于什么是Floyd-Steinberg抖动,参见这里

http://en.wikipedia.org/wiki/Floyd–Steinberg_dithering

图像效果: 1332663651_6195.png

关键代码:

  @Override
  public BufferedImage filter(BufferedImage src, BufferedImage dest) {
    int width = src.getWidth();
        int height = src.getHeight();
 
        if ( dest == null )
          dest = createCompatibleDestImage( src, null );
        src = super.filter(src, dest);
 
        int[] inPixels = new int[width*height];
        int[] outPixels = new int[width*height];
        getRGB( src, 0, 0, width, height, inPixels );
        int index = 0;
        for(int row=0; row<height; row++) {
          for(int col=0; col<width; col++) {
            index = row * width + col;
                int r1 = (inPixels[index] >> 16) & 0xff;
                int g1 = (inPixels[index] >> 8) & 0xff;
                int b1 = inPixels[index] & 0xff;
                int cIndex = getCloseColor(r1, g1, b1);
                outPixels[index] = (255 << 24) | (COLOR_PALETTE[cIndex][0] << 16) | (COLOR_PALETTE[cIndex][1] << 8) | COLOR_PALETTE[cIndex][2];
                int er = r1 - COLOR_PALETTE[cIndex][0];
                int eg = g1 - COLOR_PALETTE[cIndex][1];
                int eb = b1 -  COLOR_PALETTE[cIndex][2];
                int k = 0;
                
                if(row + 1 < height && col - 1 > 0) {
                  k = (row + 1) * width + col - 1;
                    r1 = (inPixels[k] >> 16) & 0xff;
                    g1 = (inPixels[k] >> 8) & 0xff;
                    b1 = inPixels[k] & 0xff;
                    r1 += (int)(er * kernelData[0]);
                    g1 += (int)(eg * kernelData[0]);
                    b1 += (int)(eb * kernelData[0]);
                    inPixels[k] = (255 << 24) | (clamp(r1) << 16) | (clamp(g1) << 8) | clamp(b1);
                }
                
                if(col + 1 < width) {
                  k = row * width + col + 1;
                    r1 = (inPixels[k] >> 16) & 0xff;
                    g1 = (inPixels[k] >> 8) & 0xff;
                    b1 = inPixels[k] & 0xff;
                    r1 += (int)(er * kernelData[3]);
                    g1 += (int)(eg * kernelData[3]);
                    b1 += (int)(eb * kernelData[3]);
                    inPixels[k] = (255 << 24) | (clamp(r1) << 16) | (clamp(g1) << 8) | clamp(b1);
                }
                
                if(row + 1 < height) {
                  k = (row + 1) * width + col;
                    r1 = (inPixels[k] >> 16) & 0xff;
                    g1 = (inPixels[k] >> 8) & 0xff;
                    b1 = inPixels[k] & 0xff;
                    r1 += (int)(er * kernelData[1]);
                    g1 += (int)(eg * kernelData[1]);
                    b1 += (int)(eb * kernelData[1]);
                    inPixels[k] = (255 << 24) | (clamp(r1) << 16) | (clamp(g1) << 8) | clamp(b1);
                }
                
                if(row + 1 < height && col + 1 < width) {
                  k = (row + 1) * width + col + 1;
                    r1 = (inPixels[k] >> 16) & 0xff;
                    g1 = (inPixels[k] >> 8) & 0xff;
                    b1 = inPixels[k] & 0xff;
                    r1 += (int)(er * kernelData[2]);
                    g1 += (int)(eg * kernelData[2]);
                    b1 += (int)(eb * kernelData[2]);
                    inPixels[k] = (255 << 24) | (clamp(r1) << 16) | (clamp(g1) << 8) | clamp(b1);
                }
          }
        }
        setRGB( dest, 0, 0, width, height, outPixels );
        return dest;
  }

3.       选择性灰度算法

计算选择的颜色与像素灰度颜色之间的几何距离值,跟阈值比较决定是否像素点为灰度

值,可以得到一些让你意想不到的图像处理效果!

图像效果 (Main Color = GREEN, 阈值 = 200)

原图:

image.png

处理以后

image.png

关键代码:

  public 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 );
        int index = 0;
        for(int row=0; row<height; row++) {
          int ta = 0, tr = 0, tg = 0, tb = 0;
          for(int col=0; col<width; col++) {
            index = row * width + col;
            ta = (inPixels[index] >> 24) & 0xff;
                tr = (inPixels[index] >> 16) & 0xff;
                tg = (inPixels[index] >> 8) & 0xff;
                tb = inPixels[index] & 0xff;
                int gray = (int)(0.299 * (double)tr + 0.587 * (double)tg + 0.114 * (double)tb);
                double distance = getDistance(tr, tg, tb);
                if(distance < threshold) {
                  double k = distance / threshold;
                  int[] rgb = getAdjustableRGB(tr, tg, tb, gray, (float)k);
                  tr = rgb[0];
                  tg = rgb[1];
                  tb = rgb[2];
                  outPixels[index] = (ta << 24) | (tr << 16) | (tg << 8) | tb;
                } else {
                  outPixels[index] = (ta << 24) | (gray << 16) | (gray << 8) | gray;                  
                }
                
          }
        }
        setRGB( dest, 0, 0, width, height, outPixels );
        return dest;
  }


创建新的目标Image
    public BufferedImage createCompatibleDestImage(BufferedImage src, ColorModel dstCM) {
        if ( dstCM == null )
            dstCM = src.getColorModel();
        return new BufferedImage(dstCM, dstCM.createCompatibleWritableRaster(src.getWidth(), src.getHeight()), dstCM.isAlphaPremultiplied(), null);
    }

相关文章
|
24天前
|
算法 计算机视觉
图像处理之积分图应用四(基于局部均值的图像二值化算法)
图像处理之积分图应用四(基于局部均值的图像二值化算法)
92 0
|
24天前
|
监控 算法 图计算
图像处理之积分图应用三(基于NCC快速相似度匹配算法)
图像处理之积分图应用三(基于NCC快速相似度匹配算法)
20 0
|
24天前
|
资源调度 算法 计算机视觉
图像处理之积分图应用二(快速边缘保留滤波算法)
图像处理之积分图应用二(快速边缘保留滤波算法)
12 0
|
24天前
|
算法 BI 计算机视觉
图像处理之积分图应用一(半径无关的快速模糊算法)
图像处理之积分图应用一(半径无关的快速模糊算法)
14 0
|
24天前
|
算法 Java 计算机视觉
图像处理之积分图算法
图像处理之积分图算法
14 2
|
24天前
|
文字识别 算法 计算机视觉
图像处理之Zhang Suen细化算法
图像处理之Zhang Suen细化算法
16 0
|
24天前
|
算法 计算机视觉
图像处理之基于泛红算法的二值图像内部区域填充
图像处理之基于泛红算法的二值图像内部区域填充
22 0
|
1天前
|
算法 索引
基于Prony算法的系统参数辨识matlab仿真
Prony算法在MATLAB2022a中用于信号分析,识别复指数信号成分。核心程序通过模拟信号X1,添加不同SNR的噪声,应用Prony方法处理并计算误差。算法基于离散序列的复指数叠加模型,通过构建矩阵并解线性方程组估计参数,实现LTI系统动态特性的辨识。
|
2天前
|
算法 安全 数据库
基于结点电压法的配电网状态估计算法matlab仿真
**摘要** 该程序实现了基于结点电压法的配电网状态估计算法,旨在提升数据的准确性和可靠性。在MATLAB2022a中运行,显示了状态估计过程中的电压和相位估计值,以及误差随迭代变化的图表。算法通过迭代计算雅可比矩阵,结合基尔霍夫定律解决线性方程组,估算网络节点电压。状态估计过程中应用了高斯-牛顿或莱文贝格-马夸尔特法,处理量测数据并考虑约束条件,以提高估计精度。程序结果以图形形式展示电压幅值和角度估计的比较,以及估计误差的演变,体现了算法在处理配电网状态估计问题的有效性。
|
2天前
|
算法
基于PSO粒子群优化的PID控制器参数整定算法matlab仿真
该文探讨了使用PSO(粒子群优化)算法优化PID控制器参数的方法。通过PSO迭代,不断调整PID控制器的Kp、Ki、Kd增益,以减小控制误差。文中提供了MATLAB2022a版本的核心代码,展示了参数优化过程及结果。系统仿真图像显示了参数随迭代优化的变化。PID控制器结合PSO算法能有效提升控制性能,适用于复杂系统的参数整定,未来研究可关注算法效率提升和应对不确定性。