zoukankan      html  css  js  c++  java
  • 图像处理------特殊灰度算法技巧 分类: 视频图像处理 2015-07-24 09:53 28人阅读 评论(0) 收藏

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

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

     

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

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

     

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

    处理流程:

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

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

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

    色)

    图像效果:


    关键代码:

    1. public BufferedImage filter(BufferedImage src, BufferedImage dest) {  
    2.     int width = src.getWidth();  
    3.        int height = src.getHeight();  
    4.   
    5.        if ( dest == null )  
    6.            dest = createCompatibleDestImage( src, null );  
    7.        src = super.filter(src, dest);  
    8.   
    9.        int[] inPixels = new int[width*height];  
    10.        int[] outPixels = new int[width*height];  
    11.        getRGB(src, 00, width, height, inPixels );  
    12.          
    13.        // calculate means of pixel    
    14.        int index = 0;    
    15.        double redSum = 0, greenSum = 0, blueSum = 0;    
    16.        double total = height * width;    
    17.        for(int row=0; row<height; row++) {    
    18.            int ta = 0, tr = 0, tg = 0, tb = 0;    
    19.            for(int col=0; col<width; col++) {    
    20.                index = row * width + col;    
    21.                ta = (inPixels[index] >> 24) & 0xff;    
    22.                tr = (inPixels[index] >> 16) & 0xff;    
    23.                tg = (inPixels[index] >> 8) & 0xff;    
    24.                tb = inPixels[index] & 0xff;    
    25.                redSum += tr;    
    26.                greenSum += tg;    
    27.                blueSum +=tb;    
    28.            }    
    29.        }  
    30.        int means = (int)(redSum / total);  
    31.        System.out.println(" threshold average value = " + means);  
    32.          
    33.        // dithering   
    34.        for(int row=0; row<height; row++) {  
    35.         int ta = 0, tr = 0, tg = 0, tb = 0;  
    36.         for(int col=0; col<width; col++) {  
    37.             index = row * width + col;  
    38.             ta = (inPixels[index] >> 24) & 0xff;  
    39.                tr = (inPixels[index] >> 16) & 0xff;  
    40.                tg = (inPixels[index] >> 8) & 0xff;  
    41.                tb = inPixels[index] & 0xff;  
    42.                if(tr >=means) {  
    43.                 tr = tg = tb = 255;  
    44.                } else {  
    45.                 tr = tg = tb = 0;  
    46.                }  
    47.                outPixels[index] = (ta << 24) | (tr << 16) | (tg << 8) | tb;  
    48.                  
    49.         }  
    50.        }  
    51.        setRGB( dest, 00, width, height, outPixels );  
    52.        return dest;  
    53. }  

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

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

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

    图像效果:

    关键代码:

    1. @Override  
    2. public BufferedImage filter(BufferedImage src, BufferedImage dest) {  
    3.     int width = src.getWidth();  
    4.        int height = src.getHeight();  
    5.   
    6.        if ( dest == null )  
    7.         dest = createCompatibleDestImage( src, null );  
    8.        src = super.filter(src, dest);  
    9.   
    10.        int[] inPixels = new int[width*height];  
    11.        int[] outPixels = new int[width*height];  
    12.        getRGB( src, 00, width, height, inPixels );  
    13.        int index = 0;  
    14.        for(int row=0; row<height; row++) {  
    15.         for(int col=0; col<width; col++) {  
    16.             index = row * width + col;  
    17.                int r1 = (inPixels[index] >> 16) & 0xff;  
    18.                int g1 = (inPixels[index] >> 8) & 0xff;  
    19.                int b1 = inPixels[index] & 0xff;  
    20.                int cIndex = getCloseColor(r1, g1, b1);  
    21.                outPixels[index] = (255 << 24) | (COLOR_PALETTE[cIndex][0] << 16) | (COLOR_PALETTE[cIndex][1] << 8) | COLOR_PALETTE[cIndex][2];  
    22.                int er = r1 - COLOR_PALETTE[cIndex][0];  
    23.                int eg = g1 - COLOR_PALETTE[cIndex][1];  
    24.                int eb = b1 -  COLOR_PALETTE[cIndex][2];  
    25.                int k = 0;  
    26.                  
    27.                if(row + 1 < height && col - 1 > 0) {  
    28.                 k = (row + 1) * width + col - 1;  
    29.                    r1 = (inPixels[k] >> 16) & 0xff;  
    30.                    g1 = (inPixels[k] >> 8) & 0xff;  
    31.                    b1 = inPixels[k] & 0xff;  
    32.                    r1 += (int)(er * kernelData[0]);  
    33.                    g1 += (int)(eg * kernelData[0]);  
    34.                    b1 += (int)(eb * kernelData[0]);  
    35.                    inPixels[k] = (255 << 24) | (clamp(r1) << 16) | (clamp(g1) << 8) | clamp(b1);  
    36.                }  
    37.                  
    38.                if(col + 1 < width) {  
    39.                 k = row * width + col + 1;  
    40.                    r1 = (inPixels[k] >> 16) & 0xff;  
    41.                    g1 = (inPixels[k] >> 8) & 0xff;  
    42.                    b1 = inPixels[k] & 0xff;  
    43.                    r1 += (int)(er * kernelData[3]);  
    44.                    g1 += (int)(eg * kernelData[3]);  
    45.                    b1 += (int)(eb * kernelData[3]);  
    46.                    inPixels[k] = (255 << 24) | (clamp(r1) << 16) | (clamp(g1) << 8) | clamp(b1);  
    47.                }  
    48.                  
    49.                if(row + 1 < height) {  
    50.                 k = (row + 1) * width + col;  
    51.                    r1 = (inPixels[k] >> 16) & 0xff;  
    52.                    g1 = (inPixels[k] >> 8) & 0xff;  
    53.                    b1 = inPixels[k] & 0xff;  
    54.                    r1 += (int)(er * kernelData[1]);  
    55.                    g1 += (int)(eg * kernelData[1]);  
    56.                    b1 += (int)(eb * kernelData[1]);  
    57.                    inPixels[k] = (255 << 24) | (clamp(r1) << 16) | (clamp(g1) << 8) | clamp(b1);  
    58.                }  
    59.                  
    60.                if(row + 1 < height && col + 1 < width) {  
    61.                 k = (row + 1) * width + col + 1;  
    62.                    r1 = (inPixels[k] >> 16) & 0xff;  
    63.                    g1 = (inPixels[k] >> 8) & 0xff;  
    64.                    b1 = inPixels[k] & 0xff;  
    65.                    r1 += (int)(er * kernelData[2]);  
    66.                    g1 += (int)(eg * kernelData[2]);  
    67.                    b1 += (int)(eb * kernelData[2]);  
    68.                    inPixels[k] = (255 << 24) | (clamp(r1) << 16) | (clamp(g1) << 8) | clamp(b1);  
    69.                }  
    70.         }  
    71.        }  
    72.        setRGB( dest, 00, width, height, outPixels );  
    73.        return dest;  
    74. }  
    3.       选择性灰度算法

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

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

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

    原图:

    处理以后

     关键代码:

    1. public BufferedImage filter(BufferedImage src, BufferedImage dest) {  
    2.     int width = src.getWidth();  
    3.        int height = src.getHeight();  
    4.   
    5.        if ( dest == null )  
    6.         dest = createCompatibleDestImage( src, null );  
    7.   
    8.        int[] inPixels = new int[width*height];  
    9.        int[] outPixels = new int[width*height];  
    10.        getRGB( src, 00, width, height, inPixels );  
    11.        int index = 0;  
    12.        for(int row=0; row<height; row++) {  
    13.         int ta = 0, tr = 0, tg = 0, tb = 0;  
    14.         for(int col=0; col<width; col++) {  
    15.             index = row * width + col;  
    16.             ta = (inPixels[index] >> 24) & 0xff;  
    17.                tr = (inPixels[index] >> 16) & 0xff;  
    18.                tg = (inPixels[index] >> 8) & 0xff;  
    19.                tb = inPixels[index] & 0xff;  
    20.                int gray = (int)(0.299 * (double)tr + 0.587 * (double)tg + 0.114 * (double)tb);  
    21.                double distance = getDistance(tr, tg, tb);  
    22.                if(distance < threshold) {  
    23.                 double k = distance / threshold;  
    24.                 int[] rgb = getAdjustableRGB(tr, tg, tb, gray, (float)k);  
    25.                 tr = rgb[0];  
    26.                 tg = rgb[1];  
    27.                 tb = rgb[2];  
    28.                 outPixels[index] = (ta << 24) | (tr << 16) | (tg << 8) | tb;  
    29.                } else {  
    30.                 outPixels[index] = (ta << 24) | (gray << 16) | (gray << 8) | gray;                      
    31.                }  
    32.                  
    33.         }  
    34.        }  
    35.        setRGB( dest, 00, width, height, outPixels );  
    36.        return dest;  
    37. }  

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

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    .Net常见面试题整理(一)——值类型和引用类型
    c#字符串常见操作
    sealed,new,virtual,abstract与override
    c#好的程序员必须掌握的编码习惯
    c#区分传值调用 传引用调用。
    C# const和readonly的区别
    .Net常见面试题整理(二)——装箱和拆箱
    C#Equals()和运算符==的区别
    private,protected,public和internal的区别
    2013应届毕业生“数码视讯”校招应聘总结
  • 原文地址:https://www.cnblogs.com/mao0504/p/4706367.html
Copyright © 2011-2022 走看看