gpt4 book ai didi

java - 确定图像是否具有特定的色谱

转载 作者:行者123 更新时间:2023-12-02 07:47:30 25 4
gpt4 key购买 nike

我想知道目前是否有用 Java 编写的算法来确定图像中是否包含少量不同的像素颜色。

我正在尝试检测占位符图像(通常由高比例的单色(通常是白色和灰色像素)组成,而不是全彩照片(由多种颜色组成)。

如果什么都不存在,我会自己写一些东西(正在考虑对图像中随机位置的任意像素进行采样或对图像中包含的所有像素颜色进行平均),然后确定我找到的不同颜色的数量。根据所使用的方法,速度和准确性之间可能需要权衡。

任何建议/指示/阅读 Material 表示赞赏。

最佳答案

一种方法是:

final BufferedImage image = // your image;
final Set<Integer> colours = new HashSet<Integer>();

for (int x = 0; x < image.getWidth(); x++) {
for (int y = 0; y < image.getHeight(); y++) {
colours.add(image.getRGB(x, y));
}
}

// Check your pixels here. In grayscale images the R equals G equals B

您还可以使用 Java Advanced Imaging(JAI),因为它提供了直方图类:

package com.datroop.histogram;

import java.awt.image.renderable.ParameterBlock;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;

import javax.media.jai.Histogram;
import javax.media.jai.JAI;
import javax.media.jai.PlanarImage;
import javax.media.jai.ROI;
import javax.media.jai.RenderedOp;

public class HistogramCreator {

private HistogramCreator() {
}

public static int[] createHistogram(final PlanarImage image) {
// set up the histogram
final int[] bins = { 256 };
final double[] low = { 0.0D };
final double[] high = { 256.0D };

Histogram histogram = new Histogram(bins, low, high);

final ParameterBlock pb = new ParameterBlock();

pb.addSource(image);
pb.add(null);
pb.add(1);
pb.add(1);

final RenderedOp op = JAI.create("histogram", pb);
histogram = (Histogram) op.getProperty("histogram");

// get histogram contents
final int[] local_array = new int[histogram.getNumBins(0)];

for (int i = 0; i < histogram.getNumBins(0); i++) {
local_array[i] = histogram.getBinSize(0, i);
}

return local_array;
}

public static void main(String[] args) {
try {
String filename = "file://localhost/C:/myimage.jpg";
System.setProperty("com.sun.media.jai.disableMediaLib", "true");
// Create the histogram
int[] myHistogram = createHistogram(JAI.create("url", new URL(filename)));
// Check it out here
System.out.println(Arrays.toString(myHistogram));
} catch (MalformedURLException e) {
e.printStackTrace();
}
}

}

关于java - 确定图像是否具有特定的色谱,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10633565/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com