图片原理与处理实现滤镜效果

存储颜色的方法:把颜色存储到一个整数里面。

三原色的取值范围是0-255;256=1 byte,1个int有4个byte,从高到低分别叫做b1 b2 b3 b4,取b2 b3 b4分别去存储RGB,b1放参数。

我们先来绘制一个像素点:

 //重写JFrame的paint方法

 @Override
    public void paint(Graphics g) {
        super.paint(g);
        Color color = new Color(133, 40, 40); //改变画笔颜色
        g.setColor(color);
        g.fillRect(100, 100, 10, 10);

    }

实际上,一张照片就是由很多不同的颜色值排列而成。

我们考虑绘制一张照片:

@Override
    public void paint(Graphics g) {
        super.paint(g);
        for (int i=0;i<200;i++){
            for (int j = 0; j <200 ; j++) {
                Color color = new Color(133, 40, 40);
                g.setColor(color);
                g.fillRect(100+i, 100+j, 1, 1);
            }
        }
    }

绘制成品如下:

我们也可以改变每个像素点的颜色,让图片更好看点:

@Override
    public void paint(Graphics g) {
        super.paint(g);
        for (int i=0;i<200;i++){
            for (int j = 0; j <200 ; j++) {
                Color color = new Color(i, j, 40);
                g.setColor(color);
                g.fillRect(100+i, 100+j, 1, 1);
            }
        }

结果如下,图像上每个点的像素值都会有变化:

如果我们想让每个像素点的颜色为随机取值:

@Override
    public void paint(Graphics g) {
        super.paint(g);
        Random random = new Random();
        
        for(int i = 0; i < 200; i++){
            for(int j = 0; j < 200; j++){
                // 随机一个(0-16777214)范围的整数
                int randomRGB= random.nextInt (16777215);
                Color color = new Color (randomRGB);
                g.setColor (color);
                g.fillRect (100+i,100+j,1,1);
            }
        }
    }

结果如下:

至此代码如下:

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;

public class ImagePad extends JFrame {
    public static void main(String[] args) {
        ImagePad imagePad = new ImagePad();
        imagePad.initUI();
    }

    public void initUI() {
        setTitle("图像处理");
        setSize(800, 800);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }


     @Override
    public void paint(Graphics g) {
        super.paint(g);
        Random random = new Random();

        for(int i = 0; i < 200; i++){
            for(int j = 0; j < 200; j++){
                // 随机一个(0-16777214)范围的整数
                int randomRGB= random.nextInt (16777215);
                Color color = new Color (randomRGB);
                g.setColor (color);
                g.fillRect (100+i,100+j,1,1);
            }
        }
    }
}

接下来我们要把一张图片转化为一个int类型的二维数组。

我们要写一个函数:传入一张图片的路径,返回一个int [][]。

下面获取一张图片的像素数组:

public int[][] getImagePixelArray(String path) { //传入path字符串,作为图片路径
        //将图片路径转换为一个文件对象
        File file = new File(path);

        //声明一个BUfferedImage变量名
        BufferedImage buffimg = null;

        //IO流读取文件为一个缓冲图片对象
        try {
            buffimg = ImageIO.read(file);
        } catch (IOException e) {
            e.printStackTrace();
        }

        //获取图片的宽、高
        int h=buffimg.getHeight();
        int w=buffimg.getWidth();

        //定义一个与图片像素宽高一致的二维数组
        int[][] imgArray = new int[w][h];


        //遍历存储整数值
        for(int i = 0; i < w; i++){
            for(int j = 0; j < h; j++){
                imgArray[i][j] = buffimg.getRGB (i,j);
            }
        }

        // 返回值像素值数组
        return imgArray;
    }

接下来我们将照片画到窗体上:

        @Override
        public void paint(Graphics g) {
            super.paint(g);
            // 获取一张照片的像素数组
            int[][] imagePixelArray= getImagePixelArray("C:\\Users\\BabyLu\\Desktop\\Gift.jpg");
                
            // 遍历每个像素值 并绘制出来
            for(int i = 0; i < imagePixelArray.length; i++){         
                for(int j = 0; j < imagePixelArray[0].length; j++){  
                    int rgb = imagePixelArray[i][j];
                    Color color = new Color (rgb);
                    g.setColor(color);
                    g.fillRect (100+i,100+j,1,1);
                }
            }

显示效果如下:

接下来我们获取R、G、B三原色:

 @Override
        public void paint(Graphics g) {
            super.paint(g);
           
            int[][] imagePixelArray= getImagePixelArray("C:\\Users\\BabyLu\\Desktop\\Gift.jpg");
            int ww=imagePixelArray.length;
            int hh=imagePixelArray[i].length;
            
            // 遍历每个像素值 并绘制出来
            for(int i = 0; i < ww; i++){         
                for(int j = 0; j < hh; j++){  
                    int rgb = imagePixelArray[i][j];
                    Color color = new Color (rgb);

                    // 获取RGB三原色
                    int red = color.getRed ();
                    int green = color.getGreen ();
                    int blue = color.getBlue ();

                }
            }

修改图片色调:

                    Color nColor = new Color (red / 2, green, blue);
                    g.setColor(nColor);
                    g.fillRect (100+i,100+j,1,1);

显示结果如下:

我们想把图片设置为灰色色调:

修改代码如下:

          int gray = (red + green + blue) / 3;
          Color nColor = new Color (gray, gray, gray);//0-255

结果显示如下:

在灰度的基础上,我们可以进行二值化计算,比如:

                // 二值化 在灰度的基础上 通过灰度值来判断
                if(gray < 70){
                    g.setColor (Color.BLACK);
                } else{
                    g.setColor (Color.WHITE);
                }

                g.fillRect(100 + i, 100 + j, 1, 1);

效果如下:

反色:

Color nColor = new Color (255-red,255-green,255-blue);

效果如下:

 现附上完整代码:

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

public class ImagePad extends JFrame {
        public static void main(String[] args) {
            ImagePad imagePad = new ImagePad();
            imagePad.initUI();
        }

        public void initUI() {
            setTitle("图像处理");
            setSize(800, 800);
            setVisible(true);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }

    @Override
    public void paint(Graphics g) {
        super.paint(g);

        int[][] imagePixelArray = getImagePixelArray("C:\\Users\\BabyLu\\Desktop\\BabyLu.jpg");
        int ww = imagePixelArray.length;
        int hh = imagePixelArray[0].length;

        // 遍历每个像素值 并绘制出来
        for (int i = 0; i < ww; i++) {
            for (int j = 0; j < hh; j++) {
                int rgb = imagePixelArray[i][j];
                Color color = new Color(rgb);

                // 获取RGB三原色
                int red = color.getRed();
                int green = color.getGreen();
                int blue = color.getBlue();

                Color nColor = new Color (255-red,255-green,255-blue);//0-255
                g.setColor(nColor);
                g.fillRect(100 + i, 100 + j, 1, 1);
            }
        }
    }

        public int[][] getImagePixelArray(String path) {
                //将图片路径转换为一个文件对象
                File file = new File(path);
                //声明一个BUfferedImage变量名
                BufferedImage buffimg = null;

                //IO流读取文件 为一个缓冲图片对象
                try {
                    buffimg = ImageIO.read(file);
                } catch (IOException e) {
                    e.printStackTrace();
                }

                //获取图片的宽、高
                int h=buffimg.getHeight();
                int w=buffimg.getWidth();

                //定义一个与图片像素宽高一致的二维数组
                int[][] imgArray = new int[w][h];

                //遍历存储整数值
                for(int i = 0; i < w; i++){
                    for(int j = 0; j < h; j++){
                        imgArray[i][j] = buffimg.getRGB (i,j);
                    }
                }

                // 返回值像素值数组
                return imgArray;
            }
        }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值