处理如何从颜色集合中获取最接近的颜色
Processing how to get the nearest color from a Collection of colors
我有一个 Collection of Integers
处理颜色(它包含重新缩放为 1x1 以获得 "average" 颜色的图像颜色)。
我有这个东西必须检索数组中最接近的颜色:
public static int getNearestColor(Collection<Integer> colors, int color) {
return colors.stream()
.min(Comparator.comparingInt(i -> Math.abs(i - color)))
.orElseThrow(() -> new NoSuchElementException("No value present"));
}
但是当我这样做时,它 returns 我的颜色比输入 远 ,但是数组包含一些比输入最近的颜色,这个是我不明白的问题?
java 中的数组没有流方法;也许你的意思是 Arrays.stream(colors)
。 IntStream
除了按自然顺序进行比较外,没有办法进行比较。你可以先映射到差异(abs(i - color)
),但是现在你已经删除了你要找的信息(原来的颜色),这样也不起作用。让我们装箱吧。这会产生以下代码,除了编译和运行之外,它与您的代码完全一样。然后我还会添加一个测试用例,使其成为一个独立的示例:
int[] colors = {1,4,5,9,12};
int target = 6;
int a = Arrays.stream(colors).boxed()
.min(Comparator.comparingInt(i -> Math.abs(i - target)))
.orElseThrow(() -> new NoSuchElementException());
System.out.println(a);
而且,哇哦,nelly,'5' 掉了,这正是您想要的。
换句话说,您的代码的意图是好的,如果它没有给出正确的答案,您的输入不是您认为的那样,或者其他无法从您的粘贴中收集到的错误。
我是否可以建议,如果完全有可能将问题放在一个简单的、自包含的形式中(因为这个问题很明显,请参阅此答案中的代码片段),你会这样做吗?通常你会这样回答你自己的问题:)
public static int getNearestColor(int[] colors, int color) {
int minDiff = IntStream.of(colors)
.map(val -> Math.abs(val - color))
.min()
.getAsInt();
OptionalInt num = IntStream.of(colors)
.filter(val-> val==(color + minDiff))
.findFirst();
if(num.isPresent()){
return color + minDiff;
} else {
return color - minDiff;
}
}
一个color()
are encoded in an int
. You can extract the red, green and blue component of the color, by red()
, green()
and blue()
的RGB颜色通道。
将颜色通道视为 3 维向量 (PVector
) and compute the Euclidean distance of 2 color vectors, by dist()
。具有最短 "distance" 的颜色是 "nearest" 颜色:
在下面的函数中,参数 c1
和 c2
是颜色类型 int
:
float ColorDistance(int c1, int c2) {
return PVector.dist(
new PVector(red(c1), green(c1), blue(c1)),
new PVector(red(c2), green(c2), blue(c2)));
}
通过找到最小浮点数 "distance" (ColorDistance(i, color)
),在颜色集合中找到 "nearest" 颜色。
我有一个 Collection of Integers
处理颜色(它包含重新缩放为 1x1 以获得 "average" 颜色的图像颜色)。
我有这个东西必须检索数组中最接近的颜色:
public static int getNearestColor(Collection<Integer> colors, int color) {
return colors.stream()
.min(Comparator.comparingInt(i -> Math.abs(i - color)))
.orElseThrow(() -> new NoSuchElementException("No value present"));
}
但是当我这样做时,它 returns 我的颜色比输入 远 ,但是数组包含一些比输入最近的颜色,这个是我不明白的问题?
java 中的数组没有流方法;也许你的意思是 Arrays.stream(colors)
。 IntStream
除了按自然顺序进行比较外,没有办法进行比较。你可以先映射到差异(abs(i - color)
),但是现在你已经删除了你要找的信息(原来的颜色),这样也不起作用。让我们装箱吧。这会产生以下代码,除了编译和运行之外,它与您的代码完全一样。然后我还会添加一个测试用例,使其成为一个独立的示例:
int[] colors = {1,4,5,9,12};
int target = 6;
int a = Arrays.stream(colors).boxed()
.min(Comparator.comparingInt(i -> Math.abs(i - target)))
.orElseThrow(() -> new NoSuchElementException());
System.out.println(a);
而且,哇哦,nelly,'5' 掉了,这正是您想要的。
换句话说,您的代码的意图是好的,如果它没有给出正确的答案,您的输入不是您认为的那样,或者其他无法从您的粘贴中收集到的错误。
我是否可以建议,如果完全有可能将问题放在一个简单的、自包含的形式中(因为这个问题很明显,请参阅此答案中的代码片段),你会这样做吗?通常你会这样回答你自己的问题:)
public static int getNearestColor(int[] colors, int color) {
int minDiff = IntStream.of(colors)
.map(val -> Math.abs(val - color))
.min()
.getAsInt();
OptionalInt num = IntStream.of(colors)
.filter(val-> val==(color + minDiff))
.findFirst();
if(num.isPresent()){
return color + minDiff;
} else {
return color - minDiff;
}
}
一个color()
are encoded in an int
. You can extract the red, green and blue component of the color, by red()
, green()
and blue()
的RGB颜色通道。
将颜色通道视为 3 维向量 (PVector
) and compute the Euclidean distance of 2 color vectors, by dist()
。具有最短 "distance" 的颜色是 "nearest" 颜色:
在下面的函数中,参数 c1
和 c2
是颜色类型 int
:
float ColorDistance(int c1, int c2) {
return PVector.dist(
new PVector(red(c1), green(c1), blue(c1)),
new PVector(red(c2), green(c2), blue(c2)));
}
通过找到最小浮点数 "distance" (ColorDistance(i, color)
),在颜色集合中找到 "nearest" 颜色。