java - 在 JAVA 中将图像减少到固定数量的给定颜色
问题描述
我想以一种特殊的方式编辑图像:
我已经给出了 40 种颜色的列表。现在我有一张所有可能颜色的图像。我想将图像减少到只有 12 种颜色(40 种可能的颜色)并获得最佳效果。是否有一个好的库来存档(JAVA)。
解决方案
在这个答案中,我做出以下假设:
- 您的列表包含
40
不同的颜色。 - 每种颜色都包含
24
信息8
位(红色8
位,绿色8
位和蓝色位)。 - -color
12
解决方案应该是人类感知颜色的最佳组合。
因为您的目标是确定哪些12
颜色(40
提供的)最接近图像中的所有颜色,所以您可以使用以下算法:
- 遍历所有
40
输入颜色。 - 对于每种颜色,迭代输入图像中的所有像素。
- 对于当前像素,计算其颜色与我们当前颜色之间的差异。
- 将颜色映射到相应颜色的差异总和。
- 按值排序
Map<Integer, Double>
(升序);解决办法是第一把12
钥匙。
这是实现此目的的 Java 代码:
import javax.imageio.ImageIO;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class ColorDifference {
public static void main(String[] args) {
ConcurrentMap<Color, Double> colorDifferenceMap = new ConcurrentHashMap<>();
ExecutorService executorService = Executors.newFixedThreadPool(
Runtime.getRuntime().availableProcessors());
BufferedImage inputImage;
try {
// Read in the input image
inputImage = ImageIO.read(Paths.get("input.png").toFile());
} catch (IOException e) {
throw new UncheckedIOException("Unable to read input image!", e);
}
generateInfiniteColors().distinct().limit(40).forEach(color -> {
executorService.execute(() -> {
CIELab cieLabColor = CIELab.from(color);
double sum = 0d;
for (int y = 0; y < inputImage.getHeight(); y++) {
for (int x = 0; x < inputImage.getWidth(); x++) {
Color pixelColor = new Color(inputImage.getRGB(x, y));
CIELab pixelCIELabColor = CIELab.from(pixelColor);
sum += cieLabColor.difference(pixelCIELabColor);
}
}
colorDifferenceMap.put(color, sum);
});
});
executorService.shutdown();
try {
executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
} catch (InterruptedException e) {
e.printStackTrace();
}
// The 12 solution colors are held in this list
List<Color> colorSolutions = colorDifferenceMap.entrySet()
.stream()
.sorted(Map.Entry.comparingByValue())
.limit(12)
.map(Map.Entry::getKey)
.collect(Collectors.toList());
colorSolutions.forEach(System.out::println);
for (int y = 0; y < inputImage.getHeight(); y++) {
for (int x = 0; x < inputImage.getWidth(); x++) {
CIELab cieLabColor = CIELab.from(new Color(inputImage.getRGB(x, y)));
int finalX = x;
int finalY = y;
colorSolutions.stream()
.min(Comparator.comparingDouble(color ->
cieLabColor.difference(CIELab.from(color))))
.ifPresent(closestColor ->
inputImage.setRGB(finalX, finalY, closestColor.getRGB()));
}
}
try {
ImageIO.write(inputImage, "png", new File("output.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
// Inspiration taken from https://stackoverflow.com/a/20032024/7294647
private static Stream<Color> generateInfiniteColors() {
return Stream.generate(() ->
new Color(ThreadLocalRandom.current().nextInt(0x1000000)));
}
static class CIELab {
private final double L, a, b;
public CIELab(double L, double a, double b) {
this.L = L;
this.a = a;
this.b = b;
}
public double difference(CIELab cieLab) {
return Math.sqrt(Math.pow(cieLab.L - L, 2) + Math.pow(cieLab.a - a, 2) +
Math.pow(cieLab.b - b, 2));
}
public static CIELab from(Color color) {
int sR = color.getRed();
int sG = color.getGreen();
int sB = color.getBlue();
// Convert Standard-RGB to XYZ (http://www.easyrgb.com/en/math.php)
double var_R = ( sR / 255d );
double var_G = ( sG / 255d );
double var_B = ( sB / 255d );
if ( var_R > 0.04045 ) var_R = Math.pow( ( var_R + 0.055 ) / 1.055, 2.4 );
else var_R = var_R / 12.92;
if ( var_G > 0.04045 ) var_G = Math.pow( ( var_G + 0.055 ) / 1.055, 2.4 );
else var_G = var_G / 12.92;
if ( var_B > 0.04045 ) var_B = Math.pow( ( var_B + 0.055 ) / 1.055, 2.4 );
else var_B = var_B / 12.92;
var_R = var_R * 100;
var_G = var_G * 100;
var_B = var_B * 100;
double X = var_R * 0.4124 + var_G * 0.3576 + var_B * 0.1805;
double Y = var_R * 0.2126 + var_G * 0.7152 + var_B * 0.0722;
double Z = var_R * 0.0193 + var_G * 0.1192 + var_B * 0.9505;
// Convert XYZ to CIELAB (http://www.easyrgb.com/en/math.php
double var_X = X / 96.422;
double var_Y = Y / 100.000;
double var_Z = Z / 82.521;
if ( var_X > 0.008856 ) var_X = Math.pow( var_X, 1D / 3D );
else var_X = ( 7.787 * var_X ) + ( 16D / 116 );
if ( var_Y > 0.008856 ) var_Y = Math.pow( var_Y, 1D / 3D );
else var_Y = ( 7.787 * var_Y ) + ( 16D / 116 );
if ( var_Z > 0.008856 ) var_Z = Math.pow( var_Z, 1D / 3D );
else var_Z = ( 7.787 * var_Z ) + ( 16D / 116 );
double L = ( 116 * var_Y ) - 16;
double a = 500 * ( var_X - var_Y );
double b = 200 * ( var_Y - var_Z );
return new CIELab(L, a, b);
}
}
}
473x356
为了测试代码,我从您在评论中发送的链接中抓取了以下图片:
您可以在代码中看到所有40
颜色都是随机生成的,这是程序的输出:
java.awt.Color[r=182,g=176,b=32]
java.awt.Color[r=201,g=201,b=55]
java.awt.Color[r=142,g=149,b=6]
java.awt.Color[r=223,g=158,b=36]
java.awt.Color[r=226,g=143,b=63]
java.awt.Color[r=144,g=83,b=39]
java.awt.Color[r=119,g=153,b=117]
java.awt.Color[r=50,g=136,b=72]
java.awt.Color[r=244,g=118,b=59]
java.awt.Color[r=69,g=79,b=52]
java.awt.Color[r=149,g=78,b=76]
java.awt.Color[r=62,g=190,b=28]
一种可能的输出图像如下:
上面的代码可以在 Java 8+ 上运行,对于上面发布的图像,它在一秒钟内完成。
推荐阅读
- xamarin - (Android & Ios) Webview 选择文本并突出显示
- heroku - 为什么有时我必须在 heroku 中指定我要定位的应用程序?
- python - Django:如何将上下文变量作为 src 添加到 url?
- koa - Koa - 捕获所有以前缀开头的路由
- c++ - 编译出现错误且找不到文件的 C++ 程序
- python - Python:使用尽可能少的数据在客户端和服务器上保持文本文件同步
- javascript - 当自定义元素没有值时,CSS 隐藏祖父 div 和所有子元素
- sh - sh:如何使用 jq 将数组写入 json 文件
- javascript - 在 GraphQL 中获取父级的子参数
- azure-data-factory-2 - Azure ADF:我可以手动设置(或以其他方式强制)管道的最终“状态”为“成功”吗?