cs 335 graphics and multimedia - university of kentucky
TRANSCRIPT
Image ManipulationIndependent pixels:
image subtractionimage averaginggrey level mappingthresholding
Neighborhoods of pixels:filtering, convolution, etc.
Geometric operations on pixels:rotation, scale, warp
Manipulating Images in Java
Image class:provides simple file/network readdifficult to access individual pixelseasy to make into an icon
BufferedImage class:simple file/network readeasy access to pixelscan get graphics context
Treating Pixels Independently
Image Arithmetic:Change the values of pixels based on a rule which uses information from that pixel onlyAddition/averaging: average multiple images over timeSubtraction: detect changes over timeNegative: invert the color map
Grey Level Mapping
Remap the intensity distribution of pixelsEnhances contrast between features and structures
Original image grey levels
Target imagegrey levels
Grey Level Mapping
Mapping curve can be arbitraryExample: first interval is compressed, second interval is stretched
Original image grey levels
Target imagegrey levels
Grey Level Mapping
Example: monotonically increasing curve (not line)
Original image grey levels
Target imagegrey levels
Grey Level Mapping
Example: non-monotonically increasing curve
Original image grey levels
Target imagegrey levels
Thresholding
Pixel modification based on a specified threshold criterion:
<
=otherwisey
threshold oldpixel ifx newvalue
Goal: suppress values below threshold, enhance values above
Image Histograms
Histogram: frequency distribution of grey levels or colors in that imageCan be a strong indicator for enhancement schemes: contrast enhancement, thresholdingHistograms are not unique: many images can produce the same histogramHistogram equalization: enhancement based on the histogram
The Normalized Histogram
Divide each “bin” by the total pixel countBin total becomes normalized to 1
Using the Histogram for Thresholding
Threshold value can be calculated from the shape of the histogram:
0 255
Localized Histograms
Can compute histogram for a Region of Interest (ROI)Compute total number of pixels in ROICompute pixel intensity distribution within ROICan apply enhancements more locally
Histogram Equalization (Flattening)Evenly distribute intensity values across dynamic rangeResulting histogram is flatCan be a complex grey level mapping: is usually easier to specify as a histogram operation
/************************************************************
Sample: Program to load a JPEG image and display it in a JFRAME
************************************************************/
import java.awt.*;import java.awt.event.*;import java.awt.image.*;import java.io.*;import javax.swing.*;import javax.swing.event.*;import com.sun.image.codec.jpeg.*;
public class MyImageViewer extends JFrame {
// Instance variablesprivate BufferedImage image; // the imageprivate MyImageObj view; // a component for displayprivate JLabel infoLabel; // a label for the GUIprivate int x, y;private boolean firstdrag=true;
// The Constructor for this extension of the JFrame classpublic MyImageViewer (String filename) throws IOException {
super(filename);
// Load an image from a fileimage = readImage(filename);
// Create components to display image and pixel informationview = new MyImageObj(image);infoLabel = new JLabel();
infoLabel.addMouseListener(new MouseAdapter() {
public void mousePressed (MouseEvent event) {view.repaint();
}}
);
// Listen for mouse events to show (x,y) coordinates on infoLabelview.addMouseMotionListener(
new MouseMotionAdapter() {public void mouseMoved(MouseEvent event) {
infoLabel.setText((event.getPoint()).toString());}public void mouseDragged(MouseEvent event) {
Graphics g = view.getImage().getGraphics();g.setColor (Color.white);if (firstdrag) {
x = event.getX(); y = event.getY();firstdrag = false;
}else {g.drawLine (x, y, x=event.getX(),y=event.getY());
}view.repaint();
}}
);view.addMouseListener(
new MouseAdapter() {public void mouseReleased(MouseEvent event) {
firstdrag = true;}
});
// Build the JPanel which holds (for now) the infoLabelJPanel controlPane = new JPanel();controlPane.add(infoLabel);
// Add the JPanel and the image data component to the JFramegetContentPane().add(view, BorderLayout.CENTER);getContentPane().add(controlPane, BorderLayout.SOUTH);
}
public BufferedImage readImage (String file) throws IOException {
try {FileInputStream fin = new FileInputStream(file);JPEGImageDecoder decoder =
JPEGCodec.createJPEGDecoder(fin);image = decoder.decodeAsBufferedImage();fin.close();
}catch (IOException e) {}return image;
}
public static void main(String[] argv) {
// look for a command-line filenameif (argv.length > 0) {
try {JFrame frame = new MyImageViewer(argv[0]);frame.pack();frame.setVisible(true);frame.addWindowListener (
new WindowAdapter () {public void windowClosing ( WindowEvent e) {
System.exit(0);}
});
}catch (Exception e) {
System.err.println(e);System.exit(1);
}}// no command-line filename was providedelse {
System.err.println("usage: java MyImageViewer <imagefile>");System.exit(1);
}}
/*****************************************************************This is a helper object (could be described its own file) thatextends JLabel so that it can hold a BufferedImage
**************************************************************/
public class MyImageObj extends JLabel {
// instance variable to hold the buffered imageprivate BufferedImage image;
public MyImageObj(BufferedImage img) {setImage(img);setPreferredSize(new Dimension(image.getWidth(),
image.getHeight()));}
public void setImage(BufferedImage img) {image = img;
}
public BufferedImage getImage() {return image;
}
public void paintComponent(Graphics g) {g.drawImage(image, 0, 0, this);
}}}