Pacific-Design.com

    
Home Index

1. Computer Vision

2. ReflectionPadding

+ Image

Computer Vision / ReflectionPadding /

Image Reflection Padding

package fullreflection;


public class Reflection {

    /*--------------------------------------------------------------------------------------------*/

    public int[][] conv(int[][] iimage, int x_size, int y_size) {

        int i, j, lines, samples;
        int padded_lines, padded_samples;
        int half_x, half_y, oi, oj;
        int[][] oimage;

        lines = iimage.length;   //this.lines;
        samples = iimage.length; //this.samples;
        
        padded_lines = lines + y_size - 1;
        padded_samples = samples + x_size - 1;

        oimage = new int[padded_lines][padded_samples];

        half_x = x_size >> 1;
        half_y = y_size >> 1;

        // -- Fill interior of padded array with actual image. |
        for (i = 0; i < lines; ++i) {
            for (j = 0; j < samples; ++j) {
                oimage[i + half_y][j + half_x] = iimage[i][j];
            }
        }

        // -- Fill border of padded array with a mirror image of |
        //    the actual image reflected about the boundaries.   |
        // Left border 
        for (i = 0; i < lines; ++i) {
            for (j = 0, oj = half_x - 1; j < (int) half_x; ++j, --oj) {
                oimage[i + half_y][oj] = iimage[i][j];
            }
        }

        // Right border
        for (i = 0; i < lines; ++i) {
            for (j = samples - half_x, oj = samples + (2 * half_x) - 1; j < samples; ++j, --oj) {
                oimage[i + half_y][oj] = iimage[i][j];
            }
        }

        // Top border
        for (i = 0, oi = half_y - 1; i < (int) half_y; ++i, --oi) {
            for (j = 0; j < samples; ++j) {
                oimage[oi][j + half_x] = iimage[i][j];
            }
        }

        // Bottom border
        for (i = lines - half_y, oi = lines + (2 * half_y) - 1; i < lines; ++i, --oi) {
            for (j = 0; j < samples; ++j) {
                oimage[oi][j + half_x] = iimage[i][j];
            }
        }

        // Top left corner
        for (i = 0; i < (int) half_y; ++i) {
            for (j = 0, oj = half_x - 1; j < (int) half_x; ++j, --oj) {
                oimage[i][oj] = oimage[i][j + half_x];
            }
        }

        // Bottom right corner
        for (i = lines + half_y; i < lines + (2 * half_y); ++i) {
            for (j = samples, oj = samples + (2 * half_x) - 1; j < samples + half_x; ++j, --oj) {
                oimage[i][oj] = oimage[i][j];
            }
        }

        // Top right corner
        for (i = 0; i < (int) half_y; ++i) {
            for (j = samples, oj = samples + (2 * half_x) - 1; j < samples + half_x; ++j, --oj) {
                oimage[i][oj] = oimage[i][j];
            }
        }

        // Bottom left corner
        for (i = lines + half_y; i < lines + (2 * half_y); ++i) {
            for (j = 0, oj = half_x - 1; j < (int) half_x; ++j, --oj) {
                oimage[i][oj] = oimage[i][j + half_x];
            }
        }

        return oimage;
    }
    /*--------------------------------------------------------------------------------------------*/   
    public int[][] ScaleDown(int[][] paddedImg, int x_size, int y_size) {
        
        int height=paddedImg.length-y_size+1;
        int width=paddedImg[0].length-x_size+1;
        
        int half_y=y_size/2;
        int half_x=x_size/2;
        
        int[][] scaledDownImg = new int[height][width];
        
        for(int i=0; i<512; i++) {
            for(int j=0; j<512; j++) {                                
                scaledDownImg[i][j] = paddedImg[half_y+i][half_x+j];
            }
        }
        
        return scaledDownImg;
        
    }
    /*--------------------------------------------------------------------------------------------*/
}


package fullreflection;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class Main {

    private static final int padding_x = 201;
    private static final int padding_y = 201;
    
    public static void main(String[] args) throws IOException {

        int[][] iimage = ImageRead("src/image/Lenna.png");
        Reflection ref = new Reflection();
        
        int[][] oimage = ref.conv(iimage, padding_x, padding_y);  // must be odd number
        ImageWrite(oimage, "src/image/reflection.png");
        
        int[][] simage = ref.ScaleDown(oimage,padding_x,padding_y);
        ImageWrite(simage, "src/image/scaledown.png");

    }

    /*--------------------------------------------------------------------------------------------*/

    public static int[][] ImageRead(String filename) {

        try {
            File infile = new File(filename);
            BufferedImage bi = ImageIO.read(infile);

            // -- separate image into RGB components
            int red[][] = new int[bi.getHeight()][bi.getWidth()];
            int grn[][] = new int[bi.getHeight()][bi.getWidth()];
            int blu[][] = new int[bi.getHeight()][bi.getWidth()];
            for (int i = 0; i < red.length; ++i) {
                for (int j = 0; j < red[i].length; ++j) {
                    red[i][j] = bi.getRGB(j, i) >> 16 & 0xFF;
                    grn[i][j] = bi.getRGB(j, i) >> 8 & 0xFF;
                    blu[i][j] = bi.getRGB(j, i) & 0xFF;
                }
            }

            return grn;

        } catch (IOException e) {
            System.out.println("image I/O error");
            return null;
        }
    }
    /*--------------------------------------------------------------------------------------------*/
    public static void ImageWrite(int img[][], String filename) {
        try {
            BufferedImage bi = new BufferedImage(img[0].length, img.length, BufferedImage.TYPE_INT_RGB);

            // -- prepare output image
            for (int i = 0; i < bi.getHeight(); ++i) {
                for (int j = 0; j < bi.getWidth(); ++j) {
                    int val = img[i][j];
                    int pixel = (val << 16) | (val << 8) | (val);
                    bi.setRGB(j, i, pixel);
                }
            }

            // -- write output image
            File outputfile = new File(filename);
            ImageIO.write(bi, "png", outputfile);
        } catch (IOException e) {

        }
    }
    /*--------------------------------------------------------------------------------------------*/    
}