Java BufferedImage Operation buildingCoordinatesInImage(BufferedImage imageSection)

Here you can find the source of buildingCoordinatesInImage(BufferedImage imageSection)

Description

building Coordinates In Image

License

Open Source License

Declaration

public static Polygon[] buildingCoordinatesInImage(BufferedImage imageSection) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.awt.Color;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.image.BufferedImage;

import java.util.ArrayList;

import java.util.List;

public class Main {
    public static Polygon[] buildingCoordinatesInImage(BufferedImage imageSection) {
        int[][] pixels = pixelsFromBufferedImage(imageSection);
        List<Point> bluePointList = new ArrayList<>();
        List<Polygon> buildings = new ArrayList<>();

        for (int x = 0; x < pixels.length; x++) {
            for (int y = 0; y < pixels.length; y++) {
                int px = pixels[x][y];
                int blue = (int) (px & 0xFF);
                if (blue == 255) { // is standard blue hue
                    bluePointList.add(new Point(x, y));
                }/*  w ww.ja va 2  s . c  o m*/
            }
        }
        //Point oldPoint = new Point(-1, -1);
        while (bluePointList.size() > 0) {
            System.out.println(bluePointList.size());
            Point currentPoint = bluePointList.get(0);
            /*if(oldPoint.equals(currentPoint)){
               bluePointList.remove(0);
               continue;
               // make sure no repetition happens
            }
            oldPoint = currentPoint;*/
            // all on bluelist should be removed from outer bluelist
            List<Point> starBlueList = new ArrayList<>();
            Point[] coordinatePoints = starRunner(pixels, currentPoint.x, currentPoint.y, starBlueList);
            for (int i = 0; i < starBlueList.size(); i++) {
                for (int j = 0; j < bluePointList.size(); j++) {
                    final Point star = starBlueList.get(i);
                    final Point blue = bluePointList.get(j);
                    if (star.equals(blue)) {
                        bluePointList.remove(j);
                        j--;
                    }
                }
            }

            int[] xes = new int[coordinatePoints.length];
            int[] ys = new int[coordinatePoints.length];
            for (int coordCounter = 0; coordCounter < coordinatePoints.length; coordCounter++) {
                final Point temp = coordinatePoints[coordCounter];
                xes[coordCounter] = temp.x;
                ys[coordCounter] = temp.y;
                for (int i = 0; i < bluePointList.size(); i++) {
                    if (bluePointList.get(i).equals(temp)) {
                        bluePointList.remove(i);
                        i--;
                    }
                }
            }
            Polygon border = new Polygon(xes, ys, coordinatePoints.length);
            buildings.add(border); // clean list
            cleanList(bluePointList, border); // TODO: Figure out why this causes efficiency when not tired (10 secs!)
        }

        // clean buildings
        for (int i = 0; i < buildings.size(); i++) {
            Polygon building = buildings.get(i);
            int area = polyArea(building);
            if (area < 9) {
                buildings.remove(i);
                i--;
            }
        }

        return buildings.toArray(new Polygon[0]);
    }

    public static int[][] pixelsFromBufferedImage(final BufferedImage image) {
        int[][] pixArr = new int[image.getWidth()][image.getHeight()];
        for (int x = 0; x < image.getWidth(); x++) {
            for (int y = 0; y < image.getHeight(); y++) {
                pixArr[x][y] = image.getRGB(x, y);
            }
        }
        return pixArr;
    }

    public static Point[] starRunner(int[][] screen, int x, int y, List<Point> blueList) {
        if (x < 0 || y < 0 || x >= screen.length || y >= screen[0].length)
            return new Point[0];

        Color pixelColor = new Color(screen[x][y], true);
        if (!(pixelColor.getBlue() == 255 && pixelColor.getGreen() == 0 && pixelColor.getRed() == 0)) {
            // not blue
            Point[] containerArr = { new Point(x, y) };
            return containerArr;
        }
        boolean didEndSoon = false;
        for (Point p : blueList) {
            if (p.x == x && p.y == y) {
                didEndSoon = true;
                break;
            }
        }
        if (didEndSoon) {
            // is on list
            return new Point[0];
        }

        blueList.add(new Point(x, y));

        // not on list, blue, so valid, untouched pixel
        Point[] up = starRunner(screen, x, y + 1, blueList);
        Point[] down = starRunner(screen, x, y - 1, blueList);
        Point[] right = starRunner(screen, x + 1, y, blueList);
        Point[] left = starRunner(screen, x - 1, y, blueList);

        Point[] totalArr = new Point[up.length + down.length + right.length + left.length];
        System.arraycopy(up, 0, totalArr, 0, up.length);
        System.arraycopy(down, 0, totalArr, up.length, down.length);
        System.arraycopy(right, 0, totalArr, up.length + down.length, right.length);
        System.arraycopy(left, 0, totalArr, up.length + down.length + right.length, left.length);
        return totalArr;
    }

    private static int cleanList(List<Point> blueList, Polygon borders) {
        int numRemoved = 0;
        for (int i = 0; i < blueList.size(); i++) {
            Point currentPoint = blueList.get(i);
            if (borders.contains(currentPoint)) {
                blueList.remove(i);
                i--;
                numRemoved++;
            }
        }
        return numRemoved;
    }

    public static int polyArea(Polygon target) {
        int sum = 0;
        for (int i = 0; i < target.npoints; i++) {
            sum = sum + target.xpoints[i] * target.ypoints[(i + 1) % target.npoints]
                    - target.ypoints[i] * target.xpoints[(i + 1) % target.npoints];
        }

        return Math.abs(sum / 2);
    }
}

Related

  1. binarize(BufferedImage original)
  2. binary(BufferedImage src)
  3. blackAndWhiteCleaning(BufferedImage image)
  4. boostBufferedImagePerformance(BufferedImage image, boolean translucent)
  5. buildColorStatisticsOfImage(BufferedImage image)
  6. buildPixelAverages(BufferedImage a, Rectangle[] sectors)
  7. buildSectors(BufferedImage a, int sqrtSectors)
  8. cascadeHorizontal(final BufferedImage... images)
  9. changeContrast(BufferedImage img, float amount)