Java nearestColor扫描每次给出不同的结果
我有一个程序,它扫描图像的一些像素,并将其与其他RGB颜色进行比较,告诉我最接近的颜色是什么。我找不到最接近的颜色函数。现在我有一个非常快,但我发现它每次都会给出不同的结果。我连续扫描了一个像素4次,得到了4个不同的结果Java nearestColor扫描每次给出不同的结果,java,Java,我有一个程序,它扫描图像的一些像素,并将其与其他RGB颜色进行比较,告诉我最接近的颜色是什么。我找不到最接近的颜色函数。现在我有一个非常快,但我发现它每次都会给出不同的结果。我连续扫描了一个像素4次,得到了4个不同的结果 Hellgrau (light gray) Schwarz (black) Dunkelgrau (dark gray) Dunkelpink (dark pink) 顺便说一下,它是一个黑色像素,具有rgb(0,0,0) 你知道问题是什么吗 在这里粘贴代码我只希望它不是ne
Hellgrau (light gray)
Schwarz (black)
Dunkelgrau (dark gray)
Dunkelpink (dark pink)
顺便说一下,它是一个黑色像素,具有rgb(0,0,0)强>
你知道问题是什么吗
在这里粘贴代码我只希望它不是
nearestColor=Random.nextInt()代码>:)返回(int)((pixR-r)*(pixR-r)+(pixG-g)*(pixG-g)+(pixB-b)*(pixB-b))/3)这将计算3D空间中的标准化距离。->半径为的球体上的所有颜色都将匹配。(如果将红、绿和蓝作为3个轴。但是它看起来很好,因为它寻找的是仍然保持给定颜色的最小球体。我可以看看调用此方法的部分吗?ColorUtils color=new ColorUtils();String rgbcolor=color.getColorNameFromRgb(红、绿、蓝);System.out.println(rgbcolor);
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;
}
}
}
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)