⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 grayretriever.java

📁 Visual J++ 6.0中读取图像的灰度与进行灰度变换。
💻 JAVA
字号:

import java.awt.image.*;

 
public class GrayRetriever extends PointProcessor
{
	//source ---- map to ----
	//map中的元素依次对应元素于计算公式中的参数a,b,c,d
	int nMap[] = {0,255,0,255};
	
	//灰度比例化
	float scale = 1.0f;
	
	//	isGrayImage表示是否是灰色图像。这里灰色图像的灰度级数为256,其颜色模型仍然为RGBA,只是每个像素的RGB值完全相等而已的。与早期的同一概念相比,这里有不同的内涵。
	boolean isGrayImage = true;
	
	//可用于缺省情况和灰度取反,当应用于灰度取反时,需要结合该类的setType()方法
	public GrayRetriever()
	{
		this.type = this.PIXEL_GRAY_SCALE;
	}
		
	//用于灰度比例化
	public GrayRetriever(float scale)
	{
		this.type = this.PIXEL_GRAY_SCALE;
		this.scale = scale;
	}
	
	//用于:线性灰度变换及灰度截断,
	public GrayRetriever(int a,int b,int mapping_a,int mapping_b)
	{
		this.type = this.PIXEL_GRAY_LINEARIZE ;
		
		nMap[0] = a;
		nMap[1] = b;
		
		if(a > b)
		{
			nMap[0] = b;
			nMap[1] = a;
		}
		
		nMap[2] = mapping_a;
		nMap[3] = mapping_b;		
	}
	
	//只设置类型不设置参数值
	public void setType(int type)
	{
		this.type = type;
	}
	
	public void setType1f(int type,float scale)
	{
		this.type = this.PIXEL_GRAY_SCALE ;
		this.scale = scale;
	}
	
	public void setType4i(int type,int a,int b,int mapping_a,int mapping_b)
	{
 		this.type = type;
		
		nMap[0] = a;
		nMap[1] = b;
		
		if(a > b)
		{
			nMap[0] = b;
			nMap[1] = a;
		}
		
		nMap[2] = mapping_a;
		nMap[3] = mapping_b;	
	}
	

	//setImageProperty()设置图像是灰色图像还是彩色图像,当为彩色图像时,设为false。
	public void setImageProperty(boolean isGrayImage)
	{
		this.isGrayImage = isGrayImage;
	}

	//灰度修正
	public boolean retrieveRGB(int[] scr,int scansize,int[] dst)
	{
		if(scansize == 0)return false;
		int height = scr.length / scansize;
		return (this.retrieveRGB(scr,0,0,scansize,height,scansize,dst));
	}
	
	public boolean retrieveRGB(int[] scr,int x,int y,int w,int h,int scansize,int[] dst)
	{
		if(scansize <= 0) return false;
	
		int nTotal = scr.length;
		

		//原图像的宽度
		int scrWidth = scansize;
		
		int scrHeight = nTotal / scansize;
		
		if((x > (scansize - 1)) || (y > (scrHeight - 1)))return false;

		int width = w;
		int height = h;

		if((x + w) > scansize)  width = scansize  - x;
		if((y + h) > scrHeight)  height = scrHeight  - y;
		ColorModel colorModel = ColorModel.getRGBdefault();
		
 		System.arraycopy(scr,0,dst,0, nTotal);
		int b_a = nMap[1] - nMap[0];
		int d_c = nMap[3] - nMap[2];
		
		
		int i = 0;int j = 0;	
		for(i = 0; i < height;i++)
		{
			int indexBase = (y + i) * scansize;
 			for(j = 0;j < width;j++)
			{
				int index = indexBase + x + j;
				
				int r,g,b,gray;
				
				if(this.isGrayImage)
			 		gray = colorModel.getRed(scr[i]);
				else
				{
					r = colorModel.getRed(scr[index]);
					g = colorModel.getGreen(scr[index]);
					b = colorModel.getBlue(scr[index]);

					//产生 1lm (流明) 的白光所需要的三基色的近似值可用下面的灰度方程来表示:
					// 1lm(白光) = 0.30lm(红) + 0.59lm(绿) + 0.11lm(蓝)
			
					gray = (r * 30 + g * 59 + b * 11) / 100;
				}
			
				switch(this.type)
				{
					//灰度比例化
					case this.PIXEL_GRAY_SCALE :	
						r = g = b = (int)(gray * this.scale);
						break;
					case this.PIXEL_GRAY_LINEARIZE:
						
						if(b_a == 0) r = g = b = nMap[2];
						else
							r = g = b = (d_c * (gray - nMap[0])) / b_a + nMap[2];
						break;
					
					//灰度截断
					case this.PIXEL_GRAY_ROUND_OFF :
						if(gray <= nMap[0])
							r = g = b = nMap[2];
						else if(gray > nMap[1])
							r = g = b = nMap[3];
						else
							r = g = b = (d_c * (gray - nMap[0])) / b_a + nMap[2];
						
						break;
			
					//灰度取反
					case this.PIXEL_GRAY_REVERSE:
						
						r = g = b = 255 - gray;
						break;
					
					default:
						r = g = b = gray;
						break;
				}
			
				r = (r < 0) ? 0 : ((r > 255) ? 255 : r);
				g = (g < 0) ? 0 : ((g > 255) ? 255 : g);
				b = (b < 0) ? 0 : ((b > 255) ? 255 : b);

				dst[index] = 0xFF000000 | (( r << 16 ) | ( g << 8 ) | b );
			}
		}
		return true;
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -