Made use of Point2D.Double in ImageScanner and PointInterpolator

This commit is contained in:
Paul Schaller 2023-11-20 14:32:45 +01:00
parent 8f98f3eea6
commit d322503403
2 changed files with 21 additions and 22 deletions

View File

@ -1,15 +1,14 @@
package com.cringe_studios.christmastreescanning.scanning; package com.cringe_studios.christmastreescanning.scanning;
import java.awt.Point; import java.awt.geom.Point2D;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class ImageScanner { public class ImageScanner {
public static final byte GAMMA_DELTA_DEFAULT = 0; public static final byte GAMMA_DELTA_DEFAULT = 0;
// TODO maybe use Point2D.Double as return type, so the Points don't have to be
// converted to Points2D later down the pipeline during the normalization step static private Point2D getBrightestSpotGrayscale(BufferedImage grayScaleImage, byte gammaDelta) {
static private Point getBrightestSpotGrayscale(BufferedImage grayScaleImage, byte gammaDelta) {
int[] imageColors = grayScaleImage.getRGB(0, 0, grayScaleImage.getWidth(), grayScaleImage.getHeight(), new int[grayScaleImage.getWidth() * grayScaleImage.getHeight()], 0, 0); int[] imageColors = grayScaleImage.getRGB(0, 0, grayScaleImage.getWidth(), grayScaleImage.getHeight(), new int[grayScaleImage.getWidth() * grayScaleImage.getHeight()], 0, 0);
for (int i = 0; i < imageColors.length; i++) { for (int i = 0; i < imageColors.length; i++) {
@ -33,11 +32,11 @@ public class ImageScanner {
} }
// The following line might be incorrect; TODO double check and write a test // The following line might be incorrect; TODO double check and write a test
Point brightPoints[] = brightSpots.stream() Point2D brightPoints[] = brightSpots.stream()
.map(s -> new Point(s.intValue() % grayScaleImage.getWidth(), s.intValue() / grayScaleImage.getWidth())) .map(s -> new Point2D.Double(s.intValue() % grayScaleImage.getWidth(), s.intValue() / grayScaleImage.getWidth()))
.toArray(Point[]::new); .toArray(Point2D.Double[]::new);
Point brightestSpot = new PointInterpolator(brightPoints).getCenter(); Point2D brightestSpot = new PointInterpolator(brightPoints).getCenter();
return brightestSpot; return brightestSpot;
} }
@ -46,11 +45,11 @@ public class ImageScanner {
// pixel included in the search set for the brightest spot // pixel included in the search set for the brightest spot
// The brightest point Operation is done per color dimension, so you could // The brightest point Operation is done per color dimension, so you could
// provide different images for red, green and blue brightest points // provide different images for red, green and blue brightest points
static public Point getBrightestSpot(BufferedImage redImage, BufferedImage greenImage, BufferedImage blueImage, static public Point2D getBrightestSpot(BufferedImage redImage, BufferedImage greenImage, BufferedImage blueImage,
byte gammaDelta) { byte gammaDelta) {
Point brightestRedSpot = getBrightestSpotGrayscale(redImage, gammaDelta); Point2D brightestRedSpot = getBrightestSpotGrayscale(redImage, gammaDelta);
Point brightestGreenSpot = getBrightestSpotGrayscale(greenImage, gammaDelta); Point2D brightestGreenSpot = getBrightestSpotGrayscale(greenImage, gammaDelta);
Point brightestBlueSpot = getBrightestSpotGrayscale(blueImage, gammaDelta); Point2D brightestBlueSpot = getBrightestSpotGrayscale(blueImage, gammaDelta);
double RGdistance = brightestRedSpot.distanceSq(brightestGreenSpot); double RGdistance = brightestRedSpot.distanceSq(brightestGreenSpot);
double GBdistance = brightestGreenSpot.distanceSq(brightestBlueSpot); double GBdistance = brightestGreenSpot.distanceSq(brightestBlueSpot);
@ -71,7 +70,7 @@ public class ImageScanner {
return brightestSpot.getCenter(); return brightestSpot.getCenter();
} }
static public Point getBrightestSpot(BufferedImage image, byte gammaDelta) { static public Point2D getBrightestSpot(BufferedImage image, byte gammaDelta) {
BufferedImage redImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_GRAY); BufferedImage redImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
BufferedImage greenImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_GRAY); BufferedImage greenImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
BufferedImage blueImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_GRAY); BufferedImage blueImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
@ -92,7 +91,7 @@ public class ImageScanner {
return getBrightestSpot(redImage, greenImage, blueImage, gammaDelta); return getBrightestSpot(redImage, greenImage, blueImage, gammaDelta);
} }
static public Point getBrightestSpot(BufferedImage image) { static public Point2D getBrightestSpot(BufferedImage image) {
return getBrightestSpot(image, GAMMA_DELTA_DEFAULT); return getBrightestSpot(image, GAMMA_DELTA_DEFAULT);
} }
} }

View File

@ -1,23 +1,23 @@
package com.cringe_studios.christmastreescanning.scanning; package com.cringe_studios.christmastreescanning.scanning;
import java.awt.Point; import java.awt.geom.Point2D;
public class PointInterpolator { public class PointInterpolator {
Point points[]; Point2D points[];
public PointInterpolator(Point...na) { public PointInterpolator(Point2D...na) {
points = na; points = na;
} }
Point getCenter() { Point2D getCenter() {
int xSum = 0; int xSum = 0;
int ySum = 0; int ySum = 0;
for(int i = 0; i < points.length; i++) { for(int i = 0; i < points.length; i++) {
xSum += points[i].x; xSum += points[i].getX();
ySum += points[i].y; ySum += points[i].getY();
} }
return new Point( xSum / points.length, ySum / points.length); return new Point2D.Double( xSum / points.length, ySum / points.length);
} }
} }