OpenCV-像素值读写(java版)


  很多时候,我们需要读取某个像素值,或者设置某个像素值,甚至需要遍历整个像素值。
   OpenCV中RGB图像的通道为BGR!!!

1. 读像素值

1.get()函数

OpenCV中使用get()方法来实现读去矩阵中的某个像素。下方是提供的方法

方法说明
get(int row, int col)返回double[] 类型的像素数据
get(int[] idx)
get(int row, int col, byte[] data)支持 CV_8UC(ch)、CV_8SC(ch)(ch 表示通道数,支持1-4,下方支持类型一样的道理)
get(int[] idx, byte[] data)
get(int row, int col, short[] data)支持 CV_16S(ch)、CV_16U(ch)
get(int[] idx, short[] data)
get(int row, int col, int[] data)支持 CV_32S(ch)
get(int[] idx, int[] data)
get(int row, int col, float[] data)支持 CV_32F(ch)
get(int[] idx, float[] data)
get(int row, int col, double[] data)支持类型为CV_64F(ch)
get(int[] idx, double[] data)
参数说明row,col :分别代表在图像中所在的y,x的位置(可以理解图像的坐标点,xy轴。一遍从左上角开始,再直白点就是可以理解为分别代表照片的宽高)
data :图像元素值,返回的data类型根绝图像类型而定
idx:长度为2的数组,{row,col}

2.示例

以下以CV_8UC3为例,对上述部分方法进行演示

  1. get(row,col)
    执行下方main方法
    public static void main(String[] args) {
        String libraryPath= System.getProperty("user.dir")+"\\lib\\opencv_java460.dll";
        System.load(libraryPath);
        Mat mat = new Mat(3,3, CvType.CV_8UC3);
        int rows=mat.rows();
        int cols=mat.cols();
        //遍历所有像素值
      for (int i=0;i<rows;i++){
            for (int j =0;j<cols;j++){
                //get(row,col) 演示
                double[] scalarVal=mat.get(i,j);
               System.out.println("mat["+i+","+j+"]"+"元素值:B="+scalarVal[0]+"G="+scalarVal[1]+"R="+scalarVal[2]);
            }
        }
    }

结果

mat[0,0]元素值:B=0.0G=0.0R=0.0
mat[0,1]元素值:B=0.0G=0.0R=0.0
mat[0,2]元素值:B=0.0G=0.0R=0.0
mat[1,0]元素值:B=0.0G=0.0R=0.0
mat[1,1]元素值:B=0.0G=0.0R=0.0
mat[1,2]元素值:B=0.0G=0.0R=0.0
mat[2,0]元素值:B=0.0G=0.0R=0.0
mat[2,1]元素值:B=0.0G=0.0R=0.0
mat[2,2]元素值:B=0.0G=0.0R=0.0

2.get(int[] idx)

    public static void main(String[] args) {
        String libraryPath= System.getProperty("user.dir")+"\\lib\\opencv_java460.dll";
        System.load(libraryPath);
        Mat mat = new Mat(3,3, CvType.CV_8UC3);
        //通过ids获取,ids值分别为row,col
        int[] ids ={1,2};
        double[]  scalarVal= mat.get(ids);
        System.out.println("元素值:B="+scalarVal[0]+"G="+scalarVal[1]+"R="+scalarVal[2]);
    }

结果为

元素值:B=0.0G=0.0R=0.0

3.get(int[] idx, byte[] data)

    public static void main(String[] args) {
        String libraryPath= System.getProperty("user.dir")+"\\lib\\opencv_java460.dll";
        System.load(libraryPath);
        Mat mat = new Mat(3,3, CvType.CV_8UC3);
        int[] ids ={1,2};
        //获取第一行,第2列
        byte[] data =new byte[mat.channels()];
        int B = data[0]&0xff;
        int G = data[1]&0xff;
        int R = data[2]&0xff;
        mat.get(ids,data);
        System.out.println("元素值:B="+B+"G="+G+"R="+R);
    }

结果

元素值:B=0G=0R=0

2.写像素值

1.put()函数

OpenCV中使用put()方法来实现像素的创建。方法和get()方法相对应。,下方仅举出不一样的几个方法

方法说明
put(int[] idx, double... data)data:通道对应的元素值
put(int[] idx, byte[] data, int offset, int length)offset: 偏移量,向左(+)或向右(-)迁移通道对应的元素值。无值时,默认补0 length:图像元素长度
put(int row, int col, byte[] data, int offset, int length)
## 2.示例 1.put(int[] idx, double... data)
    public static void main(String[] args) {
        String libraryPath= System.getProperty("user.dir")+"\\lib\\opencv_java460.dll";
        System.load(libraryPath);
        //创建一个200*200 8位3通道的蓝色图像
        Mat matB =new Mat(200,200, CvType.CV_8UC3,new Scalar(255,0,0));
        int rows =matB.rows();
        int cols =matB.cols();
        for (int i=0;i<rows;i++){
            for (int j=0;j<cols;j++){
                if (i>=99 && j>=99){
                    int[] idx={i,j};
                   matB.put(idx,0,255,0);
                }
            }
        }
        HighGui.imshow("matB",matB);
        HighGui.waitKey(0);
    }

执行效果
在这里插入图片描述
2.put(int[] idx, byte[] data, int offset, int length)

    public static void main(String[] args) {
        String libraryPath= System.getProperty("user.dir")+"\\lib\\opencv_java460.dll";
        System.load(libraryPath);
        //创建一个200*200 8位3通道的蓝色图像
        Mat matB =new Mat(200,200, CvType.CV_8UC3,new Scalar(255,0,0));
        int rows =matB.rows();
        int cols =matB.cols();
        //创建3*3矩阵图像
        Mat mat = new Mat(3,3, CvType.CV_8UC3,new Scalar(0,208,91));
        int[] ids ={1,2};
        byte[] data =new byte[mat.channels()];
        mat.get(ids,data);
        int B = data[0]&0xff;
        int G = data[1]&0xff;
        int R = data[2]&0xff;
        System.out.println("mat初始值:B="+B+",G="+G+",R="+R);
        for (int i=0;i<rows;i++){
            for (int j=0;j<cols;j++){
                if (i>=99 && j>=99){
                    int[] idx={i,j};
                    //赋mat的值给matB,并将元素值向右偏移一位
                   matB.put(idx,data,-1,data.length);
                }
            }
        }

        //获取 199,199位置的图像元素值
        int[] ids1 ={199,199};
        byte[] data1 =new byte[matB.channels()];
        matB.get(ids1,data1);
        int B1 = data1[0]&0xff;
        int G1 = data1[1]&0xff;
        int R1 = data1[2]&0xff;
        System.out.println("最终元素值:B="+B1+",G="+G1+",R="+R1);
        HighGui.imshow("matB",matB);
        HighGui.waitKey(0);
    }

执行结果

mat初始值:B=0,G=208,R=91
最终元素值:B=0,G=0,R=208

在这里插入图片描述

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不要喷香水

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值