图像处理学习笔记(5)—— 彩色调节与亮度调整

1 色彩调节

  • ColorAdjustFilter
package demo2.coloradjust;

import demo2.utils.AbstractImageOptionFilter;

import java.awt.image.BufferedImage;

public class ColorAdjustFilter extends AbstractImageOptionFilter {

    private float minValue;
    private float maxValue;
    private float defaultMinValue;
    private float defaultMaxValue;
    private float bvalue;
    private int[] lut;
    private boolean minmax;
    public ColorAdjustFilter() {
        defaultMinValue = 0;
        defaultMaxValue = 255;
        minValue = 0;
        maxValue = 255;
        bvalue = 20;
        minmax = true;
    }

    public void setBvalue(float bvalue) {
        this.bvalue = bvalue;
    }

    public void setMinmax(boolean minmax) {
        this.minmax = minmax;
    }

    public void setMinValue(float minValue) {
        this.minValue = minValue;
    }

    public void setMaxValue(float maxValue) {
        this.maxValue = maxValue;
    }

    @Override
    public BufferedImage process(BufferedImage src) {
        int width = src.getWidth();
        int height = src.getHeight();
        int[] pixels = new int[width * height];
        getRGB(src, 0, 0, width, height, pixels);
        int index = 0;
        if(minmax) {
            setupMinMaxLut();
        }
        else {
            setupBrightnessLut();
        }
        for (int row = 0; row < height; row++) {
            for (int col = 0; col < width; col++) {
                index = row * width + col;
                int pixel = pixels[index];
                int ta = (pixel >> 24) & 0xff; // Alpha
                int tr = (pixel >> 16) & 0xff; // red
                int tg = (pixel >> 8) & 0xff; // green
                int tb = (pixel) & 0xff; // blue

                pixels[index] = (ta << 24 | lut[tr] << 16 | lut[tg] << 8 | lut[tb]);
            }
        }
        setRGB(src, 0, 0, width, height, pixels);
        return src;
    }

    public void setupMinMaxLut() {
        float min = defaultMinValue + minValue*(defaultMaxValue-defaultMinValue)/255;
        float max = defaultMinValue + maxValue*(defaultMaxValue-defaultMinValue)/255;
        System.out.println("min : " + min + ", max : " + max);
        lut = new int[256];
        for(int i=0; i<256; i++) {
            float v1 = (i - min);
            float delta = max - min;
            int v = (int)((v1 / delta) * 256);
            if(v < 0) {
                v = 0;
            }
            if(v > 255) {
                v = 255;
            }
            lut[i] = v;
        }
    }

    public void setupBrightnessLut() {
        float center = defaultMinValue + (defaultMaxValue-defaultMinValue)*((256-bvalue)/256);
        float min = defaultMinValue + minValue*(defaultMaxValue-defaultMinValue)/255;
        float max = defaultMinValue + maxValue*(defaultMaxValue-defaultMinValue)/255;
        float delta= max-min;
        min = center - delta/2.0f;
        max = center + delta/2.0f;

        System.out.println("min : " + min + ", max : " + max);
        lut = new int[256];
        for(int i=0; i<256; i++) {
            float v1 = (i - min);
            float delta2 = max - min;
            int v = (int)((v1 / delta2) * 256);
            if(v < 0) {
                v = 0;
            }
            if(v > 255) {
                v = 255;
            }
            lut[i] = v;
        }
    }


}

  • ImagePanel
package demo2.coloradjust;

import demo2.rgbspace.WhiteImageFilter;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class ImagePanel extends JComponent implements ActionListener {

    private BufferedImage image;

    private JButton processBtn;

    public ImagePanel(BufferedImage image) {
        this.image = image;
    }

    public JButton getButton() {
        processBtn = new JButton("按钮");
        processBtn.addActionListener(this);

        return processBtn;
    }

    @Override
    protected void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        if (null != image) {
            g2d.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);
        }
    }

    public void process() {
        ColorAdjustFilter tool = new ColorAdjustFilter();
        tool.setMinValue(20);

        tool.process(image);
    }


    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == processBtn) {
            this.process();
            this.repaint();
        }
    }


    public static void main(String[] args) {
        File file = new File("resource/color.png");

        try {
            BufferedImage image = ImageIO.read(file);

            ImagePanel imp = new ImagePanel(image);
            JFrame frame = new JFrame();
            frame.getContentPane().add(imp, BorderLayout.CENTER);
            frame.getContentPane().add(imp.getButton(), BorderLayout.SOUTH);
            frame.setSize(600, 600);
            frame.setTitle("图像显示测试");
            frame.setVisible(true);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


}

在这里插入图片描述

2 亮度调整

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值