Advertisement
dan-masek

Sudoku intersections with OpenCV in Java

Mar 27th, 2019
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.12 KB | None | 0 0
  1. import org.opencv.core.Mat;
  2. import org.opencv.core.Core;
  3. import org.opencv.core.Point;
  4. import org.opencv.core.Scalar;
  5. import org.opencv.imgcodecs.Imgcodecs;
  6. import org.opencv.imgproc.Imgproc;
  7.  
  8. public class sudoku
  9. {
  10.     public static void main(String[] args)
  11.     {
  12.         System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
  13.        
  14.         Mat image = Imgcodecs.imread("sudoku.png", Imgcodecs.CV_LOAD_IMAGE_COLOR);
  15.         if ((image == null) || image.empty()) {
  16.             System.out.println("Failed to load input image.");
  17.             System.exit(-1);
  18.         }
  19.        
  20.         int TILES_X = 9;
  21.         int TILES_Y = 9;
  22.  
  23.         // Estimated point coordinates from sample image...
  24.         Point p1 = new Point(110, 128);
  25.         Point p2 = new Point(518, 49);
  26.         Point p3 = new Point(187, 524);
  27.         Point p4 = new Point(611, 428);
  28.        
  29.         // Vectors representing each edge of the puzzle
  30.         Point dx_puzzle = new Point(p2.x - p1.x, p2.y - p1.y);
  31.         Point dy_puzzle = new Point(p3.x - p1.x, p3.y - p1.y);
  32.        
  33.         // Scaled down vectors representing edge of a single tile
  34.         Point dx_tile = new Point(dx_puzzle.x / TILES_X, dx_puzzle.y / TILES_X);
  35.         Point dy_tile = new Point(dy_puzzle.x / TILES_Y, dy_puzzle.y / TILES_Y);
  36.        
  37.         System.out.println(String.format("dx_puzzle = %s", dx_puzzle));
  38.         System.out.println(String.format("dy_puzzle = %s", dy_puzzle));
  39.         System.out.println(String.format("dx_tile = %s", dx_tile));
  40.         System.out.println(String.format("dy_tile = %s", dy_tile));
  41.        
  42.         for (int row = 0; row < (TILES_Y + 1); row++) {
  43.             for (int col = 0; col < (TILES_X + 1); col++) {
  44.                 double x = Math.round(p1.x + dx_tile.x * col + dy_tile.x * row);
  45.                 double y = Math.round(p1.y + dx_tile.y * col + dy_tile.y * row);
  46.                 Point p = new Point(x, y);
  47.                 System.out.println(String.format("p[%d,%d] = %s", row, col, p));
  48.                 Imgproc.circle(image, p, 3, new Scalar(255,0,255));
  49.             }            
  50.         }
  51.        
  52.          Imgcodecs.imwrite("sudoku_out.png", image);
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement