求解直方图java代码
79
2024-02-28 04:12
java
public class ImageHistogram {
public int[] calculateHistogram(int[][] image) {
int[] histogram = new int[256];
for (int i = 0; i < image.length; i++) {
for (int j = 0; j < image[i].length; j++) {
int pixelValue = image[i][j];
histogram[pixelValue]++;
}
}
return histogram;
}
public void printHistogram(int[] histogram) {
for (int i = 0; i < histogram.length; i++) {
System.out.println("灰度级别 " + i + " 的像素数量为: " + histogram[i]);
}
}
public static void main(String[] args) {
int[][] image = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}};
ImageHistogram imageHistogram = new ImageHistogram();
int[] histogram = imageHistogram.calculateHistogram(image);
imageHistogram.printHistogram(histogram);
}
}
顶一下
(0)
踩一下
(0)