1

OpenCV-像素值读写(java版)

 1 year ago
source link: https://blog.51cto.com/u_15820731/5745661
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

OpenCV-像素值读写(java版)

精选 原创

@​ ​TOC​​​

很多时候,我们需要读取某个像素值,或者设置某个像素值,甚至需要遍历整个像素值。

​​​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}

​以下以​​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)

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);
}

OpenCV-像素值读写(java版)_scala

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

OpenCV-像素值读写(java版)_scala_02

  • 收藏
  • 评论
  • 分享
  • 举报

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK