我有一个程序,可以扫描图像的某些像素并将其与其他RGB颜色进行比较,并告诉我最近的颜色是什么。我在查找NearestColor函数时遇到问题。现在,我得到了一个非常快的结果,但是我发现每次给出的结果都不一样。我连续扫描了4个像素,结果有4种不同。
Hellgrau (light gray)
Schwarz (black)
Dunkelgrau (dark gray)
Dunkelpink (dark pink)
顺便说一下,它是一个黑色像素,具有 rgb(0,0,0);
您知道问题出在哪里吗:Link to code
import java.awt.Color;
import java.util.ArrayList;
/**
* Java Code to get a color name from rgb/hex value/awt color
*
* The part of looking up a color name from the rgb values is edited from
* https://gist.github.com/nightlark/6482130#file-gistfile1-java (that has some errors) by Ryan Mast (nightlark)
*
* @author Xiaoxiao Li
*
*/
public class ColorUtils {
/**
* Initialize the color list that we have.
*/
private ArrayList<ColorName> initColorList() {
ArrayList<ColorName> colorList = new ArrayList<ColorName>();
colorList.add(new ColorName("Hellgrau", 0xC1, 0xC1, 0xC1));
colorList.add(new ColorName("Rot", 0xEF, 0x13, 0x0B));
colorList.add(new ColorName("Orange", 0xFF, 0x71, 0x00));
colorList.add(new ColorName("Gelb", 0xFF, 0xE4, 0x00));
colorList.add(new ColorName("Grün", 0x00, 0xCC, 0x00));
colorList.add(new ColorName("Hellblau", 0x00, 0xB2, 0xFF));
colorList.add(new ColorName("Mondblau", 0x23, 0x1F, 0xD3));
colorList.add(new ColorName("Lila", 0xA3, 0x00, 0xBA));
colorList.add(new ColorName("Pink", 0xD3, 0x7C, 0xAA));
colorList.add(new ColorName("Hellbraun", 0xA0, 0x52, 0x2D));
colorList.add(new ColorName("Schwarz", 0x00, 0x00, 0x00));
colorList.add(new ColorName("Dunkelgrau", 0x4C, 0x4C, 0x4C));
colorList.add(new ColorName("Dunkelrot", 0x74, 0x0B, 0x07));
colorList.add(new ColorName("Dunkelorange", 0xC2, 0x38, 0x00));
colorList.add(new ColorName("Dunkelgelb", 0xE8, 0xA2, 0x00));
colorList.add(new ColorName("Dunkelgrün", 0x00, 0x55, 0x10));
colorList.add(new ColorName("Blau", 0x00, 0x56, 0x9E));
colorList.add(new ColorName("Dunkelblau", 0x0E, 0x08, 0x65));
colorList.add(new ColorName("Dunkellila", 0x55, 0x00, 0x69));
colorList.add(new ColorName("Dunkelpink", 0xA7, 0x55, 0x74));
colorList.add(new ColorName("Dunkelbraun", 0x63, 0x30, 0x0D));
return colorList;
}
/**
* Get the closest color name from our list
*
* @param r
* @param g
* @param b
* @return
*/
public String getColorNameFromRgb(int r, int g, int b) {
ArrayList<ColorName> colorList = initColorList();
ColorName closestMatch = null;
int minMSE = Integer.MAX_VALUE;
int mse;
for (ColorName c : colorList) {
mse = c.computeMSE(r, g, b);
if (mse < minMSE) {
minMSE = mse;
closestMatch = c;
}
}
if (closestMatch != null) {
return closestMatch.getName();
} else {
return "No matched color name.";
}
}
/**
* Convert hexColor to rgb, then call getColorNameFromRgb(r, g, b)
*
* @param hexColor
* @return
*/
public String getColorNameFromHex(int hexColor) {
int r = (hexColor & 0xFF0000) >> 16;
int g = (hexColor & 0xFF00) >> 8;
int b = (hexColor & 0xFF);
return getColorNameFromRgb(r, g, b);
}
public int colorToHex(Color c) {
return Integer.decode("0x"
+ Integer.toHexString(c.getRGB()).substring(2));
}
public String getColorNameFromColor(Color color) {
return getColorNameFromRgb(color.getRed(), color.getGreen(),
color.getBlue());
}
/**
* SubClass of ColorUtils. In order to lookup color name
*
* @author Xiaoxiao Li
*
*/
public class ColorName {
public int r, g, b;
public String name;
public ColorName(String name, int r, int g, int b) {
this.r = r;
this.g = g;
this.b = b;
this.name = name;
}
public int computeMSE(int pixR, int pixG, int pixB) {
return (int) (((pixR - r) * (pixR - r) + (pixG - g) * (pixG - g) + (pixB - b)
* (pixB - b)) / 3);
}
public int getR() {
return r;
}
public int getG() {
return g;
}
public int getB() {
return b;
}
public String getName() {
return name;
}
}
}
粘贴的相关类:基本上Iam在这里读取图像并进行比较。有点混乱,但Iam专注于使其正常工作。
public class ScanWrite {
int alpha = 0;
int red = 0;
int green = 0;
int blue = 0;
int x, y;
int width;
int height;
int delay = 0;
String nearestColor = "";
static boolean cancel = false;
String rgbcolor = "";
String[] Farben = {"Weiß","Hellgrau","Rot","Orange","Gelb","Grün","Hellblau","Mondblau","Lila","Pink","Hellbraun","Schwarz","Dunkelgrau","Dunkelrot","Dunkelorange","Dunkelgelb","Dunkelgrün","Blau","Dunkelblau","Dunkellila","Dunkelpink","Dunkelbraun"};
int[] FarbenX = {0,577,602,627,655,678,706,736,760,786,814,550,575,601,628,655,681,708,736,761,785,814};
int[] FarbenY = {0,927,927,927,927,927,927,927,927,927,927,953,953,953,953,953,953,953,953,953,953,953};
int[] redc = {255,193,239,255,255,0,0,35,163,211,160,0,76,116,194,232,0,0,14,85,167,99};
int[] greenc = {255,193,19,113,228,204,178,31,0,124,82,0,76,11,56,162,85,86,8,0,85,48};
int[] bluec = {255,193,11,0,0,0,255,211,186,170,45,0,76,7,0,0,16,158,101,105,116,13};
void scanandwrite(int delay, File file) throws AWTException, InterruptedException {
ColorUtils color = new ColorUtils();
BufferedImage photo = GetImage.bufferImage(file);
FastRGB getPixel = new FastRGB(photo);
int[][] pixels = new int[photo.getWidth()][photo.getHeight()];
//int pixel = (getPixel.getRGB(photo.getMinX(), photo.getMinY())>>24 & 0xff);
width = photo.getWidth();
height = photo.getHeight();
MouseMover.goStart(); //mouse cursor to a stated position
int i = 0; //it is the x position of the image
int j = 0; //it is the y position of the image
for(int k = 0; k < Farben.length; k++) { //loops to 22 colors
if(cancel == false) { //checks if I pressed the cancel button
MouseMover.selectColor(FarbenX[k], FarbenY[k]); //moves the cursor to the position where the stated color is
i = 0; j = 0;
while(i < width) { //counts x up
if(i == width-1 && j < height-3) { //if x is at the end
j+=2; //go 2 lines down (y)
i = 0; //x set to 0
}
if(this.delay > delay) { //delay I build in
pixels[i][j] = getPixel.getRGB(i, j); //gets the pixel from the position
//System.out.println(i + ", " + j);
alpha = (pixels[i][j]>>24) & 0xff;
red = (pixels[i][j]>>16) & 0xff;
green = (pixels[i][j]>>8) & 0xff;
blue = pixels[i][j] & 0xff;
rgbcolor = color.getColorNameFromRgb(red, green, blue); //gets the nearest color of the rgb
if(!rgbcolor.matches("Weiß")) {
if(rgbcolor.matches(Farben[k])) { //if rgbcolor has the same name as Farben[k] Iam looping through
if(red != 255 && green != 255 && blue != 255 && red != 0 && green != 0 && blue != 0) {
if(cancel == false) { //checks again if I cancelled
System.out.println(rgbcolor);
//MouseMover.drawPixel(i, j); //goes to the screen and draws
}
}
}
}
this.delay = 0; //delay set to 0 after loop
}
this.delay++; //here the delay counts up
i++; //y counts up
}
}
}
}
}
更新:我认为我昨天在声明问题时犯了一个错误。我将在此处粘贴图像,并提供每次扫描得到的输出。
输出:
20x Hellgrau (light gray)
23x Schwarz (black)
7x Dunkelgrau (dark gray)
29x Dunkelpink (dark pink)