📄 picture2.java
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package transformace;import java.awt.Color;import java.awt.Graphics;import java.awt.GraphicsConfiguration;import java.awt.GraphicsDevice;import java.awt.GraphicsEnvironment;import java.awt.HeadlessException;import java.awt.Image;import java.awt.Toolkit;import java.awt.Transparency;import java.awt.image.BufferedImage;import java.awt.image.ColorModel;import java.awt.image.MemoryImageSource;import javax.swing.ImageIcon;/** * * @author xsobot10 */public class Picture2 { Image image; BufferedImage bImage; int height, width; //konstruktor public Picture2(String path) { this.image = new ImageIcon(path).getImage(); this.bImage = toBufferedImage(image); } //nova metoda ... vygooglena public static BufferedImage toBufferedImage(Image image) { if (image instanceof BufferedImage) { return (BufferedImage) image; } image = new ImageIcon(image).getImage(); BufferedImage bimage = null; GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); try { int transparency = Transparency.OPAQUE; GraphicsDevice gs = ge.getDefaultScreenDevice(); GraphicsConfiguration gc = gs.getDefaultConfiguration(); bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency); } catch (HeadlessException e) { } if (bimage == null) { int type = BufferedImage.TYPE_INT_RGB; bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type); } Graphics g = bimage.createGraphics(); g.drawImage(image, 0, 0, null); g.dispose(); return bimage; } //metoda na ziskani slozek z RGB public double[][][] getRGB() { ColorModel colormodel = bImage.getColorModel(); // zistkam v jakym je modelu, cili RGB this.height = bImage.getHeight(); // zjistim vysku obreazku this.width = bImage.getWidth(); // zjistim sirku obrazku double[][][] rgb = new double[3][this.width][this.height]; //pole pro slozky RGB // cyklus ktery vytahne jednotlivy slozky RGB for (int i = 0; i < this.width; i++) { for (int j = 0; j < this.height; j++) { rgb[0][i][j] = colormodel.getRed(bImage.getRGB(i, j)); rgb[1][i][j] = colormodel.getGreen(bImage.getRGB(i, j)); rgb[2][i][j] = colormodel.getBlue(bImage.getRGB(i, j)); } } return (rgb); // vracim pole rgb se hodotami slozek RGB } //********************************************************* //metoda na prevod z RGB do YUV public double[][][] rgbTOyuv(double[][][] rgb) { double[][][] yuv = new double[3][this.width][this.height]; for (int j = 0; j < height; j++) { for (int i = 0; i < width; i++) { //yuv[0][i][j] = ((0.299 * rgb[0][i][j]) + (0.587 * rgb[1][i][j]) + (0.114 * rgb[2][i][j])); //yuv[1][i][j] = ((rgb[2][i][j] - yuv[0][i][j]) * 0.492f); //yuv[2][i][j] = ((rgb[0][i][j] - yuv[0][i][j]) * 0.877f); yuv[0][i][j] = 0.299*rgb[0][i][j] + 0.587*rgb[1][i][j] + 0.114*rgb[2][i][j]; yuv[1][i][j] = (-0.147)*rgb[0][i][j] + (-0.289)*rgb[1][i][j] + 0.436*rgb[2][i][j]; yuv[2][i][j] = 0.615*rgb[0][i][j] + (-0.515)*rgb[1][i][j] + (-0.100)*rgb[2][i][j]; } } return (yuv); } //********************************************************* //********************************************************* //metoda na prevod z RGB do YUV public double[][][] rgbTOy(double[][][] rgb) { double[][][] yuv = new double[3][this.width][this.height]; for (int j = 0; j < height; j++) { for (int i = 0; i < width; i++) { yuv[0][i][j] = 0.299*rgb[0][i][j] + 0.587*rgb[1][i][j] + 0.114*rgb[2][i][j]; yuv[1][i][j] = 0; yuv[2][i][j] = 0; } } return (yuv); } //********************************************************* //********************************************************* //metoda na prevod z YUV do RGB public double[][][] yuvTOrgb(double[][][] yuv) { double[][][] rgb = new double[3][this.width][this.height]; for (int j = 0; j < height; j++) { for (int i = 0; i < width; i++) { //rgb[0][i][j] = ((yuv[2][i][j]+(0.877*yuv[0][i][j]))/0.877); //R //rgb[2][i][j] = ((yuv[1][i][j]+(0.492*yuv[0][i][j]))/0.492); //B //rgb[1][i][j] = ((yuv[0][i][j]-(0.299*rgb[0][i][j])-(0.114*rgb[2][i][j]))/0.587); rgb[0][i][j] = yuv[0][i][j] + 1.137*yuv[2][i][j]; rgb[1][i][j] = yuv[0][i][j] + (-0.397)*yuv[1][i][j] + (-0.580)*yuv[2][i][j]; rgb[2][i][j] = yuv[0][i][j] + 2.034*yuv[1][i][j]; } } return (rgb); } //********************************************************* //********************************************************* // jednorozmerna DPCM, vraci pouze Y slozku ktera byla postoupena DPCM public double[][][] jDPCM(double[][][] yuv) { double[][][] yuv_a = new double[3][this.width][this.height]; //pro zdroj double[][][] yuv_b = new double[3][this.width][this.height]; //pro cil int predictorA = 0; int predictorB = 0; int vstupA, vstupB; for (int i = 0; i < height; i++) { predictorA=0; predictorB=0; for (int j = 0; j < width; j++) { // vysilaci strana vstupA = (int)yuv[0][i][j]; yuv_a[0][i][j] = (int)yuv[0][i][j] - predictorA; predictorA = vstupA; // prijimaci strana vstupB = (int)yuv_a[0][i][j]; yuv_b[0][i][j] = vstupB + predictorB; predictorB = (int)yuv_b[0][i][j]; } } return (yuv_b); } //********************************************************* //********************************************************* //vytvoreni noveho obrazku z YUV do RGB public Image createImage(double[][][] rgb) { Image newImage = null; Color color; int pom = 0; //nepotrebuji protoze ji mam defaultne 256x256 //int width = (int) rgb[0].length; //int height = (int) rgb[0][0].length; int[] pixels = new int[width * height]; // [ R G B 0] - po 8 bitech for (int j = 0; j < height; j++) { for (int i = 0; i < width; i++) { //konstruktor tridy Color - nastavuju hodnoty z pozic rgb[][][] color = new Color((int) rgb[0][i][j], (int) rgb[1][i][j], (int) rgb[2][i][j]); pixels[pom] = color.getRGB(); // do pole pixelu ukladam barvy pom++; } } Toolkit tk = Toolkit.getDefaultToolkit(); newImage = tk.createImage(new MemoryImageSource(width, height, pixels, 0, width)); return (newImage); } //********************************************************* }//void rgb2yuv(int r,int g, int b, int[] yuv,) {// int y = (int)(0.299 * r + 0.587 * g + 0.114 * b);// int u = (int)((b - y) * 0.492f);// int v = (int)((r - y) * 0.877f);// yuv[0]= y;// yuv[1]= u;// yuv[2]= v;// RGB to YUV// (Y) ( 0.299 0.587 0.114) (R)// (U) = (-0.147 -0.289 0.436) * (G)// (V) ( 0.615 -0.515 -0.100) (B)// YUV to RGB// (R) (1 0 1.137) (Y)// (G) = (1 -0.397 -0.580) * (U)// (B) (1 2.034 0 ) (V)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -