• 【图像处理】获取图片像素点


    获取图片像素点

    本文通过两种方法获取图片的像素点,方法1通过java自带的API,方法2通过OpenCV的API。

    1.Java自带API获取像素点

    函数声明:

    //获取当前像素rgb,其中x代表横坐标(横着的,对应于width)
    public int getRGB(int x,int y)
    
    //r、g、b能从color中获取
    Color color = new Color(img.getRGB(j, i));
    
    • 1
    • 2
    • 3
    • 4
    • 5

    当进行两层循环时,getRGB的参数需要反着写,具体看下面

    使用:

    BufferedImage img = ImageIO.read(new File("C:\\Users\\***.jpg"));
    
    for (int i = 0; i < height; i++) {//行
        for (int j = 0; j < width; j++) {//列
        
          	Color color = new Color(img.getRGB(j, i)); //反着写,列对应值x轴
            int red = color.getRed();
            int green = color.getGreen();
            int blue = color.getBlue();
            System.out.println("["+i+","+j + "]:(" + red + "," + green + "," + blue + ")");
         }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    2.opencv获取像素点

    函数声明

    public double[] get(int row,int col)
    //Mat中的方法,获取该点的像素点,
    //返回值是一个数组,顺序是B、G、R,是反着的
    //ans[0]代表blue值,ans[1]代表green,ans[2]代表red
    
    • 1
    • 2
    • 3
    • 4

    使用:

    Mat img = Imgcodecs.imread("C:\\***.jpg");
    
    for (int i = 0; i < size.height; i++) {
        for (int j = 0; j < size.width; j++) {
            double[] rgb = img.get(i, j);
            System.out.println("["+i+","+j + "]:(" + rgb[2] + "," + rgb[1] + "," + rgb[0] + ")");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    例子

    • 使用OpenCV获取所有像素点
        @Test
        public void sad() {
            System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
            Mat img = Imgcodecs.imread("C:\\Users\\Administrator\\Desktop\\w21005s1.jpg");
    
            img = img.submat(300, 305, 300, 303);//5*3
    
            Size size = img.size();
            System.out.println(size.height);
            System.out.println(size.width);
    
            int cnt = 0;
            for (int i = 0; i < size.height; i++) {
                for (int j = 0; j < size.width; j++) {
                    double[] rgb = img.get(i, j);
                    if (cnt<200)
                        System.out.println("["+i+","+j + "]:(" + rgb[2] + "," + rgb[1] + "," + rgb[0] + ")");
                    cnt++;
                }
            }
    
            System.out.println(cnt);
       }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 使用ImageIO获取所有像素点
    @Test
        public void ad() throws IOException {
            BufferedImage img = ImageIO.read(new File("C:\\Users\\Administrator\\Desktop\\w21005s1.jpg"));
            img = img.getSubimage(300,300,3,5);//5*3
            int height = img.getHeight();
            int width = img.getWidth();
    
            System.out.println(height);//5
            System.out.println(width);//3
    
            int cnt = 0;
            for (int i = 0; i < height; i++) {
                for (int j = 0; j < width; j++) {
    
                    Color color = new Color(img.getRGB(j, i));//getRGB(int x,int y) 其中x坐标代表width
                    int red = color.getRed();
                    int green = color.getGreen();
                    int blue = color.getBlue();
    
                    if (cnt < 200)
                        System.out.println("["+i+","+j + "]:(" + red + "," + green + "," + blue + ")");
    
                    cnt++;
                }
            }
            System.out.println(cnt);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    风格对比

    • 参数风格

      他们的参数正好是反着的,行对应的是y轴,列对应的是x轴,所以在使用的时候要略微注意不同

      • opencv的参数风格是:行,列。

      • 而Java原生的参数风格是:x轴,y轴。

    • opencv获取RGB时的顺序是B、G、R,是反着的

     double[] rgb = img.get(i, j);
    
    • 1

    rgb[0]代表blue,rgb[2]代表red

    未完待续

    操作像素点,可以做有意思的功能,比如,一台相机固定于一个地点,每天中午12:00拍摄一张照片,那么经过一年的拍摄,共能拍摄365或者366张图片,每张图片取最中间的四列像素,这样按照拍摄时间排序,就能够组成一张年度照片,该年度照片记录了这一个地点的中间位置一年的变化情况。

  • 相关阅读:
    信息学奥赛一本通(c++):1128:图像模糊处理
    Go语言操作protobuf协议使用详解
    IAB视频广告标准《数字视频和有线电视广告格式指南》之 概述- IAB受众和技术标准 - 我为什么要翻译介绍美国人工智能科技公司IAB系列(2)
    攻防世界-web-wzsc_文件上传
    Linux apt-get update - Could not connect to XXX(Connection refused)
    使用 Docker 部署 VS Code in The Browser
    2022年最新前端面试题
    vue+ts做一个类似课程表
    LeetCode 0816. 模糊坐标
    【TypeScript】—1— 关于TypeScript你必须知道的一切
  • 原文地址:https://blog.csdn.net/Supreme7/article/details/125896048