java 向量量化

來源:互聯網
上載者:User

下面代碼中用到的sourceImage是一個已經存在的Image對象

  映像剪下

  對於一個已經存在的Image對象,要得到它的一個局部映像,可以使用下面的步驟:

//import java.awt.*;
//import java.awt.image.*;
Image croppedImage;
ImageFilter cropFilter;
CropFilter =new CropImageFilter(25,30,75,75);//四個參數分別為映像起點座標和寬高,即CropImageFilter(int x,int y,int width,int height),詳細情況請參考API
CroppedImage= Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(sourceImage.getSource(),cropFilter));
//如果是在Component的子類中使用,可以將上面的Toolkit.getDefaultToolkit().去掉。
//FilteredImageSource是一個ImageProducer對象。

  映像縮放

  對於一個已經存在的Image對象,得到它的一個縮放的Image對象可以使用Image的getScaledInstance方法:

Image scaledImage=sourceImage. getScaledInstance(100,100, Image.SCALE_DEFAULT);//得到一個100X100的映像
Image doubledImage=sourceImage. getScaledInstance(sourceImage.getWidth(this)*2,sourceImage.getHeight(this)*2, Image.SCALE_DEFAULT);//得到一個放大兩倍的映像,這個程式一般在一個swing的組件中使用,而類Jcomponent實現了映像觀察者介面ImageObserver,所有可以使用this。
//其它情況請參考API。

  灰階變換

  下面的程式使用三種方法對一個彩色映像進行灰階變換,變換的效果都不一樣。一般而言,灰階變換的演算法是將象素的三個顏色分量使用R*0.3+G*0.59+B*0.11得到灰階值,然後將之賦值給紅綠藍,這樣顏色取得的效果就是灰階的。另一種就是取紅綠藍三色中的最大值作為灰階值。java核心包也有一種演算法,但是沒有看原始碼,不知道具體演算法是什麼樣的,效果和上述不同。

/* GrayFilter.java*/
/*@author:cherami */
/*email:cherami@163.net*/
import java.awt.image.*;

public class GrayFilter extends RGBImageFilter {
int modelStyle;
public GrayFilter() {
modelStyle=GrayModel.CS_MAX;
canFilterIndexColorModel=true;
}
public GrayFilter(int style) {
modelStyle=style;
canFilterIndexColorModel=true;
}
public void setColorModel(ColorModel cm) {
if (modelStyle==GrayModel.CS_MAX) {
substituteColorModel(cm,new GrayModel(cm));
}
else if (modelStyle==GrayModel.CS_FLOAT) {
substituteColorModel(cm,new GrayModel(cm,modelStyle));
}
}
public int filterRGB(int x,int y,int pixel) {
return pixel;
}
}

/* GrayModel.java*/
/*@author:cherami */
/*email:cherami@163.net*/

import java.awt.image.*;

public class GrayModel extends ColorModel {
public static final int CS_MAX=0;
public static final int CS_FLOAT=1;
ColorModel sourceModel;
int modelStyle;
public GrayModel(ColorModel sourceModel) {
super(sourceModel.getPixelSize());
this.sourceModel=sourceModel;
modelStyle=0;
}
public GrayModel(ColorModel sourceModel,int style) {
super(sourceModel.getPixelSize());
this.sourceModel=sourceModel;
modelStyle=style;
}
public void setGrayStyle(int style) {
modelStyle=style;
}
protected int getGrayLevel(int pixel) {
if (modelStyle==CS_MAX) {
return Math.max(sourceModel.getRed(pixel),Math.max(sourceModel.getGreen(pixel),sourceModel.getBlue(pixel)));
}
else if (modelStyle==CS_FLOAT){
return (int)(sourceModel.getRed(pixel)*0.3+sourceModel.getGreen(pixel)*0.59+sourceModel.getBlue(pixel)*0.11);
}
else {
return 0;
}
}
public int getAlpha(int pixel) {
return sourceModel.getAlpha(pixel);
}
public int getRed(int pixel) {
return getGrayLevel(pixel);
}
public int getGreen(int pixel) {
return getGrayLevel(pixel);
}
public int getBlue(int pixel) {
return getGrayLevel(pixel);
}
public int getRGB(int pixel) {
int gray=getGrayLevel(pixel);
return (getAlpha(pixel)<<24)+(gray<<16)+(gray<<8)+gray;
}
}

  如果你有自己的演算法或者想取得特殊的效果,你可以修改類GrayModel的方法getGrayLevel()。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.