java 图像特效之黑白 浮雕和底片

阅读本系列,请先看前言!谢谢

所谓黑白图片其实就是对图像进行去色,也就是灰度化,只要调用之前写过的灰度化函数即可。

底片的原理就是对RGB取补色即可。也就是将当前像素点的RGB值分别与255之差后的值作为当前点的RGB值,即
R = 255 – R;G = 255 – G;B = 255 – B;

浮雕其实就是一种边缘检测,在图像处理基础那个系列中,介绍过的sobel算子,Prewitt算子 或者是拉普拉斯算子我觉得都有着类似的效果

本文采用了新的梯度检测算法,相比之前介绍的边缘检测算子,效果稍弱,但是视觉效果好。

底片的代码:

public Image filter() {
		if(this.img.gray)
			return this.img;  // Grayscale images can not be processed
		
		for (int y = 0; y < this.img.h; y++) {
            for (int x = 0; x < this.img.w; x++) {
            	
                int c = this.img.data[x + y * this.img.w];
                int A = (c >> 24) & 0xff; 
                int R = (c >> 16) & 0xFF;
                int G = (c >> 8) & 0xFF;
                int B = (c >> 0) & 0xFF;
                
                R = 255 - R;  
                G = 255 - G;  
                B = 255 - B;  
                  
                this.img.data[x + y * this.img.w] = (A << 24) | (R << 16) | (G << 8) | B;              
            }
        }
		
		return this.img;
	}

浮雕的代码:

public Image filter() {
		
		if(this.img.gray)
			return this.img;  // Grayscale images can not be processed
		
		int[] d = new int[this.img.w*this.img.h]; //what's the fuck. it must be writed as this. not int[] d = this.img.data;
		
		for (int y = 0; y < this.img.h -1; y++) {
            for (int x = 0; x < this.img.w -1; x++) {

            	int pc = this.img.data[x + y * this.img.w];
            	
            	int PR = (pc >> 16) & 0xFF;
                int PG = (pc >> 8) & 0xFF;
                int PB = (pc >> 0) & 0xFF;
                
            	int c = this.img.data[x + 1 + (y+1) * this.img.w];
                int R = (c >> 16) & 0xFF;
                int G = (c >> 8) & 0xFF;
                int B = (c >> 0) & 0xFF;
                
                R = PR - R + 128;  
                G = PG - G + 128;  
                B = PB - B + 128;  
                
                d[x + y * this.img.w] = (255 << 24) | (R << 16) | (G << 8) | B;    
            }
        }
		
		this.img.data = d;
		this.img.toGray();
		return this.img;
	}

处理后的效果

java 图像特效之黑白 浮雕和底片_第1张图片java 图像特效之黑白 浮雕和底片_第2张图片java 图像特效之黑白 浮雕和底片_第3张图片

其实除了浮雕之外,都可以用颜色矩阵来实现。

你可能感兴趣的:(java,图像特效,java,图像处理基础与图像特效)