Guest User

Untitled

a guest
Mar 21st, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Rectangle;
  3. import java.awt.image.BufferedImage;
  4. import java.io.File;
  5. import java.io.IOException;
  6. import javax.imageio.ImageIO;
  7. import javax.swing.ImageIcon;
  8. import javax.swing.JLabel;
  9. import javax.swing.JOptionPane;
  10.  
  11. public class Test {
  12.  
  13. public static void main(String[] args) throws IOException {
  14. BufferedImage img = ImageIO.read(new File("/Users/swhitehead/Downloads/47hb1.png"));
  15. Rectangle bounds = getBounds(img, Color.WHITE);
  16. System.out.println(bounds);
  17. BufferedImage trimmed = img.getSubimage(bounds.x, bounds.y, bounds.width, bounds.height);
  18. JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(trimmed)));
  19. }
  20.  
  21. public static Rectangle getBounds(BufferedImage img, Color fillColor) {
  22. int top = getYInset(img, 20, 0, 1, fillColor);
  23. int bottom = getYInset(img, 20, img.getHeight() - 1, -1, fillColor);
  24. int left = getXInset(img, 0, top, 1, fillColor);
  25. int right = getXInset(img, img.getWidth() - 1, top, -1, fillColor);
  26.  
  27. return new Rectangle(left, top, right - left, bottom - top);
  28. }
  29.  
  30. public static int getYInset(BufferedImage img, int x, int y, int step, Color fillColor) {
  31. while (new Color(img.getRGB(x, y), true).equals(fillColor)) {
  32. y += step;
  33. }
  34. return y;
  35. }
  36.  
  37. public static int getXInset(BufferedImage img, int x, int y, int step, Color fillColor) {
  38. while (new Color(img.getRGB(x, y), true).equals(fillColor)) {
  39. x += step;
  40. }
  41. return x;
  42. }
  43. }
  44.  
  45. import static marvin.MarvinPluginCollection.*;
  46. import java.awt.Rectangle;
  47. import marvin.image.MarvinImage;
  48. import marvin.io.MarvinImageIO;
  49.  
  50. public class BoardSegmentation {
  51.  
  52. public BoardSegmentation() {
  53. MarvinImage imageOriginal = MarvinImageIO.loadImage("./res/board.png");
  54. MarvinImage image = imageOriginal.clone();
  55. thresholding(image, 250);
  56. Rectangle rect = getBoundingBox(image);
  57. crop(imageOriginal, image, rect.x, rect.y, rect.width, rect.height);
  58. MarvinImageIO.saveImage(image, "./res/board_cropped.png");
  59. }
  60. public Rectangle getBoundingBox(MarvinImage image) {
  61. Rectangle r = new Rectangle();
  62. r.x = -1; r.y = -1; r.width = -1; r.height = -1;
  63. for(int y=0; y<image.getHeight(); y++) {
  64. for(int x=0; x<image.getWidth(); x++) {
  65. if(image.getIntColor(x, y) == 0xFF000000) {
  66. if(r.x == -1 || x < r.x) { r.x = x; }
  67. if(r.width == -1 || x > r.x + r.width) { r.width = x - r.x; }
  68. if(r.y == -1 || x < r.y) { r.y = y; }
  69. if(r.height == -1 || y > r.y + r.height) { r.height = y - r.y; }
  70. }
  71. }
  72. }
  73. return r;
  74. }
  75. public static void main(String[] args) {
  76. new BoardSegmentation();
  77. }
  78. }
Add Comment
Please, Sign In to add comment