You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java逆选择排序实现图像像素亮度排序失效问题求助

修复逆选择排序对图像像素亮度排序的问题

你的代码存在几个关键错误,导致处理后图像没有变化,以下是问题分析和修改方案:

关键问题分析

  • 缺失pixelBrightness方法:代码中调用了该方法计算像素亮度,但未实现,无法正确获取亮度值。
  • 缺失selectionSort方法:核心的排序逻辑未实现,亮度列表完全没有被排序,自然图像无变化。
  • 对角线遍历初始化错误:int i = Math.min(0, image.getWidth() - 1)会让i始终为0,导致仅遍历第一列的对角线,大部分像素未被处理,应该改为int i = Math.min(m, image.getWidth() - 1)。
  • 亮度转RGB逻辑问题:直接将亮度值作为红、绿、蓝分量时,未确保数值在0-255区间,容易出现异常;同时原逻辑未考虑亮度计算的标准公式,导致亮度值不准确。

修改后的完整代码

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

public class Sort {
    private BufferedImage image;
    private int rgb;
    int iterationCount;

    public Sort(BufferedImage image) {
        this.image = image;
        this.iterationCount = 0;
    }

    public void ChainListWithBrightness(BufferedImage image) {
        double[][] points = new double[image.getWidth()][image.getHeight()];
        for (int i = 0; i < image.getWidth(); i++) {
            for (int j = 0; j < image.getHeight(); j++) {
                rgb = image.getRGB(i, j);
                points[i][j] = pixelBrightness(rgb);
            }
        }
        diagonalListingOfPoints(points);
    }

    // 实现标准像素亮度计算方法
    private double pixelBrightness(int rgb) {
        Color color = new Color(rgb);
        // 采用ITU-R BT.601标准亮度公式,结果范围0-255
        return 0.299 * color.getRed() + 0.587 * color.getGreen() + 0.114 * color.getBlue();
    }

    private void diagonalListingOfPoints(double[][] points) {
        ArrayList<Double> diagonalBrightnessValues = new ArrayList<>();
        for (int m = 0; m < image.getWidth() + image.getHeight() - 1; m++) {
            // 修正对角线遍历的i初始值,确保遍历所有对角线像素
            int i = Math.min(m, image.getWidth() - 1);
            int j = Math.max(0, m - image.getWidth() + 1);
            while (i >= 0 && j < image.getHeight()) {
                diagonalBrightnessValues.add(points[i][j]);
                i--;
                j++;
            }
        }
        // 执行逆选择排序
        selectionSort(diagonalBrightnessValues);

        // 将排序后的亮度值写回数组
        int index = 0;
        for (int m = 0; m < image.getWidth() + image.getHeight() - 1; m++) {
            int i = Math.min(m, image.getWidth() - 1);
            int j = Math.max(0, m - image.getWidth() + 1);
            while (i >= 0 && j < image.getHeight()) {
                points[i][j] = diagonalBrightnessValues.get(index);
                index++;
                i--;
                j++;
            }
        }

        // 将亮度值转换为灰度像素并设置回图像
        for (int i = 0; i < image.getWidth(); i++) {
            for (int j = 0; j < image.getHeight(); j++) {
                int brightness = (int) Math.round(points[i][j]);
                // 确保亮度值在0-255合法范围内
                brightness = Math.max(0, Math.min(255, brightness));
                Color newColor = new Color(brightness, brightness, brightness);
                image.setRGB(i, j, newColor.getRGB());
            }
        }
        iterationCount++;
    }

    // 实现逆选择排序(降序排列亮度值)
    private void selectionSort(ArrayList<Double> list) {
        int n = list.size();
        for (int i = 0; i < n - 1; i++) {
            // 找到当前未排序段的最大值索引
            int maxIndex = i;
            for (int j = i + 1; j < n; j++) {
                if (list.get(j) > list.get(maxIndex)) {
                    maxIndex = j;
                }
            }
            // 交换当前位置与最大值位置的元素
            double temp = list.get(maxIndex);
            list.set(maxIndex, list.get(i));
            list.set(i, temp);
        }
    }

    public static void main(String[] args) throws IOException {
        // Windows路径需用双反斜杠转义
        BufferedImage img = ImageIO.read(new File("C:\\Users\\jafar\\Documents\\semester3\\cs102-1\\Lab\\lab6\\skyy.jpg"));
        Sort sort = new Sort(img);
        sort.ChainListWithBrightness(img);
        System.out.println("Sorting completed with " + sort.iterationCount + " iterations.");

        // 显示排序后的图像
        showImage(sort.image);
    }

    private static void showImage(BufferedImage image) {
        JFrame frame = new JFrame("Sorted Image");
        ImageIcon icon = new ImageIcon(image);
        JLabel label = new JLabel(icon);
        frame.add(label);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}

额外说明

  1. 路径转义:原代码中Windows文件路径使用单反斜杠会导致编译错误,已改为双反斜杠\\。
  2. 排序方向调整:若需要升序排序,只需将selectionSort方法中的list.get(j) > list.get(maxIndex)改为list.get(j) < list.get(maxIndex)。
  3. 亮度范围控制:添加了数值边界限制,避免因亮度值超出0-255范围导致Color构造函数抛出异常。

内容的提问来源于stack exchange,提问作者parinaz jafarypour

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 22:50:40