Download Class Structure – Image Processing Class

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Autostereogram wikipedia , lookup

Portable Network Graphics wikipedia , lookup

Waveform graphics wikipedia , lookup

Anaglyph 3D wikipedia , lookup

Video card wikipedia , lookup

Graphics processing unit wikipedia , lookup

Framebuffer wikipedia , lookup

Edge detection wikipedia , lookup

Molecular graphics wikipedia , lookup

2.5D wikipedia , lookup

Apple II graphics wikipedia , lookup

Tektronix 4010 wikipedia , lookup

Indexed color wikipedia , lookup

Rendering (computer graphics) wikipedia , lookup

Hold-And-Modify wikipedia , lookup

Computer vision wikipedia , lookup

Stereoscopy wikipedia , lookup

Stereo display wikipedia , lookup

BSAVE (bitmap format) wikipedia , lookup

Spatial anti-aliasing wikipedia , lookup

Medical image computing wikipedia , lookup

Image editing wikipedia , lookup

Transcript
Image Processing
Objectives
To present the class Image.
To give some simple transformations with Images
- Loading and Drawing an image.
- Processing an Image.
- Point Transformations
- Geometrical Transformations
Java Classes for Images. [Knudsen, 1999].
5/24/2017
Computer Graphics - Lecture 16
1
Digital Images In Java
Java digital image can be defined as:
rectangle of colored pixels or an array of integers.
Classes for digital images:
1. Image is rectangle of pixels; no image data; load, draw + basic
information.
2. BufferedImage is an array of integers; image data is accessible;
several other classes for image processing in java.awt.image
3. PlanarImage complete API for Image Processing
Image works only gif, jpec and png.
PlanarImage accepts all the graphics formats.
5/24/2017
Computer Graphics - Lecture 16
2
The Class Image
The class Image offers simple methods for:
- get the image sizes. getHeight(this), getWidth(this)
- get the graphics context. getGraphics()
The class Applet gives methods to load an image: getImage ();
The class Graphics has got drawImage().
The class Toolkit provides several methods to work with Image.
getImage(), createImage(), etc.
5/24/2017
Computer Graphics - Lecture 16
3
Loading and Drawing Images
public Image getImage(URL url); // load an image located at url
public Image getImage(URL url, String file); // load an image located in the file at url
public URL getDocumentBase(); // return the current URL (in the class URL)
public boolean drawImage(Image im, int x, int y, ImageObserver obs);
public boolean drawImage(Image im, int x, int y, int w, int h, ImageObserver obs);
public boolean drawImage(Image im, int dx1, int dy1, int dx2, int dy2,
int sx1, int sy1, int sx2, int sy2, ImageObserver ob);
// display the image im (or a part of it) from (x,y).
!! Important !! They all are asynchronous methods
5/24/2017
Computer Graphics - Lecture 16
4
Example: Loading and displaying the Java Duke image
import java.net.URL;
import java.applet.*;
import java.awt.*;
import java.applet.*;
import java.awt.*;
public class L16App1 extends Frame {
private Image im;
public L16App1(String str){
super(str);
im=Toolkit.getDefaultToolkit().getImage("Picture1.gif");
}
public class Image1 extends Applet{
private Image im;
public void init(){
im=getImage(getCodeBase(),"JavaDuke.gif");
}
public void paint(Graphics g){
g.drawImage(im,10,10,this);
}
}
5/24/2017
public void paint(Graphics g){
g.drawImage(im,10,10,this);
}
public static void main(String [] args){
L16App1 fr = new L16App1("Simple Load");
fr.resize(500,500);fr.show();
}
}
Computer Graphics - Lecture 16
5
5/24/2017
Computer Graphics - Lecture 16
6
Loading Images with MediaTracker
The MediaTracker class is to monitor the loading progress of any number of images.
public MediaTracker(Component comp); // creates a new MediaTracker obj
public void addImage(Image im, int id); // add image im with the id number id to mt
public void removeImage(Image im); // remove the image im
public void waitForID(int id) throws InteruptedException; // wait for the image id
public boolean isErrorID(Image im, int id); // detect if an error has encountered
while im is loading
MediaTracker offers a synchronous method to load several images.
Step1. Declare and Create a MediaTracker mt and a varID
Step 2. Create a function waitForImage
5/24/2017
Computer Graphics - Lecture 16
7
private static final Component comp= new Component () {}
private static final MediaTracker mt = new MediaTracker (comp);
private static int varID=0;
public static boolean waitForImage(Image im){
int id; synchronized(comp){id=varID++;}
mt.addImage(im);
try{mt.waitForID(id);}
catch(InterruptedException ie){return false;}
return mt.isErrorID(id));
}
5/24/2017
Computer Graphics - Lecture 16
8
import java.net.URL;
import java.applet.*;
import java.awt.*;
public void init(){
URL codebase=getCodeBase();
im1=getImage(codebase,"Picture2.gif");
waitForImage(im1);
public class Image2 extends Applet{
private static final Component comp=
new Component () {};
private static final MediaTracker mt =
new MediaTracker (comp);
private static int imageID=0;
private Image im1,im2;
im2= getImage(codebase,"JavaDuke.gif");
waitForImage(im2);
}
public void paint(Graphics g){
g.drawImage(im1,10,10,200,200,this);
g.drawRect(10,10,200,200);
public static boolean waitForImage(Image im){
int id; synchronized(comp){id=imageID++;}
g.drawImage(im2,210,10,200,200,this);
mt.addImage(im,id);
g.drawRect(210,10,200,200);
try{mt.waitForID(id);}
}
catch(InterruptedException ie){return false;}
return mt.isErrorID(id);
}
5/24/2017
Computer Graphics - Lecture 16
9
How to construct a gallery
The names of images are known.
- declare an bi-dimensional array of images.
- load all the images in the array.
- draw all of them.
The names of images are NOT known.
- get all the files from the applet’s folder.
- filter those files and keep only the jpeg, gif and png files.
- do exactly as above.
5/24/2017
Computer Graphics - Lecture 16
10
5/24/2017
Computer Graphics - Lecture 16
11
The PixelGrabber Class
PixelGrabber is a class to get (grab) the image array.
Step 0. Declare an array.
int [] pixels = new int [w*h];
Step 1. Construct a PixelGrabber object for the myImage image.
PixelGrabber pg=new PixelGrabber(myImage,0,0,w,h,pixels,0,w);
Step 2. Grab the pixel from the image to the array.
try{pg.grabPixels();}catch(InterruptedException e){}
Create a new image from an array of pixels
createImage(new MemoryImageSource(w, h, pixelsNew,0,w))
5/24/2017
Computer Graphics - Lecture 16
12
Packing and Unpacking Colors
The A, R, G, B values of a pixel are stored into an element of the array:
alpha
red
green
24
16
blue
8
0
Unpack the color c to it’s a,r,g,b components
a=(c & 0xff000000) >> 24;
r=(c & 0xff0000) >> 16;
g=(c & 0xff00) >> 8;
b=(c & 0xff);
Pack the components a,r,g,b back into the color c
c=b + (g<<8) + (r<<16) + (a<<24);
5/24/2017
Computer Graphics - Lecture 16
13
The Structure of a PT method
Image invertImage(Image myImage){
int w=myImage.getWidth(this),h=myImage.getHeight(this);
int pixels [] = new int[w*h]; int pixelsNew [] = new int[w*h];
PixelGrabber pg=new pixelGrabber(myImage,0,0,w,h,pixels,0,w);
try{pg.grabPixels();}catch(InterruptedException e){ }
for(int x=0;x<imageW*imageH;x++){
int r,g,b,a,c=pixels [x];
a=(c & 0xff000000) >> 24; r=(c & 0xff0000) >> 16;
g=(c & 0xff00) >> 8; b=(c & 0xff);
int r1,g1,b1,a1,c1;
a1=a; r1=255-r; g1=255-g; b1=255-b; // this is the only code to be changed
c1=b1+(g1<<8)+(r1<<16)+(a1<<24);
pixelsNew[x]=c1;
}
return createImage(new MemoryImageSource(w,h,pixelsNew,0,w));
}
5/24/2017
Computer Graphics - Lecture 16
14
The Structure of a GT method
Image mirrorImage(Image myImage){
int w=myImage.getWidth(this),h=myImage.getHeight(this);
int pixels [] = new int[w*h]; int pixelsNew [] = new int[w*h];
int w1=w,h1=h;
PixelGrabber pg=new pixelGrabber(myImage,0,0,w,h,pixels,0,w);
try{pg.grabPixels();}catch(InterruptedException e){ }
for(int x=0;x<imageW*imageH;x++){
int row=x/w, col=x%w;
int row1= w-1-row, col1=col; // this is the only code to be changed
int x1=row1*w1+col1;
pixelsNew[x1]=pixels[x];
}
return createImage(new MemoryImageSource(w1,h1,pixelsNew,0,w1));
}
5/24/2017
Computer Graphics - Lecture 16
15
Image Enhancement
Why:
- correct some simple errors, noises, …
- accentuation and sharpening some image features (edge, boundary)
- emphasize the contrast of some image regions
Classification:
Point
Spatial
Geometrical
Contrast Stretching
Noise Smoothing
Mirroring
Noise Clipping
Median Filtering
Rotations
Window slicing
Low, Band, High Filtering …
Histogram modelling
Zooming
5/24/2017
Computer Graphics - Lecture 16
16
Histograms
Consider an image im: the sizes w, h and 256 gray levels.
For a channel R, G, B
Number of pixels with the gray level l is nr[l], l=0,1,…,255.
Histogram is a graphics representing (l,nr[l]), l=0,1,…,255.
Normalized Histograms: (l,fr[l]), l=0,1,…,255 where
fr[l]=nr[l]/(imH*imW) frequency of l.
Histograms gives statistical information about.
- How the colours are distributed.
- The most used colour … platforms …
- The mean value, the median value, the variance, …
5/24/2017
Computer Graphics - Lecture 16
17
5/24/2017
Computer Graphics - Lecture 16
18
5/24/2017
Computer Graphics - Lecture 16
19
Histogram for Red
public int [] histoRedImage(Image myImage){
int w=myImage.getWidth(this);int h=myImage.getHeight(this);
int pixels [] = new int[w*h];
PixelGrabber pg=new PixelGrabber(myImage,0,0,w,h,pixels,0,w);
try{pg.grabPixels();}catch(InterruptedException e){System.out.println("Grabber error");}
int [] nrPixelsRed = new int [256];
for(int x=0;x<256;x++)nrPixelsRed[x]=0;
for(int x=0;x<imageW*imageH;x++){
int c1=pixelsMyImage[x];
int r1=(c1 & 0xff0000) >> 16;
nrPixelsRed[r1]++;
}
return nrPixelsRed;
}
public void drawHisto(Graphics g,int x0,int y0,int [] pixels){
g.drawLine(x0,y0,x0+255,y0); g.drawLine(x0,y0,x0,y0+500);
for(int i=0;i<255;i++) g.drawLine(x0+i,y0+pixels[i],x0+(i+1),y0+pixels[i+1]);
}
5/24/2017
Computer Graphics - Lecture 16
20
References:
J. Knudsen, Java 2D, O’Reilly Publisher, 2000.
Java Black Book.
Problems to solve.
Write a Java applet/application to create a gallery of images.
Write a Java applet/application to compare two images.
5/24/2017
Computer Graphics - Lecture 16
21