Created
June 29, 2019 11:35
-
-
Save geakstr/17aedc29a407c1017b892e41b778e412 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.Color; | |
import java.awt.Graphics2D; | |
import java.awt.image.BufferedImage; | |
import java.io.File; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
import javax.imageio.ImageIO; | |
public class Main2 { | |
final static int SCALE_HEIGHT = 50; | |
final static Color red = new Color(244, 39, 19); | |
final static Color black = new Color(0, 0, 0); | |
final static Color yellow = new Color(255, 238, 11); | |
final static Color white = new Color(255, 255, 244); | |
final static Color blue = new Color(64, 107, 213); | |
final static Color grey = new Color(132, 134, 143); | |
final static List<Color> predefinedColors = Arrays.asList(red, black, yellow, white, blue, grey); | |
final static String predefinedColorsNames[] = { "red", "black", "yellow", "white", "blue", "grey" }; | |
BufferedImage readImage(String path) throws IOException { | |
BufferedImage originalImage = ImageIO.read(new File(path)); | |
return scale(originalImage, originalImage.getWidth(), SCALE_HEIGHT); | |
} | |
BufferedImage scale(BufferedImage imageToScale, int dWidth, int dHeight) { | |
BufferedImage scaledImage = null; | |
if (imageToScale != null) { | |
scaledImage = new BufferedImage(dWidth, dHeight, imageToScale.getType()); | |
Graphics2D graphics2D = scaledImage.createGraphics(); | |
graphics2D.drawImage(imageToScale, 0, 0, dWidth, dHeight, null); | |
graphics2D.dispose(); | |
} | |
return scaledImage; | |
} | |
double calcDistanceBetweenColors(Color a, Color b) { | |
return Math.sqrt(Math.pow(a.getRed() - b.getRed(), 2) + Math.pow(a.getGreen() - b.getGreen(), 2) | |
+ Math.pow(a.getBlue() - b.getBlue(), 2)); | |
} | |
List<String> detectColors() throws IOException { | |
BufferedImage image = readImage("/Users/geakstr/bomb_wires_3.png"); | |
int width = image.getWidth(); | |
int height = image.getHeight(); | |
Map<String, Integer> colorsStatMap = new HashMap<String, Integer>(); | |
for (int x = 0; x < width; x++) { | |
String lastColorName = "grey"; | |
ArrayList<String> finalColors = new ArrayList<String>(); | |
for (int y = 0; y < height; y++) { | |
Color pixelColor = new Color(image.getRGB(x, y)); | |
double minColorsDistance = Integer.MAX_VALUE; | |
int matchedPredefinedColorIdx = -1; | |
for (int predefinedColorIdx = 0; predefinedColorIdx < 6; predefinedColorIdx++) { | |
Color predefinedColor = predefinedColors.get(predefinedColorIdx); | |
double distance = calcDistanceBetweenColors(pixelColor, predefinedColor); | |
if (distance < minColorsDistance) { | |
minColorsDistance = distance; | |
matchedPredefinedColorIdx = predefinedColorIdx; | |
} | |
} | |
String pixelColorName = predefinedColorsNames[matchedPredefinedColorIdx]; | |
if (pixelColorName != lastColorName && pixelColorName != "grey") { | |
finalColors.add(pixelColorName); | |
} | |
lastColorName = pixelColorName; | |
} | |
if (finalColors.size() <= 6) { | |
String foundColors = String.join(",", finalColors); | |
if (colorsStatMap.containsKey(foundColors)) { | |
colorsStatMap.put(foundColors, colorsStatMap.get(foundColors) + 1); | |
} else { | |
colorsStatMap.put(foundColors, 1); | |
} | |
} | |
} | |
int bestColorsCount = Integer.MIN_VALUE; | |
String bestColors = ""; | |
for (Map.Entry<String, Integer> colorsToCount : colorsStatMap.entrySet()) { | |
if (bestColorsCount < colorsToCount.getValue()) { | |
bestColorsCount = colorsToCount.getValue(); | |
bestColors = colorsToCount.getKey(); | |
} | |
} | |
return Arrays.asList(bestColors.split("\\s*,\\s*")); | |
} | |
Map<String, Integer> calcColors(List<String> colors) { | |
Map<String, Integer> map = new HashMap<String, Integer>(); | |
for (int i = 0 ; i < colors.size(); i++) { | |
String color = colors.get(i); | |
} | |
return map; | |
} | |
void solve(List<String> colors) { | |
if (colors.size() == 3) { | |
} | |
} | |
void run() throws IOException { | |
solve(detectColors()); | |
} | |
public static void main(String[] args) throws IOException { | |
new Main2().run(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment