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

简介: <p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">介绍几种特殊的灰度算法滤镜,将彩色图像转换为灰度图像。其中涉及到的有基于阈值的图</p><p style="color: rgb(51, 51, 51); font-family: Arial; font-siz

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

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

 

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

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

 

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

处理流程:

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

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

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

色)

图像效果:


关键代码:

[java]  view plain copy
  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

图像效果:

关键代码:

[java]  view plain copy
  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)

原图:

处理以后

 关键代码:

[java]  view plain copy
  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. }  

[java]  view plain copy
  1. 创建新的目标Image  
[java]  view plain copy
  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. }  
相关文章
|
29天前
|
算法 计算机视觉
图像处理常用算法—6个算子 !!
图像处理常用算法—6个算子 !!
23 2
|
6月前
|
算法 C++
OpenCV-白平衡(灰度世界算法)
OpenCV-白平衡(灰度世界算法)
127 0
|
1月前
|
算法 数据可视化 计算机视觉
使用Python实现图像处理中的边缘检测算法
图像处理中的边缘检测是计算机视觉和图像识别领域的重要技术之一。本文将介绍如何利用Python语言实现常见的边缘检测算法,包括Sobel、Canny等,并结合实例演示其在图像处理中的应用。
|
4月前
|
存储 算法 搜索推荐
PACS/RIS医学影像管理系统源码 提供先进图像处理和算法
PACS/RIS医学影像管理系统源码 提供先进图像处理和算法
35 0
|
7月前
|
机器学习/深度学习 算法 C语言
FPGA图像处理之边缘检测算法的实现
边缘检测是图像处理和计算机视觉中的基本问题,边缘检测的目的是标识数字图像中亮度变化明显的点。图像属性中的显著变化通常反映了属性的重要事件和变化。这些包括(i)深度上的不连续、(ii)表面方向不连续、(iii)物质属性变化和(iv)场景照明变化。边缘检测是图像处理和计算机视觉中,尤其是特征提取中的一个研究领域。
FPGA图像处理之边缘检测算法的实现
|
7月前
|
算法 计算机视觉 异构计算
FPGA图像处理之rgbtogray算法的实现
Ycbcrr或Y'CbCr有的时候会被写作:YCBCR或是Y'CBCR,是色彩空间的一种,通常会用于影片中的影像连续处理,或是数字摄影系统中。Y'为颜色的亮度(luma)成分、而CB和CR则为蓝色和红色的浓度偏移量成份。Y'和Y是不同的,而Y就是所谓的流明(luminance),表示光的浓度且为非线性,使用伽马修正(gamma correction)编码处理。
|
9月前
|
算法 计算机视觉
差分进化算法在图像处理中的应用研究(Matlab代码实现)
差分进化算法在图像处理中的应用研究(Matlab代码实现)
|
9月前
|
算法 计算机视觉
差分进化算法在图像处理中的应用研究(Matlab代码实现)
差分进化算法在图像处理中的应用研究(Matlab代码实现)
|
1月前
|
机器学习/深度学习 算法 生物认证
基于深度学习的人员指纹身份识别算法matlab仿真
基于深度学习的人员指纹身份识别算法matlab仿真
|
23天前
|
传感器 算法 计算机视觉
基于肤色模型和中值滤波的手部检测算法FPGA实现,包括tb测试文件和MATLAB辅助验证
该内容是关于一个基于肤色模型和中值滤波的手部检测算法的描述,包括算法的运行效果图和所使用的软件版本(matlab2022a, vivado2019.2)。算法分为肤色分割和中值滤波两步,其中肤色模型在YCbCr色彩空间定义,中值滤波用于去除噪声。提供了一段核心程序代码,用于处理图像数据并在FPGA上实现。最终,检测结果输出到&quot;hand.txt&quot;文件。