12.07.2015 Views

Digital Image Processing (using Java)

Digital Image Processing (using Java)

Digital Image Processing (using Java)

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

• java LogPolar matthead.png 256 256• java QuantisationSimulator matthead.png(p.29)- 3 -


(p.30)- 4 -


(p.31)HSICalc.java: RGB → HIS(0~255) (0~1)LISTING 3.1 A program to convert RGB colours into values of hue, saturation andintensity. A conversion method from <strong>Java</strong>’s Color class is used.import java.awt.Color;import java.text.DecimalFormat;public class HSICalc {public static void main(String[] argv) {if (argv.length > 2) {int[] rgb = new int[3];for (int i = 0; i < 3; ++i)rgb[i] = Integer.parseInt(argv[i]);float[] values = Color.RGBtoHSB(rgb[0], rgb[1], rgb[2], null);String[] labels = { "H=", "S=", "I=" };DecimalFormat floatValue = new DecimalFormat("0.000");for (int i = 0; i < 3; ++i)System.out.println(labels[i] + floatValue.format(values[i]));}}}else {System.err.println("usage: java HSICalc ");System.exit(1);}- 5 -


(p.34)(p.36)• java LimitsLISTING 3.2 Program to print limits for the primitive types. The limits are defined asstatic constants in the standard <strong>Java</strong> ‘wrapper’ classes Byte, Short, Integer, Floatand Double.public class Limits {public static void main(String[] argv) {java.io.PrintStream s = System.out;s.println("Min byte value = " + Byte.MIN_VALUE);s.println("Max byte value = " + Byte.MAX_VALUE);s.println("Min short value = " + Short.MIN_VALUE);s.println("Max short value = " + Short.MAX_VALUE);s.println("Min int value = " + Integer.MIN_VALUE);s.println("Max int value = " + Integer.MAX_VALUE);s.println("Min float value = " + Float.MIN_VALUE);s.println("Max float value = " + Float.MAX_VALUE);s.println("Min double value = " + Double.MIN_VALUE);s.println("Max double value = " + Double.MAX_VALUE);}}- 6 -


(p.37)(p.37)- 7 -


(p.38)(p.39)- 8 -


(p.40)- 9 -


(p.41)(p.42)LISTING 3.3 Example of polymorphism.public class Mean {public static void print1 (Byte<strong>Image</strong> image){System.out.println (“mean value is “ + image.getMeanValue());}public static void print2 (Base<strong>Image</strong> image){System.out.println (“mean value is “ + image.getMeanValue());}public static void print3 (BaseArray array){System.out.println (“mean value is “ + array.getMeanValue());}}- 10 -


(p.79)• usage: java convert infile outfileLISTING 5.6 A simple image format conversion program.public class Convert {public static void main(String[] argv) {if (argv.length > 1) {try {<strong>Image</strong>Decoder input = <strong>Image</strong>File.create<strong>Image</strong>Decoder(argv[0]);Buffered<strong>Image</strong> image = input.decodeAsBuffered<strong>Image</strong>();<strong>Image</strong>Encoder output = <strong>Image</strong>File.create<strong>Image</strong>Encoder(argv[1]);output.encode(image);System.exit(0);}catch (Exception e) {System.err.println(e);System.exit(1);}}else {System.err.println("usage: java Convert ");System.exit(1);}}}(p.85)• usage: java Display imagefileLISTING 5.9 A simple image display application <strong>using</strong> Swing components.import java.awt.*;import java.awt.image.*;import javax.swing.*;import com.pearsoneduc.ip.io.*;public class Display extends JFrame{public Display (String filename){super (filename);<strong>Image</strong>Decoder input = <strong>Image</strong>File.create<strong>Image</strong>Decoder (filename);Buffered<strong>Image</strong> image = input.decodeAsBuffered<strong>Image</strong>();- 11 -


}Jlabel view = new Jlabel (new <strong>Image</strong>Icon (image));getContentPane().add(view);addWindowListener (new WindowEvent event){public void windowClosing (WindowEvent event){System.exit(0);}});}public static void main (String[] argv){if (argv.length > 0){try {JFrame frame = new Display (argv[0]);frame.pack();frame.setVisible (true);}catch (Exception e){System.err.println (e);System.exit (1);}}else {System.err.println (“usage: java Display imagefile”);System.exit (1);}}(p.86)• usage: java <strong>Image</strong>Viewer matthead.pngLISTING 5.10 A Swing component to display images.import java.awt.*;import java.awt.image.*;import javax.swing.*;public class <strong>Image</strong>View extends JLabel implements Scrollable{private Buffered<strong>Image</strong> image; //image to be displayedprivate Dimension viewSize; //size of view, if in a JScrollPane- 12 -


public <strong>Image</strong>View (Buffered<strong>Image</strong> img){image = img;int width = Math.min (256, image.getWidth());int height = Math.min (256, image.getHeight());viewSize = new Dimension (width, height);setPreferredSize (new Dimension (image.getWidth(), image.getHeight()));}public void paintComponent (Graphics g){}g.draw<strong>Image</strong> (image, 0, 0, this);public void setViewSize (Dimension newSize){}viewSize.setSize (newSize);public Dimension getPreferredScrollableViewportSize(){}return viewSize;public int getScrollableUnitIncrement (Rectangle rect, int orient, int dir){}return 1;public int getScrollableBlockIncrement (Rectangle rect, int orient, int dir){if (orient == SwingConstants.HORIZONTAL)return image.getWidth() / 10;elsereturn image.getHeight() / 10;}public boolean getScrollableTracksViewportWidth(){}return false;}public boolean getScrollableTracksViewportHeight(){}return false;- 13 -


(p.88)halftoning: gray → binary1. patterning-replacing each pixel by a pattern taken from a ‘binary font’.(e.g.: fig 5.10)2. Dithering:• <strong>Java</strong> Dither matthead.png3. Error diffusion:<strong>Java</strong> ErrorDiffustion matthead.png- 14 -


(p.92)ALGORITHM 5.1 Dithering BY Floyd-Steinberg error diffusion.threshold = (black + white) /2for all x and y doif ƒ (x, y) < threshold theng (x, y) = blackε = ƒ (x, y) – blackelseg (x, y) = whiteε = ƒ (x, y) – whiteend ifƒ (x+1, y) = ƒ (x+1,y) + 7ε/16ƒ (x-1, y+1) = ƒ (x-1, y+1) + 3ε/16ƒ (x, y+1) = ƒ (x,y+1) + 5ε/16ƒ (x+1, y+1) = ƒ (x+1, y+1) + ε/16end for(p.93)LISTING 5.12 <strong>Java</strong> implementation of Algorithm 5.1public static Buffered<strong>Image</strong> errorDiffusion (Buffered<strong>Image</strong> image) {// Create a binary output image (0=black, 1=white)int w = image.getWidth()-1;int h = image.getHeight()-1;output<strong>Image</strong> = new Buffered<strong>Image</strong>(w, h, buffered<strong>Image</strong>.TYPE_BYTE_BINARY);- 15 -


Copy input image because error diffusion modifies itWritableRaster input = image.copyData(null);WritableRaster output = output<strong>Image</strong>.getRaster();final int threshold = 128;int value, error;for (int y = 1; y < h; ++y)for (int x = 1; x < w; ++x) {// Threshold value and compute errorvalue = input.getSample(x, y, 0);if (value < threshold) {output.setSample(x, y, 0, 0); //set to blackerror = value;}else {output.setSample(x, y, 0, 1); //set to whiteerror = value - 255;}// Disperse error to pixels that are ahead of usvalue = input.getSample(x+1, y, 0);input.setSample(x+1, y, 0, clamp(value + 0.4375f * error));value = input.getSample(x-1, y+1, 0);input.setSample(x-1, y+1, 0, clamp(value + 0.1875f * error));value = input.getSample(x, y+1, 0);input.setSample(x, y+1, 0, clamp(value + 0.3125f * error));value = input.getSample(x+1, y+1, 0);input.setSample(x+1, y+1, 0, clamp(value + 0.0625f * error));}}return output<strong>Image</strong>;// Rounds a float to the nearest int between 0 and 255- 16 -


public static int clamp(float value) {}return Math.min(Math.max(Math.round(value), 0), 255);ROI (region of interest)<strong>Java</strong> MeanROI matthead.pngLISTING 5.13 Example of <strong>using</strong> a ROI in the calculation of mean grey level.public double meanValue(Buffered<strong>Image</strong> img) {Raster raster = img.getRaster();double sum = 0.0;for (int y = 0; y < img.getHeight(); ++y)for (int x = 0; x < img.getWidth(); ++x)sum += raster.getSample(x, y, 0);return sum / (img.getWidth()*img.getHeight());}public double meanValue(Buffered<strong>Image</strong> img, Rectangle region) {return meanValue(img.getSubimage(region.x, region.y,region.width, region.height));}(p.97)LISTING 5.14 <strong>Java</strong> code to enlarge an image by pixel replication.public static Buffered<strong>Image</strong> enlarge (Buffered<strong>Image</strong> image, int n ){int w = n*image.getWidth();int h = n*image.getHeight();Buffered<strong>Image</strong> enlarged<strong>Image</strong> = new Buffered<strong>Image</strong>(w, h, image.getType());for (int y = 0; y < h; ++y)for (int x=0; x < w; ++x)enlarged<strong>Image</strong>.setRGB(x, y, image.getRGB(x/n, y/n));}return enlarged<strong>Image</strong>;LISTING 5.15 <strong>Java</strong> code to shrink an image by skipping pixels.- 17 -


public static Buffered<strong>Image</strong> shrink (Buffered<strong>Image</strong> image, int n ){int w = image.getWidth() / n;int h = image.getHeight() / n;Buffered<strong>Image</strong> shrunkd<strong>Image</strong> = new Buffered<strong>Image</strong>(w, h, image.getType());for (int y = 0; y


LISTING 5.17 <strong>Java</strong> code to average a set of images.public static Buffered<strong>Image</strong> average (Buffered<strong>Image</strong>[] imgArray ){int n = imgArray.length;int w = imgArray[0].getWidth(); //assume that they all haveint h = imgArray[0].getHeight(); //the same dimensionsBuffered<strong>Image</strong> average =new Buffered<strong>Image</strong>(w, h, Buffered<strong>Image</strong>.TYPE_BYTE_GRAY);}for (int y = 0; y < h; ++y)for (int x=0; x < w; ++x)float sum = 0.0f;for (int I = 0; I < n; ++i)sum += imgArray[i].getRaster().getSample(x, y, 0);raster.setSample (x, y, Math.round (sum/n));}return average;(p.102)Exercise 5.71. Write a <strong>Java</strong> program that will(a) Display an image(b) Allow the user to select a region of interest (ROI)(c) Extract this ROI from the image(d) Write the ROI to a user-specified file(p.111)- 19 -


(p.112)IntervalTimer timer = new IntervalTimer();Timer.start();//some code to be timedSystem.out.println(timer.elapsed()); //doesn’t stop the clock//more code to be timedSystem.out.println(timer.stop()); //stops clockMapTest1.javaimport java.util.Random;public class MapTest1 {public static void randomFill(short[] array) {Random random = new Random();for (int i = 0; i < array.length; ++i)array[i] = (short) random.nextInt(256);}public static void main(String[] argv) {int n = 512;if (argv.length > 0)n = Integer.parseInt(argv[0]);// Create image and fill it with random values- 20 -


int numPixels = n*n;short[] image = new short[numPixels];randomFill(image);// Perform the mapping directlyIntervalTimer timer = new IntervalTimer();timer.start();for (int i = 0; i < numPixels; ++i)image[i] = (short) Math.round(Math.sqrt(image[i]));System.out.println("direct calculation: " + timer.stop() + " sec");// Perform the mapping with a lookup tablerandomFill(image);timer.start();short[] table = new short[256];for (int i = 0; i < 256; ++i)table[i] = (short) Math.round(Math.sqrt(i));for (int i = 0; i < numPixels; ++i)image[i] = table[image[i]];System.out.println("lookup table: " + timer.stop() + " sec");}System.exit(0);}• <strong>Java</strong> MapTest1- 21 -


(p.113)- 22 -


public LookupOp(LookupTable table, RenderingHints hints)ex: byte[] table = new byte[256];for (int I = 0; I < 256; ++i)table[i] = (byte) (255-i);ByteLookupTable invertTable = new ByteLookupTable(0, table);LookupOp invertOp = new LookupOp(invertTable, null);Buffered<strong>Image</strong> inverted<strong>Image</strong> = invertOp.filter(image, null);(p.116)LISTING 6.3 A <strong>Java</strong> class to perform mapping of grey levels in an image.package com.pearsoneduc.ip.op;import java.awt.image.*;public abstract class GreyMapOp implements Buffered<strong>Image</strong>Op {protected byte[] table = new byte[256];public int getTableEntry(int i) {if (table[i] < 0)return 256 + (int) table[i];elsereturn (int) table[i];}protected void setTableEntry(int i, int value) {if (value < 0)table[i] = (byte) 0;else if (value > 255)table[i] = (byte) 255;elsetable[i] = (byte) value;}public void computeMapping() {}computeMapping(0, 255);- 23 -


public abstract void computeMapping(int low, int high);public Buffered<strong>Image</strong> filter(Buffered<strong>Image</strong> src, Buffered<strong>Image</strong> dest) {check<strong>Image</strong>(src);if (dest == null)dest = createCompatibleDest<strong>Image</strong>(src, null);LookupOp operation = new LookupOp(new ByteLookupTable(0, table), null);operation.filter(src, dest);return dest;}}(p.117)LISTING 6.4 A subclass of GreyMapOp that applies a linear grey level mappingfunction to an image.public class LinearOp extends GreyMapOp {public LinearOp() {}computeMapping();public LinearOp(int low, int high) {}computeMapping(low, high);public void computeMapping(int low, int high) {if (low < 0 || high > 255 || low >= high)throw new java.awt.image.ImagingOpException("invalid mapping limits");float scaling = 255.0f / (high - low);for (int i = 0; i < 256; ++i)setTableEntry(i, Math.round(scaling*(i - low)));}}- 24 -


GreyMap.java• java GreyMap in.jpg out.jpg linear• java GreyMap in. jpg out.jpg log 10 220//logarithmic mapping of [10,220] onto [0,255]// lin: linearinv: inverted linearsq: square-rootexp: exponential• java GreyMapTool matthead.pngGrey MapTool.javaimport java.awt.FlowLayout;import java.awt.image.Buffered<strong>Image</strong>;import java.io.IOException;import java.util.*;import javax.swing.*;public class GreyMapTool extends JFrame {public GreyMapTool(String filename)throws IOException, <strong>Image</strong>DecoderException, HistogramException {// Load image from file and create display componentsuper("GreyMapTool: " + filename);<strong>Image</strong>Decoder input = <strong>Image</strong>File.create<strong>Image</strong>Decoder(filename);Buffered<strong>Image</strong> image = input.decodeAsBuffered<strong>Image</strong>();LinearOp op = new LinearOp();<strong>Image</strong>View imageView = new <strong>Image</strong>View(image, op);// Create and store a set of grey level mapping operationsHashtable ops = new Hashtable();ops.put("linear", op);ops.put("square-root", new SquareRootOp());ops.put("logarithmic", new LogOp());ops.put("exponential", new ExpOp());ops.put("inverted", new InvertOp());ops.put("thresholded", new ThresholdOp(128));ops.put("equalised", new EqualiseOp(new Histogram(image)));// Create labels for the operations- 25 -


Vector names = new Vector();names.addElement("linear");names.addElement("square-root");names.addElement("logarithmic");names.addElement("exponential");names.addElement("inverted");names.addElement("thresholded");names.addElement("equalised");// Add a control panel and a scrolling image display to the frameJPanel pane = new JPanel();pane.setLayout(new FlowLayout());pane.add(new GreyMapPanel(imageView, ops, names));pane.add(new JScrollPane(imageView));setContentPane(pane);addWindowListener(new WindowMonitor());}public static void main(String[] argv) {if (argv.length > 0) {try {JFrame frame = new GreyMapTool(argv[0]);frame.pack();frame.setVisible(true);}catch (Exception e) {System.err.println(e);System.exit(1);}}else {System.err.println("usage: java GreyMapTool ");System.exit(1);}}}- 26 -


(p.119)ALGORITHM 6.3 Calculation of an image histogram.Create an array histogram with 2 b elementsfor all grey levels, i , dohistogram[i ] = 0;end forfor all pixel coordinates, x and y, doIncrement histogram[ƒ (x,y)] by 1end for(p.123)- 27 -


(p.124)HistogramTool.javaimport java.awt.*;import java.awt.event.*;import java.awt.image.*;import java.io.*;import javax.swing.*;import javax.swing.border.*;public class HistogramTool extends JFrame implements ActionListener {private Histogram histogram;// histogram data to be displayedprivate HistogramView[] view; // plot of the histogramprivate HistogramInfoPane infoPane; // displays value and frequencyprivate JPanel mainPane;// contains histogram and info panelprivate JMenu menu;// input/output menuprivate JFileChooser fileChooser = // handles selection of filesnew JFileChooser(System.getProperty("user.dir"));public HistogramTool(Histogram theHistogram, String description) {super(description);// labels the frame// Create components to display histogram and informationhistogram = theHistogram;infoPane = new HistogramInfoPane(histogram);mainPane = new JPanel(new BorderLayout());if (histogram.getNumBands() == 3)createMultipleViews(); // three views (R, G, B) in a tabbed paneelsecreateSingleView();mainPane.add(infoPane, BorderLayout.SOUTH);setContentPane(mainPane);// Add a menu bar to support image input and histogram outputJMenuBar menuBar = new JMenuBar();menuBar.setBorder(new BevelBorder(BevelBorder.RAISED));createFileMenu();- 28 -


menuBar.add(menu);setJMenuBar(menuBar);addWindowListener(new WindowMonitor());}// Creates a single HistogramView object to display a// greyscale histogram and adds it to the GUIpublic void createSingleView() {view = new HistogramView[1];view[0] = new HistogramView(histogram, infoPane);mainPane.add(view[0], BorderLayout.CENTER);}// Creates three HistogramView objects for the red, green// and blue bands of a colour histogram, places these in a// tabbed pane and adds the tabbed pane to the GUIpublic void createMultipleViews() {view = new HistogramView[3];Color[] bandColor = { Color.red, Color.green, Color.blue };String[] tabLabel = { "Red", "Green", "Blue" };JTabbedPane views = new JTabbedPane(JTabbedPane.BOTTOM);for (int i = 0; i < 3; ++i) {view[i] = new HistogramView(histogram, i, infoPane);view[i].setColor(bandColor[i]);views.add(tabLabel[i], view[i]);}mainPane.add(views, BorderLayout.CENTER);}// Creates a menu to support image input, histogram output// and termination of the applicationpublic void createFileMenu() {menu = new JMenu("File");menu.setMnemonic('F');String[] itemName = { "Load image", "Save histogram", "Exit" };- 29 -


}char[] shortcut = { 'L', 'S', 'X' };for (int i = 0; i < 3; ++i) {JMenuItem item = new JMenuItem(itemName[i], shortcut[i]);item.addActionListener(this);menu.add(item);}// Handles Action events triggered by menu selectionspublic void actionPerformed(ActionEvent event) {String command = event.getActionCommand();if (command.startsWith("Load")) {load<strong>Image</strong>();repaint();}else if (command.startsWith("Save")) {saveHistogram();repaint();}else if (command.equals("Exit")) {setVisible(false);dispose();System.exit(0);}}// Loads a new image, computes its histogram and updates the GUIpublic void load<strong>Image</strong>() {fileChooser.setDialogTitle("Load image");if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {// Load image and compute its histogramtry {File file = fileChooser.getSelectedFile();<strong>Image</strong>Decoder input =<strong>Image</strong>File.create<strong>Image</strong>Decoder(file.getAbsolutePath());- 30 -


Buffered<strong>Image</strong> image = input.decodeAsBuffered<strong>Image</strong>();histogram.computeHistogram(image);setTitle(file.getName());}catch (FileNotFoundException e) {error("File not found.");return;}catch (<strong>Image</strong>DecoderException e) {error("Cannot read this image format.");return;}catch (IOException e) {error("Failed to read image data.");return;}catch (HistogramException e) {error("Cannot compute histogram for this image type.");return;}// Rebuild GUI}}mainPane.removeAll();if (histogram.getNumBands() == 3)createMultipleViews();elsecreateSingleView();mainPane.add(infoPane, BorderLayout.SOUTH);mainPane.invalidate();validate();pack();// Saves current histogram to a file selected by userpublic void saveHistogram() {if (histogram.getNumBands() == 0) {- 31 -


}error("No histogram data to save!");return;}else {fileChooser.setDialogTitle("Save histogram");if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {try {File file = fileChooser.getSelectedFile();if (file.exists()) {int response = JOptionPane.showConfirmDialog(this,"File will be overwritten! Are you sure?", "File exists",JOptionPane.OK_CANCEL_OPTION);if (response != JOptionPane.OK_OPTION)return;}histogram.write(new FileWriter(file));fileChooser.rescanCurrentDirectory();}catch (IOException e) {error("Cannot open output file.");}}}// Displays an error message in a dialog boxpublic void error(String message) {JOptionPane.showMessageDialog(this, message, "Error",JOptionPane.ERROR_MESSAGE);}public static void main(String[] argv) {if (argv.length > 0) {try {<strong>Image</strong>Decoder input = <strong>Image</strong>File.create<strong>Image</strong>Decoder(argv[0]);Buffered<strong>Image</strong> image = input.decodeAsBuffered<strong>Image</strong>();Histogram hist = new Histogram(image);HistogramTool histTool = new HistogramTool(hist, argv[0]);- 32 -


}histTool.pack();histTool.setVisible(true);}catch (Exception e) {System.err.println(e);System.exit(1);}}else {Histogram hist = new Histogram();HistogramTool histTool = new HistogramTool(hist, "HistogramTool");histTool.pack();histTool.setVisible(true);}}CalcHist.javaimport java.awt.image.Buffered<strong>Image</strong>;import java.io.FileWriter;public class CalcHist {public static void main(String[] argv) {if (argv.length > 1) {try {<strong>Image</strong>Decoder input = <strong>Image</strong>File.create<strong>Image</strong>Decoder(argv[0]);Buffered<strong>Image</strong> image = input.decodeAsBuffered<strong>Image</strong>();Histogram histogram = new Histogram(image);FileWriter histFile = new FileWriter(argv[1]);histogram.write(histFile);if (argv.length > 2) {FileWriter cumHistFile = new FileWriter(argv[2]);histogram.writeCumulative(cumHistFile);}System.exit(0);}catch (Exception e) {System.err.println(e);- 33 -


}}System.exit(1);}}else {System.err.println("usage: java CalcHist []");System.exit(1);}• <strong>Java</strong> HistogramTool(p.125)- 34 -


(p.126)(p.128)LISTING 6.5 A subclass of GreyMapOp that performs histogram equalisation.pckage com.pearsoneduc.ip.op;public class EqualiseOp extends GreyMapOp {/*** Constructs an EqualiseOp object <strong>using</strong> histogram data.* @param hist Histogram of the image to be equalised* @exception HistogramException if the histogram is from a colour image.*/public EqualiseOp(Histogram hist) throws HistogramException {float scale = 255.0f / hist.getNumSamples();for (int i = 0; i < 256; ++i)table[i] = (byte) Math.round(scale*hist.getCumulativeFrequency(i));}public void computeMapping(int low, int high) {}// Does nothing - limits are meaningless in histogram equalisation}- 35 -


Histogram hist = new Histogram(image);EqualiseOp eq = new EqualiseOp(hist);Buffered<strong>Image</strong> eq_img = eq.filter(image, null);(p.130)(p.135)- 36 -


(p.136)- 37 -


(p.138)- 38 -


(p.142)- 39 -


(p.144)(p.145)(p.146)Listing 7.1: WriteKernel.javaLISTING 7.1 A simple program demonstrating how a convolution kernel can becreated and written to an output stream.import java.io.OutputStreamWriter;import com.pearsoneduc.ip.op.StandardKernel;public class WriteKernel{- 40 -


}public static void main(String[] argv){float[] data = new float[25];float coeff = 0.04f;for (int i = 0; I < 25; ++i)data[i] = coeff;StandardKernel kernel = new StandardKernel (5, 5, data, 2);Kernel.write(new OutputStreamWriter(System.out));}(p.147)Reader input = new FileReader(“test.ker”);Kernel kernel = StandardKernel.createJerbek(input);Listing 7.2: Convolve.javausage: java ConvolutionTool matthead.png(p.152)LISTING 7.2 A convolution application with a command line interface.import java.awt.image.*;import java.io.*;import com.pearsoneduc.ip.io.*;import com.pearsoneduc.ip.op.*;import com.pearsoneduc.ip.util.IntervalTimer;public class Convolve {public static void main(String[] argv) {if (argv.length > 5) {try {// Parse command line arguments<strong>Image</strong>Decoder input = <strong>Image</strong>File.create<strong>Image</strong>Decoder(argv[0]);<strong>Image</strong>Encoder output = <strong>Image</strong>File.create<strong>Image</strong>Encoder(argv[1]);Reader kernelInput = new FileReader(argv[2]);boolean normaliseKernel = (Integer.parseInt(argv[3]) != 0);int borderStrategy =- 41 -


Math.max(1, Math.min(4, Integer.parseInt(argv[4])));int rescaleStrategy =Math.max(1, Math.min(3, Integer.parseInt(argv[5])));// Load image and kernelBuffered<strong>Image</strong> input<strong>Image</strong> = input.decodeAsBuffered<strong>Image</strong>();Kernel kernel =StandardKernel.createKernel(kernelInput, normaliseKernel);// Create convolution operator and convolve imageConvolutionOp convOp = new ConvolutionOp(kernel,borderStrategy, ConvolutionOp.SINGLE_PASS, rescaleStrategy);IntervalTimer timer = new IntervalTimer();timer.start();Buffered<strong>Image</strong> output<strong>Image</strong> = convOp.filter(input<strong>Image</strong>, null);System.out.println("Convolution finished [" + timer.stop() + " sec]");// Write results to output fileoutput.encode(output<strong>Image</strong>);System.exit(0);}}}catch (Exception e) {System.err.println(e);System.exit(1);}}else {System.err.println("usage: java Convolve " +" ");System.exit(1);}- 42 -


(p.153)(p.155)low pass filtering: convolution kernel >0e.g.: mean filter (Fig.7.12、Fig.7.13), Gaussian filter ((7.13)、Fig.7.14)(p.156)- 43 -


(p.157)- 44 -


(p.158)High pass filtering: kernel has +, - coefficients.(p.159)high boost filtering:if c=8 => high pass filterif c is more close to 8 => sharper(p.160)Listing 7.3: High PassKernel.javaUsage:java HighPassKernel > highpass.kerLISTING 7.3 A kernel class that performs high pass filtering.public class HighPassKernel extends StandardKernel {private static final float[] data = { -1.0f, -1.0f, -1.0f,-1.0f, 8.0f, -1.0f,-1.0f, -1.0f, -1.0f };public HighPassKernel() {super(3, 3, data, 0);- 45 -


}public static void main(String[] argv) {StandardKernel kernel = new HighPassKernel();kernel.write(new java.io.OutputStreamWriter(System.out));}}(p.161)java GaussianKernel: generate for σ=1.0 Gussian filterBuffered<strong>Image</strong> output<strong>Image</strong> = ConvolutionOp.gaussianBlur(input<strong>Image</strong>, 3.0f);↖σ(p.162)LISTING 7.4 A kernel class to support Gaussian low pass filteringpublic class GaussianKernel extends StandardKernel {public GaussianKernel() {}this(1.0f);public GaussianKernel(float sigma) {}super(getSize(sigma), getSize(sigma), createKernelData(sigma));public static int getSize(float sigma) {int radius = (int) Math.ceil(4.0f*sigma);return 2*radius+1;}public static float[] createKernelData(float sigma) {int n = (int) Math.ceil(4.0f*sigma);int size = 2*n+1;float[] data = new float[size*size];double r, s = 2.0*sigma*sigma;float norm = 0.0f;- 46 -


int i = 0;for (int y = -n; y


(p.163)LISTING 7.5 A mean filtering application.import java.awt.image.*;import com.pearsoneduc.ip.io.*;import com.pearsoneduc.ip.op.MeanKernel;import com.pearsoneduc.ip.util.IntervalTimer;public class MeanFilter {public static void main(String[] argv) {if (argv.length > 3) {try {<strong>Image</strong>Decoder input = <strong>Image</strong>File.create<strong>Image</strong>Decoder(argv[0]);<strong>Image</strong>Encoder output = <strong>Image</strong>File.create<strong>Image</strong>Encoder(argv[1]);int w = Integer.parseInt(argv[2]);int h = Integer.parseInt(argv[3]);Buffered<strong>Image</strong> input<strong>Image</strong> = input.decodeAsBuffered<strong>Image</strong>();Kernel kernel = new MeanKernel(w, h);ConvolveOp blurOp = new ConvolveOp(kernel);IntervalTimer timer = new IntervalTimer();timer.start();Buffered<strong>Image</strong> output<strong>Image</strong> = blurOp.filter(input<strong>Image</strong>, null);System.out.println("Mean filtering finished [" +timer.stop() + " sec]");output.encode(output<strong>Image</strong>);System.exit(0);}catch (Exception e) {System.err.println(e);System.exit(1);}}else {System.err.println("usage: java MeanFilter ");System.exit(1);}}}- 48 -


java MeanFilter matthead.png matt-mean.pngjava GaussianBlur matthead.png matt-gaus.pngw←3h←31←σ(p.164)Edge detection:g x (x,y) = h x * ƒ (x,y),g y (x,y) = h y * ƒ (x,y),Prewitt kernel:Sobel kernel:(p.165)gradient vectorgradient magnitude:≒gradient direction:- 49 -


(p.166)- 50 -


(p.169)Laplacian- 51 -


LOG (Laplacian of Gaussian) filter(p.171)DOG (Difference of Gaussian) filter(p.172)algorithm 7.7 -Canny edge dector(p.175)Buffered<strong>Image</strong>Op op = new CannyEdgeOp(2.0f, 50, 100);Buffered<strong>Image</strong> edgeMap = op.filter (image, null);Buffered<strong>Image</strong> map<strong>Image</strong> = op.getGradientOrientation<strong>Image</strong>();Buffered<strong>Image</strong> orient<strong>Image</strong> = op.getGradientOrientation<strong>Image</strong>();- 52 -


(p.176)mean filter:median filter: sort the 9 elements. Find the median value. (for impulsive noise)java MedianTest matthead.png 3 3java MinTest matthead.png 3 3java RankFilterTool matthead.png- 53 -


(p.184)σ-trimmed mean filter: f1≦ f2≦…≦ fn 2σ: the number of values removed from each end of the list.(p.186)MMSE filter:• java MMSEFilter matthead.png matt-mmse.png(p.189)- 54 -


(P.190)- 55 -


(p.194)(p.231)affine transformation(p.232)- 56 -


import java.awt.geom.AffineTransformAffineTransform transform = new AffineTransform();AffineTransform scale = AffineTransform.getScaleInstance(1.5, 1.5);// increase image size by 50%double angle = Math.Pi/4.0;double x = image.getWidth()/2.0;double y = image.getHeight()/2.0;AffineTransform rotate = AffineTransform.getRotateInstance(angel, x, y);(p.233)(p.234)(p.235)- 57 -


AffineTransform t = new AffineTransform();t.setToTranslation(10,20);t.rotate(30.0*Math.PI/180.0);AffineTransform translate = AffineTransform.getTranslateInstance(5. 0);t.concatenate(translate);AffineTransform scale = AffineTransform.getScaleInstance(0.3, 0.3);t.preConcatenate(scale);(p.236)(p.237)- 58 -


(p.238)LISTING 9.2 <strong>Java</strong> implementation of Algorithm 9.1public static Buffered<strong>Image</strong> rotate(Buffered<strong>Image</strong> input, double angle){int width = input.getWidth();int height = input.getHeight();Buffered<strong>Image</strong> output = new Buffered<strong>Image</strong>(width, height, input.getType());double a0 = Math.cos(angle*Math.PI/180.0);double b0 = Math.sin(angle*Math.PI/180.0);double a1 = -b0, b1 = a0;}int rx, ry;for (int y = 0; y < height; ++y)for (int x = 0; x < width; ++x){rx = (int) Math.round(a0*x + a1*y);ry = (int) Math.round(b0*x + b1*y);if (rx >= 0 && rx < width && ry >=0 && ry < height)output.setRGB(rx, ry, input.getRGB(x,y));}return output;(p.239)- 59 -


(p.240)- 60 -


(p.241)(p.243)- 61 -


(p.244)LISTING 9.3 A method to calculate the bounding box of a transformed image.public static Rectangle2D getBoundingBox(Buffered<strong>Image</strong> image, AffineTransformtransformation){// Apply transformation to image corners}int xmax = image.getWidth()-1;int ymax = image.getHeight()-1;Point2D[] corners = new Point2D.Double[4];Corners[0] = new Point2D.Double(, );• java Rotate1 input.png output.png 0 62.5• java Rotate2 input.png output.png ↑interp. ↑angle• java AffineTransformTool matthead.png(p.247)- 62 -


(p.248)(p.255)- 63 -


(p.258)LISTING 10.1 A <strong>Java</strong> class to perform grey level thresholding.public class ThresholdOp extends GreyMapOp {public ThresholdOp(int threshold) {}computeMapping(threshold, 255);public ThresholdOp(int low, int high) {}computeMapping(low, high);public void setThreshold(int threshold) {}computeMapping(threshold, 255);}public void setThresholds(int low, int high) {computeMapping(low, high);public void computeMapping(int low, int high) {if (low < 0 || high > 255 || low >= high)throw new java.awt.image.ImagingOpException("invalid thresholds");int i;for (i = 0; i < low; ++i)table[i] = (byte) 0;for (; i


(p.260)(p.261)LISTING 10.2 <strong>Java</strong> code to perform connected region labelling, taken from theRegionLabelOp class.public Buffered<strong>Image</strong> filter(Buffered<strong>Image</strong> src, Buffered<strong>Image</strong> dest) {check<strong>Image</strong>(src);if (dest == null)dest = createCompatibleDest<strong>Image</strong>(src, null);width = src.getWidth();height = src.getHeight();WritableRaster in = src.copyData(null);WritableRaster out = dest.getRaster();}int n = 1;for (int y = 0; y < height; ++y)for (int x = 0; x < width; ++x)if (in.getSample(x, y, 0) > 0) {label(in, out, x, y, n);++n;if (n > MAX_REGIONS)return dest;}return dest;private void label(WritableRaster in, WritableRaster out,- 65 -


int x, int y, int n) {in.setSample(x, y, 0, 0);out.setSample(x, y, 0, n);int j, k;for (int i = 0; i < connectivity; ++i) {j = x + delta[i].x;k = y + delta[i].y;if (in<strong>Image</strong>(j, k) && in.getSample(j, k, 0) > 0)label(in, out, j, k, n);}}}private final boolean in<strong>Image</strong>(int x, int y) {}return x >= 0 && x < width && y >= 0 && y < height;(p.263)- 66 -


java RegionGrowingTool matthead.png↗4-connetjava RegionGrow test.jpg region.pbm 100 100 4 35( x , y ) ↘threshold(p.273)- 67 -


(p.277)Erosion: ƒΘ s (ƒ→ image; s→ stnecturing element)(p.278)boundary: ƒ – (ƒΘ s) = ƒ – erosion(ƒ)- 68 -


Dilation: ƒ ♁ s(p.282)LISTING 11.1 BinaryErodeOp’s filter() method.public Buffered<strong>Image</strong> filter(Buffered<strong>Image</strong> src, Buffered<strong>Image</strong> dest) {check<strong>Image</strong>(src);if (dest == null)dest = createCompatibleDest<strong>Image</strong>(src, null);int w = src.getWidth();int h = src.getHeight();Raster srcRaster = src.getRaster();WritableRaster destRaster = dest.getRaster();// Determine range of pixels for which operation can be performedPoint origin = structElement.getOrigin(null);int xmin = Math.max(origin.x, 0);int ymin = Math.max(origin.y, 0);int xmax = origin.x + w - structElement.getWidth();- 69 -


int ymax = origin.y + h - structElement.getHeight();xmax = Math.min(w-1, xmax);ymax = Math.min(h-1, ymax);// Fit structuring element into source imagefor (int y = ymin; y


union:(p.284)Opening = erosion dilate(p.286)closing: dilate → erode(p.287)hit and miss transform:(p.288)- 71 -


(p.289)LISTING 11.2 filter() method of BinaryOpenOp.public Buffered<strong>Image</strong> filter(Buffered<strong>Image</strong> src, Buffered<strong>Image</strong> dest){BinaryErodeOp erodeOp = new BinaryErodeOp(structElement);BinaryDilateOp dilateOp = new BinaryDilateOp(structElement);if (dest == null)dest = createCompatibleDest<strong>Image</strong>(src, null);return dilateOp.filter(erodeOp.filter(src, null), dest);}java BinaryOpen input.pbm result.pbm sq3x3.basjava HitAndMiss java BinaryMorphologyTool char_a.png 5 5heightwidth# binary structuringelement# width =3# height = 3# xorigin = 1# yorigin = 1111111111- 72 -


(p.291)- 73 -


(p.292)greyscale erosiongreyscale dilation(p.293)- 74 -


(p.294)detect peakstop-hat transform: g = ƒ -(ƒ ο s).open(ƒ․s) – ƒ → detect valleys in the grey level surfaceclosing(p.296)GreyErodeGreyDilateGreyOpenGreyClose(p.297)Exercises4. Using the classes described in this chapter, write a <strong>Java</strong> program to perform thetop-hat transform.(p.301)RMS error (root-mean-square) for MXN image- 75 -


(p.303)entropy(p.304)information redundancycompression ratioinsideContour(A,B) = A – erode(A,B)outsideContour(A,B) = dilate(A,B)-AmiddleContour(A,B) = dilate(A,B)-erode(A,B)http://iris.usc.edu/ision-Notes/bibliogruphy/text/contents.htmlhttp://140.115.11.235/~chen/course/vision- 76 -

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!