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

简介: 图像处理之特殊灰度算法技巧 介绍几种特殊的灰度算法滤镜,将彩色图像转换为灰度图像。其中涉及到的有基于阈值的图 像二值化,弗洛伊德.斯坦德伯格抖动算法,基于阈值的部分灰度化   基础知识- 怎么把RGB转换为单色的[0 ~256]之间的灰度,最常用的转换公式如下: Gray = 0.

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


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

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

 

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

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

 

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

处理流程:

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

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

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

色)

图像效果:


关键代码:

	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

图像效果:

关键代码:

	@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)

原图:

处理以后

 关键代码:

	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);
    }


目录
相关文章
|
2月前
|
算法 计算机视觉
图像处理常用算法—6个算子 !!
图像处理常用算法—6个算子 !!
30 2
|
7月前
|
算法 C++
OpenCV-白平衡(灰度世界算法)
OpenCV-白平衡(灰度世界算法)
153 0
|
2月前
|
算法 数据可视化 计算机视觉
使用Python实现图像处理中的边缘检测算法
图像处理中的边缘检测是计算机视觉和图像识别领域的重要技术之一。本文将介绍如何利用Python语言实现常见的边缘检测算法,包括Sobel、Canny等,并结合实例演示其在图像处理中的应用。
|
5月前
|
存储 算法 搜索推荐
PACS/RIS医学影像管理系统源码 提供先进图像处理和算法
PACS/RIS医学影像管理系统源码 提供先进图像处理和算法
39 0
|
8月前
|
机器学习/深度学习 算法 C语言
FPGA图像处理之边缘检测算法的实现
边缘检测是图像处理和计算机视觉中的基本问题,边缘检测的目的是标识数字图像中亮度变化明显的点。图像属性中的显著变化通常反映了属性的重要事件和变化。这些包括(i)深度上的不连续、(ii)表面方向不连续、(iii)物质属性变化和(iv)场景照明变化。边缘检测是图像处理和计算机视觉中,尤其是特征提取中的一个研究领域。
FPGA图像处理之边缘检测算法的实现
|
8月前
|
算法 计算机视觉 异构计算
FPGA图像处理之rgbtogray算法的实现
Ycbcrr或Y'CbCr有的时候会被写作:YCBCR或是Y'CBCR,是色彩空间的一种,通常会用于影片中的影像连续处理,或是数字摄影系统中。Y'为颜色的亮度(luma)成分、而CB和CR则为蓝色和红色的浓度偏移量成份。Y'和Y是不同的,而Y就是所谓的流明(luminance),表示光的浓度且为非线性,使用伽马修正(gamma correction)编码处理。
|
10月前
|
算法 计算机视觉
差分进化算法在图像处理中的应用研究(Matlab代码实现)
差分进化算法在图像处理中的应用研究(Matlab代码实现)
|
10月前
|
算法 计算机视觉
差分进化算法在图像处理中的应用研究(Matlab代码实现)
差分进化算法在图像处理中的应用研究(Matlab代码实现)
|
12天前
|
机器学习/深度学习 人工智能 算法
基于DCT和扩频的音频水印嵌入提取算法matlab仿真
本文介绍了结合DCT和扩频技术的音频水印算法,用于在不降低音质的情况下嵌入版权信息。在matlab2022a中实现,算法利用DCT进行频域处理,通过扩频增强水印的隐蔽性和抗攻击性。核心程序展示了水印的嵌入与提取过程,包括DCT变换、水印扩频及反变换步骤。该方法有效且专业,未来研究将侧重于提高实用性和安全性。
|
2天前
|
算法 数据安全/隐私保护 计算机视觉
基于DCT变换的彩色图像双重水印嵌入和提取算法matlab仿真
**算法摘要:** - 图形展示:展示灰度与彩色图像水印应用,主辅水印嵌入。 - 软件环境:MATLAB 2022a。 - 算法原理:双重水印,转换至YCbCr/YIQ,仅影响亮度;图像分割为M×N块,DCT变换后嵌入水印。 - 流程概览:两步水印嵌入,每步对应不同图示表示。 - 核心代码未提供。