新聞中心
用java編寫數(shù)字圖像處理中的代碼好不好寫
java基于圖形處理的類庫也很強(qiáng)大,建議查閱javaSE 的API,java學(xué)到最后拼的都是自學(xué)能力,針對不同領(lǐng)域的應(yīng)用,有成千上萬的類庫等著你去探索。查查資料,看看API,一定能解決。曾經(jīng)用java寫過簡單的圖形驗(yàn)證碼,印象中好像寫了100多行代碼,這個(gè)應(yīng)該算是最簡單的了,至于你說的數(shù)字圖像處理,這個(gè)概念很大!建議從具體需求出發(fā),所有問題都能迎刃而解,沒有什么好不好寫之說,只有想不想寫之念
堅(jiān)守“ 做人真誠 · 做事靠譜 · 口碑至上 · 高效敬業(yè) ”的價(jià)值觀,專業(yè)網(wǎng)站建設(shè)服務(wù)10余年為成都成都水處理設(shè)備小微創(chuàng)業(yè)公司專業(yè)提供成都企業(yè)網(wǎng)站建設(shè)營銷網(wǎng)站建設(shè)商城網(wǎng)站建設(shè)手機(jī)網(wǎng)站建設(shè)小程序網(wǎng)站建設(shè)網(wǎng)站改版,從內(nèi)容策劃、視覺設(shè)計(jì)、底層架構(gòu)、網(wǎng)頁布局、功能開發(fā)迭代于一體的高端網(wǎng)站建設(shè)服務(wù)。
關(guān)于java圖像處理
Java圖像處理技巧四則
下面代碼中用到的sourceImage是一個(gè)已經(jīng)存在的Image對象
圖像剪切
對于一個(gè)已經(jīng)存在的Image對象,要得到它的一個(gè)局部圖像,可以使用下面的步驟:
//import java.awt.*;
//import java.awt.image.*;
Image croppedImage;
ImageFilter cropFilter;
CropFilter =new CropImageFilter(25,30,75,75); //四個(gè)參數(shù)分別為圖像起點(diǎn)坐標(biāo)和寬高,即CropImageFilter(int x,int y,int width,int height),詳細(xì)情況請參考API
CroppedImage= Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(sourceImage.getSource(),cropFilter));
如果是在Component的子類中使用,可以將上面的Toolkit.getDefaultToolkit().去掉。FilteredImageSource是一個(gè)ImageProducer對象。
圖像縮放
對于一個(gè)已經(jīng)存在的Image對象,得到它的一個(gè)縮放的Image對象可以使用Image的getScaledInstance方法:
Image scaledImage=sourceImage. getScaledInstance(100,100, Image.SCALE_DEFAULT); //得到一個(gè)100X100的圖像
Image doubledImage=sourceImage. getScaledInstance(sourceImage.getWidth(this)*2,sourceImage.getHeight(this)*2, Image.SCALE_DEFAULT); //得到一個(gè)放大兩倍的圖像,這個(gè)程序一般在一個(gè)swing的組件中使用,而類Jcomponent實(shí)現(xiàn)了圖像觀察者接口ImageObserver,所有可以使用this。
//其它情況請參考API
灰度變換
下面的程序使用三種方法對一個(gè)彩色圖像進(jìn)行灰度變換,變換的效果都不一樣。一般而言,灰度變換的算法是將象素的三個(gè)顏色分量使用R*0.3+G*0.59+ B*0.11得到灰度值,然后將之賦值給紅綠藍(lán),這樣顏色取得的效果就是灰度的。另一種就是取紅綠藍(lán)三色中的最大值作為灰度值。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
else if (modelStyle==GrayModel
}
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)+(gray16)+(gray8)+gray;
}
}
如果你有自己的算法或者想取得特殊的效果,你可以修改類GrayModel的方法getGrayLevel()。
色彩變換
根據(jù)上面的原理,我們也可以實(shí)現(xiàn)色彩變換,這樣的效果就很多了。下面是一個(gè)反轉(zhuǎn)變換的例子:
/* ReverseColorModel.java*/
/*@author:cherami */
/*email:cherami@163.net*/
import java.awt.image.*;
public class ReverseColorModel extends ColorModel {
ColorModel sourceModel;
public ReverseColorModel(ColorModel sourceModel) {
super(sourceModel.getPixelSize());
this.sourceModel=sourceModel;
}
public int getAlpha(int pixel) {
return sourceModel.getAlpha(pixel);
}
public int getRed(int pixel) {
return ~sourceModel.getRed(pixel);
}
public int getGreen(int pixel) {
return ~sourceModel.getGreen(pixel);
}
public int getBlue(int pixel) {
return ~sourceModel.getBlue(pixel);
}
public int getRGB(int pixel) {
return (getAlpha(pixel)24)+(getRed(pixel)16)+(getGreen(pixel)8)+getBlue(pixel);
}
}
/* ReverseColorModel.java*/
/*@author:cherami */
/*email:cherami@163.net*/
import java.awt.image.*;
public class ReverseFilter extends RGBImageFilter {
public ReverseFilter() {
canFilterIndexColorModel=true;
}
public void setColorModel(ColorModel cm) {
substituteColorModel(cm,new ReverseColorModel(cm));
}
public int filterRGB(int x,int y,int pixel) {
return pixel;
}
}
要想取得自己的效果,需要修改ReverseColorModel.java中的三個(gè)方法,getRed、getGreen、getBlue。
下面是上面的效果的一個(gè)總的演示程序。
/*GrayImage.java*/
/*@author:cherami */
/*email:cherami@163.net*/
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import java.awt.color.*;
public class GrayImage extends JFrame{
Image source,gray,gray3,clip,bigimg;
BufferedImage bimg,gray2;
GrayFilter filter,filter2;
ImageIcon ii;
ImageFilter cropFilter;
int iw,ih;
public GrayImage() {
ii=new ImageIcon(\"images/11.gif\");
source=ii.getImage();
iw=source.getWidth(this);
ih=source.getHeight(this);
filter=new GrayFilter();
filter2=new GrayFilter(GrayModel.CS_FLOAT);
gray=createImage(new FilteredImageSource(source.getSource(),filter));
gray3=createImage(new FilteredImageSource(source.getSource(),filter2));
cropFilter=new CropImageFilter(5,5,iw-5,ih-5);
clip=createImage(new FilteredImageSource(source.getSource(),cropFilter));
bigimg=source.getScaledInstance(iw*2,ih*2,Image.SCALE_DEFAULT);
MediaTracker mt=new MediaTracker(this);
mt.addImage(gray,0);
try {
mt.waitForAll();
} catch (Exception e) {
}
用java編寫一個(gè)圖像處理,光線補(bǔ)償 、
寫了很多篇關(guān)于圖像處理的文章,沒有一篇介紹Java 2D的圖像處理API,文章討論和提及的
API都是基于JDK6的,首先來看Java中如何組織一個(gè)圖像對象BufferedImage的,如圖:
一個(gè)BufferedImage的像素?cái)?shù)據(jù)儲存在Raster中,ColorModel里面儲存顏色空間,類型等
信息,當(dāng)前Java只支持一下三種圖像格式- JPG,PNG,GIF,如何向讓Java支持其它格式,首
先要 完成Java中的圖像讀寫接口,然后打成jar,加上啟動(dòng)參數(shù)- Xbootclasspath/p
newimageformatIO.jar即可。
Java中如何讀寫一個(gè)圖像文件,使用ImageIO對象即可。讀圖像文件的代碼如下:
File file = new File("D:\\test\\blue_flower.jpg");
BufferedImage image = ImageIO.read(file);
寫圖像文件的代碼如下:
File outputfile = new File("saved.png");
ImageIO.write(bufferedImage, "png",outputfile);
從BufferedImage對象中讀取像素?cái)?shù)據(jù)的代碼如下:
1 int type= image.getType();2 if ( type ==BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB )3 ? ? ?return (int [])image.getRaster().getDataElements(x, y, width, height, pixels );4 else5 ? ? return image.getRGB( x, y, width, height, pixels, 0, width );
首先獲取圖像類型,如果不是32位的INT型數(shù)據(jù),直接讀寫RGB值即可,否則需要從Raster
對象中讀取。
往BufferedImage對象中寫入像素?cái)?shù)據(jù)同樣遵守上面的規(guī)則。代碼如下:
1 int type= image.getType();2 if ( type ==BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB )3 ?? image.getRaster().setDataElements(x, y, width, height, pixels );4 else5 ? ?image.setRGB(x, y, width, height, pixels, 0, width );
讀取圖像可能因?yàn)閳D像文件比較大,需要一定時(shí)間的等待才可以,Java Advance Image
Processor API提供了MediaTracker對象來跟蹤圖像的加載,同步其它操作,使用方法如下:
MediaTracker tracker = new MediaTracker(this); //初始化對象 om/roucheng/tracker.addImage(image_01, 1); // 加入要跟蹤的BufferedImage對象image_001tracker.waitForID(1, 10000) // 等待10秒,讓iamge_01圖像加載
從一個(gè)32位int型數(shù)據(jù)cARGB中讀取圖像RGB顏色值的代碼如下:
1 int alpha = (cARGB 24) 0xff; //透明度通道 g/2 int red = (cARGB 16) 0xff;3 int green = (cARGB 8) 0xff;4 int blue = cARGB 0xff;
將RGB顏色值寫入成一個(gè)INT型數(shù)據(jù)cRGB的代碼如下:
cRGB = (alpha 24) | (red 16) | (green 8) | blue;
創(chuàng)建一個(gè)BufferedImage對象的代碼如下:
BufferedImage image = newBufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB);
一個(gè)完整的源代碼Demo如下:
1 package com.gloomyfish.swing; ?2 ??3 import java.awt.BorderLayout; ?4 import java.awt.Dimension; ?5 import java.awt.Graphics; ?6 import java.awt.Graphics2D; ?7 import java.awt.RenderingHints; ?8 import java.awt.image.BufferedImage; ?9 import java.io.File; 10 import java.io.IOException; 11 ?12 import javax.imageio.ImageIO; 13 import javax.swing.JComponent; 14 import javax.swing.JFrame; 15 ?16 public class PlasmaDemo extends JComponent {
17 ? ?18 ? ? /** ?19 ?? ? *
20 ? ? ?*/ ? 21 ? ? private static final long serialVersionUID = -2236160343614397287L;
22 ? ? private BufferedImage image = null;
23 ? ? private int size = 256; 24 ? ? ? ?25 ? ? public PlasmaDemo() {
26 ? ? ? ? super();
27 ? ? ? ? this.setOpaque(false);
28 ?? ?}
29 ? ? ? ?30 ? ? protected void paintComponent(Graphics g) {
31 ? ? ? ? Graphics2D g2 = (Graphics2D)g;
32 ?? ? ? ?g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
33 ? ? ? ? g2.drawImage(getImage(), 5, 5, image.getWidth(), image.getHeight(), null);
34 ?? ?}
35 ? ? ? ?36 ? ? private BufferedImage getImage() {
37 ? ? ? ? if(image == null) {
38 ? ? ? ? ? ? image = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
39 ? ? ? ? ? ? int[] rgbData = new int[size*size];
40 ?? ? ? ? ? ?generateNoiseImage(rgbData);
41 ? ? ? ? ? ? setRGB(image, 0, 0, size, size, rgbData); 42 ? ? ? ? ? ? File outFile = new File("plasma.jpg"); 43 ? ? ? ? ? ? try { 44 ? ? ? ? ? ? ? ? ImageIO.write(image, "jpg", outFile); 45 ? ? ? ? ? ? } catch (IOException e) { 46 ?? ? ? ? ? ? ? ?e.printStackTrace(); 47 ?? ? ? ? ? ?} 48 ?? ? ? ?}
49 ? ? ? ? return image;
50 ?? ?}
51 ? ? ? ?52 ? ? public void generateNoiseImage(int[] rgbData) {
53 ? ? ? ? int index = 0;
54 ? ? ? ? int a = 255;
55 ? ? ? ? int r = 0;
56 ? ? ? ? int g = 0;
57 ? ? ? ? int b = 0;
58 ? 59 ? ? ? ? for(int row=0; rowsize; row++) {
60 ? ? ? ? ? ? for(int col=0; colsize; col++) {
61 ? ? ? ? ? ? ? ? // set random color value for each pixel ? 62 ? ? ? ? ? ? ? ? r = (int)(128.0 + (128.0 * Math.sin((row + col) / 8.0)));
63 ? ? ? ? ? ? ? ? g = (int)(128.0 + (128.0 * Math.sin((row + col) / 8.0)));
64 ? ? ? ? ? ? ? ? b = (int)(128.0 + (128.0 * Math.sin((row + col) / 8.0)));
65 ? ? ? ? ? ? ? ? ? ?66 ? ? ? ? ? ? ? ? rgbData[index] = ((clamp(a) 0xff) 24) |
67 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ((clamp(r) 0xff) 16) ?|
68 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ((clamp(g) 0xff) 8) ? |
69 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ((clamp(b) 0xff));
70 ? ? ? ? ? ? ? ? index++;
71 ?? ? ? ? ? ?}
72 ?? ? ? ?}
73 ? ? ? ? ? ?74 ?? ?}
75 ? ? ? ?76 ? ? private int clamp(int rgb) {
77 ? ? ? ? if(rgb 255)
78 ? ? ? ? ? ? return 255;
79 ? ? ? ? if(rgb 0)
80 ? ? ? ? ? ? return 0;
81 ? ? ? ? return rgb;
82 ?? ?}
83 ? ?84 ? ? public void setRGB( BufferedImage image, int x, int y, int width, int height, int[] pixels ) {
85 ? ? ? ? int type = image.getType();
86 ? ? ? ? if ( type == BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB )
87 ?? ? ? ? ? ?image.getRaster().setDataElements( x, y, width, height, pixels );
88 ? ? ? ? else ? 89 ? ? ? ? ? ? image.setRGB( x, y, width, height, pixels, 0, width );
90 ?? ?}
91 ? ? ? ?92 ? ? public static void main(String[] args) {
93 ? ? ? ? JFrame frame = new JFrame("Noise Art Panel");
94 ?? ? ? ?frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
95 ? ? ? ? frame.getContentPane().setLayout(new BorderLayout()); m/roucheng/ 98 ? ? ? ? frame.getContentPane().add(new PlasmaDemo(), BorderLayout.CENTER);
99 ? ? ? ? frame.setPreferredSize(new Dimension(400 + 25,450)); ?
100 ?? ? ? ?frame.pack(); ?
101 ? ? ? ? frame.setVisible(true); ?
102 ?? ?} ?
103 } ?
代碼不長,java 圖像處理 關(guān)天setRGB(x,y,rgb)
不會啊,我試了一下圖像是變化的,不過下邊這段代碼沒太懂,為什么i和j要遞增2呢?遞增1的話感覺效果更有趣一些:)
for(i=0;iwidth-1;i+=2)
for(j=0;jheight-1;j+=2)
{ pix=temp.getRGB(i,j);
if(pix!=im.getRGB(i,j))//每個(gè)相素進(jìn)行比較如果不等
{im.setRGB(i,j,pix);//則更新為現(xiàn)在的像素值。
}
}
文章題目:java圖像處理軟件代碼 java圖像處理opencv
本文地址:http://www.dlmjj.cn/article/ddocgoi.html