Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2009
    Posts
    34
    Rep Power
    0

    Default Downsampling....

    Can somebody suggest me to implement down sampling algorithm using java....:mad:

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    5

    Default

    something like the libsamplerate for audio formats, but in Java.?

    One one simple way to start is to make a state machine (or counter) that will simply discard one of every N samples, assuming each sample is an element in an array, you work to re-write a new array (or stream) as you go, using the counter to discard the periodic sample.

    more complicated approaches might involve interpolating the value that was discarded (average of samples before and after the discarded one). or using a fourier transform to first convert the samples into a frequency domain, and then inverse transform to get back to a time domain, but here we would unpack them into fewer samples.

    See: Data Analysis they go into a lot more theory on this. while their approach is oriented towards reducing the data set for statistical analysis, the similar ideas would be employed in down sampling too I would imagine.

  3. #3
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    To downsample by N, you just pick every Nth sample. However, you have to filter first or you'll end up with aliasing. Typical simple filters include averaging and peak picking. These work ok for a lot of applications, but not so well for others. An entire field of engineering knon as signal processing is devoted to issues like this.

  4. #4
    Join Date
    Mar 2009
    Posts
    34
    Rep Power
    0

    Default

    Alryt here is the code which i hav written so far... input to the system are binary images of black and white. I wanna reduce the images to 10 * 10 ratio. Plz help me with da code...
    Or else let me know a API which i can perfome the same task??
    Java Code:
    /*
     * NewJFrame.java
     *
     * Created on March 8, 2010, 7:49 AM
     */
    
    package downsampling;
    
    import java.awt.image.BufferedImage;
    import java.io.File;
    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    import javax.swing.JFileChooser;
    
    
    
    
    /**
     *
     * @author  acer
     */
    public class NewJFrame extends javax.swing.JFrame {
         BufferedImage img = null;
        private int DrawSizeX = 31;
        private int DrawSizeY = 30;
        private int ScaleSizeX = 9;
        private int ScaleSizeY = 9;
        private java.util.Vector imageHolder = new java.util.Vector();
        private java.awt.Image downsamplePreviewImage = null;
        private java.awt.image.BufferedImage downSample = new java.awt.image.BufferedImage(getScaleSizeX(),getScaleSizeY(),java.awt.image.BufferedImage.TYPE_INT_RGB);
        private java.awt.image.BufferedImage drawImage = new java.awt.image.BufferedImage(getDrawSizeX(),getDrawSizeY(),java.awt.image.BufferedImage.TYPE_INT_RGB);
        private java.util.Vector idHolder = new java.util.Vector();
        private int currentImage = 0;
        private boolean alone;    
        protected int downSampleLeft;    
        protected int downSampleRight;    
        protected int downSampleTop;    
        protected int downSampleBottom;        
        protected double ratioX;    
        protected double ratioY;    
        protected int pixelMap[];
        private javax.swing.JPanel DownsamplePanel;
        
        /** Creates new form NewJFrame */
        public NewJFrame() {
            initComponents();
        }
        
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
    
            jButton1 = new javax.swing.JButton();
            jButton2 = new javax.swing.JButton();
            jLabel1 = new javax.swing.JLabel();
            jButton3 = new javax.swing.JButton();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            jButton1.setText("Downsampling");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });
    
            jButton2.setText("Browse");
            jButton2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton2ActionPerformed(evt);
                }
            });
    
            jButton3.setText("Downsampling JOONE");
            jButton3.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton3ActionPerformed(evt);
                }
            });
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(22, 22, 22)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jButton1)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 206, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jButton3)
                        .addComponent(jButton2))
                    .addContainerGap(31, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(39, 39, 39)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButton1)
                        .addComponent(jButton2))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(34, 34, 34)
                            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 193, Short.MAX_VALUE))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(18, 18, 18)
                            .addComponent(jButton3)))
                    .addContainerGap())
            );
    
            pack();
        }// </editor-fold>                        
    
        private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    try{
                    
                    JFileChooser chooser = new JFileChooser();          
                               
                    chooser.setMultiSelectionEnabled(false);     
    
    
                    int option = chooser.showOpenDialog(this);
                    File fl = null;
                   
    
                    if (option == JFileChooser.APPROVE_OPTION) {
                       fl = chooser.getSelectedFile();                  
                                            
                        img = ImageIO.read(fl);
                        
                        jLabel1.setIcon(new ImageIcon(img));
                    jLabel1.setText("");
                        
    
    //                }
    
                    
                }
    }
                catch(NullPointerException e){
                    System.out.println("Cancel option Selected ");
                }
                catch(OutOfMemoryError e){
                    System.out.print(e.getMessage());
                    //JOptionPane.showMessageDialog(null, "Restart The Programme To Acquire Required Memory"); 
    //                Main.main(null);
    //                this.dispose();
                }
                catch(Exception e){
                    e.printStackTrace();
                }
        }                                        
    
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            // TODO add your handling code here:
            ImageIcon icon = (ImageIcon) jLabel1.getIcon();        
     
        }                                        
        
        private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            // TODO add your handling code here:
            DrawSizeX = img.getWidth();
            DrawSizeY = img.getHeight();
            downSample();
        }                                        
       protected boolean hLineClear(int y) {
            int w = drawImage.getWidth(this);
            for ( int i=0;i<w;i++ ) {
                if ( pixelMap[(y*w)+i] !=-1 )
                    return false;
            }
            return true;
        }
        
        /**
         * This method is called to determine ....
         *
         * @param x The vertical line to scan.
         * @return True if there are any pixels in the
         * specified vertical line.
         */
        protected boolean vLineClear(int x) {
            int w = drawImage.getWidth(this);
            int h = drawImage.getHeight(this);
            for ( int i=0;i<h;i++ ) {
                if ( pixelMap[(i*w)+x] !=-1 )
                    return false;
            }
            return true;
        }
        protected void findBounds(int w,int h) {
            // top line
            for ( int y=0;y<h;y++ ) {
                if ( !hLineClear(y) ) {
                    downSampleTop=y;
                    break;
                }
            }
            // bottom line
            for ( int y=h-1;y>=0;y-- ) {
                if ( !hLineClear(y) ) {
                    downSampleBottom=y;
                    break;
                }
            }
            // left line
            for ( int x=0;x<w;x++ ) {
                if ( !vLineClear(x) ) {
                    downSampleLeft = x;
                    break;
                }
            }
            
            // right line
            for ( int x=w-1;x>=0;x-- ) {
                if ( !vLineClear(x) ) {
                    downSampleRight = x;
                    break;
                }
            }
        }
        
        public void downSample(){
             int w = drawImage.getWidth(this);
            int h = drawImage.getHeight(this);
            
            java.awt.image.PixelGrabber grabber = new java.awt.image.PixelGrabber(drawImage,0,0,w,h,true);
            
            try {
                
                grabber.grabPixels();
                pixelMap = (int[])grabber.getPixels();
                findBounds(w,h);
                
                // now downsample
                ratioX = (double)(downSampleRight - downSampleLeft)/(double)downSample.getWidth();
                ratioY = (double)(downSampleBottom - downSampleTop)/(double)downSample.getHeight();
                
    //            for ( int y=0;y<downSample.getHeight();y++ ) {
    //                for ( int x=0;x<downSample.getWidth();x++ ) {
    //                    if ( downSampleQuadrant(x,y) )
    //                        downSample.setRGB(x,y,java.awt.Color.BLACK.getRGB());
    //                    else
    //                        downSample.setRGB(x,y,java.awt.Color.WHITE.getRGB());
    //                }
    //            }
    //            
                // We have now down sampled the current draw image to the downSample image.
                // Now produce a large sclae version of the down sample so user can see it.
                
                downsamplePreviewImage = downSample.getScaledInstance(getDrawSizeX(), getDrawSizeY(), java.awt.Image.SCALE_DEFAULT);
                if ( downsamplePreviewImage!= null) {
                    ((ImageDrawer)DownsamplePanel).setImageToDraw(downsamplePreviewImage);
                }
            } catch ( InterruptedException e ) {
            }
            
        
        }
        
        
        public int getDrawSizeX() {
            return DrawSizeX;
        }
        
        /** Setter for property DrawSizeX.
         * @param DrawSizeX New value of property DrawSizeX.
         *
         */
        public void setDrawSizeX(int DrawSizeX) {
            this.DrawSizeX = DrawSizeX;
        }
        
        /** Getter for property DrawSizeY.
         * @return Value of property DrawSizeY.
         *
         */
        public int getDrawSizeY() {
            return DrawSizeY;
        }
        
        /** Setter for property DrawSizeY.
         * @param DrawSizeY New value of property DrawSizeY.
         *
         */
        public void setDrawSizeY(int DrawSizeY) {
            this.DrawSizeY = DrawSizeY;
        }
        
        /** Getter for property ScaleSizeX.
         * @return Value of property ScaleSizeX.
         *
         */
        public int getScaleSizeX() {
            return ScaleSizeX;
        }
        
        /** Setter for property ScaleSizeX.
         * @param ScaleSizeX New value of property ScaleSizeX.
         *
         */
        public void setScaleSizeX(int ScaleSizeX) {
            this.ScaleSizeX = ScaleSizeX;
        }
        
        /** Getter for property ScaleSizeY.
         * @return Value of property ScaleSizeY.
         *
         */
        public int getScaleSizeY() {
            return ScaleSizeY;
        }
        
        /** Setter for property ScaleSizeY.
         * @param ScaleSizeY New value of property ScaleSizeY.
         *
         */
        public void setScaleSizeY(int ScaleSizeY) {
            this.ScaleSizeY = ScaleSizeY;
        }
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new NewJFrame().setVisible(true);
                }
            });
        }
        
        // Variables declaration - do not modify                     
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton2;
        private javax.swing.JButton jButton3;
        private javax.swing.JLabel jLabel1;
        // End of variables declaration                   
        
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •