Advertisement
mahmudkuet

Homography matrix

Feb 3rd, 2016
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.16 KB | None | 0 0
  1. static double x1,y1,x2,y2,x3,y3,x4,y4,X1,Y1,X2,Y2,X3,Y3,X4,Y4;
  2.    
  3.    
  4.     public static void main(String[] args) {
  5.        
  6.         /*
  7.         * read image
  8.         */
  9.         BufferedImage imgBuffer = null;
  10.         try {
  11.             imgBuffer = ImageIO.read(new File("C:\\Users\\mohar\\Desktop\\test\\3.jpg"));
  12.         } catch (IOException ex) {
  13.             Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
  14.         }
  15.  
  16.        
  17. //        int rgb_val = imgBuffer.getRGB(9, 9);
  18. //        System.out.println(imgBuffer.getWidth());
  19. //        System.out.println(imgBuffer.getHeight());
  20. //        Color p = new Color(rgb_val);
  21. //        System.out.println(p.getRed());
  22. //        System.out.println(p.getGreen());
  23. //        System.out.println(p.getBlue());
  24.        
  25.         /*
  26.         * scale image
  27.         */
  28.         /*File output = new File("G:\\matlab practise\\output.jpg");
  29.         try {
  30.             ImageIO.write(sclae2(imgBuffer, 1000, 1280), "jpg", output);
  31.         } catch (IOException ex) {
  32.             Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
  33.         }*/
  34.        
  35.        
  36.         x1 = 319;
  37.         y1 = 558;
  38.         x2 = 1527;
  39.         y2 = 534;
  40.         x3 = 1675;
  41.         y3 = 1939;
  42.         x4 = 199;
  43.         y4 = 1979;
  44.        
  45.         X1 = 0;
  46.         Y1 = 0;
  47.         X2 = 499;
  48.         Y2 = 0;
  49.         X3 = 499;
  50.         Y3 = 499;
  51.         X4 = 0;
  52.         Y4 = 499;
  53.        
  54.         double M_a[][] = {
  55.             {x1,y1,1,0,0,0,-x1*X1,-y1*X1},
  56.             {x2,y2,1,0,0,0,-x2*X2,-y2*X2},
  57.             {x3,y3,1,0,0,0,-x3*X3,-y3*X3},
  58.             {x4,y4,1,0,0,0,-x4*X4,-y4*X4},
  59.             {0,0,0,x1,y1,1,-x1*Y1,-y1*Y1},
  60.             {0,0,0,x2,y2,1,-x2*Y2,-y2*Y2},
  61.             {0,0,0,x3,y3,1,-x3*Y3,-y3*Y3},
  62.             {0,0,0,x4,y4,1,-x4*Y4,-y4*Y4},
  63.         };
  64.        
  65.         double M_b[][] = {
  66.             {X1},
  67.             {X2},
  68.             {X3},
  69.             {X4},
  70.             {Y1},
  71.             {Y2},
  72.             {Y3},
  73.             {Y4},
  74.         };
  75.        
  76.         Matrix A = new Matrix(M_a);
  77.         Matrix B = new Matrix(M_b);
  78.         Matrix C = A.solve(B);
  79.         double a = C.get(0, 0);
  80.         double b = C.get(1, 0);
  81.         double c = C.get(2, 0);
  82.         double d = C.get(3, 0);
  83.         double e = C.get(4, 0);
  84.         double f = C.get(5, 0);
  85.         double g = C.get(6, 0);
  86.         double h = C.get(7, 0);
  87.        
  88.        
  89.         int width = imgBuffer.getWidth();
  90.         int height = imgBuffer.getHeight();
  91.        
  92.         BufferedImage output = new BufferedImage(500, 500, BufferedImage.TYPE_INT_RGB);
  93.         int max = -5000;
  94.         int min = 5000;
  95.         for (int i = 0; i < width; i++) {
  96.             for (int j = 0; j < height; j++) {
  97.                 if(isInside(i, j)){
  98.                    int x = (int)(((a*i)+(b*j)+c)/((g*i)+(h*j)+1));
  99.                    int y = (int)(((d*i)+(e*j)+f)/((g*i)+(h*j)+1));
  100.                    if(y > max) max = y;
  101.                    if(y < min) min = y;
  102.                    int p = imgBuffer.getRGB(i, j);
  103.                    output.setRGB(x, y, p);
  104.                 }
  105.                
  106.             }
  107.         }
  108.         System.out.println("max: " + max);
  109.         System.out.println("min: " + min);
  110.         File output1 = new File("C:\\Users\\mohar\\Desktop\\test\\bal.jpg");
  111.         try {
  112.             ImageIO.write(output, "jpg", output1);
  113.         } catch (IOException ex) {
  114.             Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
  115.         }
  116.        
  117.        
  118.        
  119.        
  120.        
  121.     }
  122.    
  123.     public static boolean isInside(int x, int y){
  124.        
  125.         double apd = Math.abs(0.5 * (x1*y+x*y4+x4*y1-x*y1-x4*y-x1*y4));
  126.         double dpc = Math.abs(0.5 * (x4*y+x*y3+x3*y4-x*y4-x3*y-x4*y3));
  127.         double cpb = Math.abs(0.5 * (x3*y+x*y2+x2*y3-x*y3-x2*y-x3*y2));
  128.         double pba = Math.abs(0.5 * (x*y2+x2*y1+x1*y-x2*y-x1*y2-x*y1));
  129.         double rec = Math.abs(0.5 * (x1*y2+x2*y3+x3*y4+x4*y1-x2*y1-x3*y2-x4*y3-x1*y4));
  130.        
  131.         if((apd + dpc + cpb + pba) > rec){
  132.             return false;
  133.         }else{
  134.             return true;
  135.         }
  136.        
  137.        
  138.        
  139.        
  140.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement