📜  OpenCV-存储图像

📅  最后修改于: 2020-11-23 03:35:34             🧑  作者: Mango


为了捕获图像,我们使用诸如照相机和扫描仪之类的设备。这些设备记录图像的数值(例如:像素值)。 OpenCV是一个处理数字图像的库,因此我们需要存储这些图像以进行处理。

OpenCV库的Mat类用于存储图像的值。它代表n维数组,用于存储灰度或彩色图像,体素体积,矢量场,点云,张量,直方图等图像数据。

此类包含两个数据部分:标头指针

  • 标头-包含诸如大小,用于存储的方法以及矩阵地址(大小恒定)之类的信息。

  • 指针-存储图像的像素值(保持变化)。

垫类

OpenCV Java库在包org.opencv.core中为此类提供了相同的名称( Mat )。

建设者

OpenCV Java库的Mat类具有各种构造函数,可以使用它们构造Mat对象。

S.No Constructors and Description
1

Mat()

This is the default constructor with no parameters in most cases. We use this to constructor to create an empty matrix and pass this to other OpenCV methods.

2

Mat(int rows, int cols, int type)

This constructor accepts three parameters of integer type representing the number of rows and columns in a 2D array and the type of the array (that is to be used to store data).

3

Mat(int rows, int cols, int type, Scalar s)

Including the parameters of the previous one, this constructor additionally accepts an object of the class Scalar as parameter.

4

Mat(Size size, int type)

This constructor accepts two parameters, an object representing the size of the matrix and an integer representing the type of the array used to store the data.

5

Mat(Size size, int type, Scalar s)

Including the parameters of the previous one, this constructor additionally accepts an object of the class Scalar as parameter.

6

Mat(long addr)

7

Mat(Mat m, Range rowRange)

This constructor accepts an object of another matrix and an object of the class Range representing the range of the rows to be taken to create a new matrix.

8

Mat(Mat m, Range rowRange, Range colRange)

Including the parameters of the previous one, this constructor additionally accepts an object of the class. Range representing the column range.

9

Mat(Mat m, Rect roi)

This constructor accepts two objects, one representing another matrix and the other representing the Region Of Interest.

注意

  • 数组类型。使用CV_8UC1,…,CV_64FC4创建1-4通道矩阵,或使用CV_8UC(n),…,CV_64FC(n)创建多通道(最多CV_CN_MAX通道)矩阵。

  • 矩阵的类型由CvType类的各个字段表示,该字段属于org.opencv.core包。

方法与说明

以下是Mat类提供的一些方法。

S.No Methods and Description
1

Mat col(int x)

This method accepts an integer parameter representing the index of a column and retrieves and returns that column.

2

Mat row(int y)

This method accepts an integer parameter representing the index of a row and retrieves and returns that row.

3

int cols()

This method returns the number of columns in the matrix.

4

int rows()

This method returns the number of rows in the matrix.

5

Mat setTo(Mat value)

This method accepts an object of the Mat type and sets the array elements to the specified value.

6

Mat setTo(Scalar s)

This method accepts an object of the Scalar type and sets the array elements to the specified value.

创建和显示矩阵

在本节中,我们将讨论我们的第一个OpenCV示例。我们将看到如何创建和显示简单的OpenCV矩阵。

下面给出了在OpenCV中创建和显示矩阵所要遵循的步骤。

步骤1:加载OpenCV本机库

使用OpenCV库编写Java代码时,第一步是使用loadLibrary()加载OpenCV的本机库。如下所示加载OpenCV本机库。

//Loading the core library 
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

步骤2:实例化Mat类

使用本章前面提到的任何功能实例化Mat类。

//Creating a matrix 
Mat matrix = new Mat(5, 5, CvType.CV_8UC1, new Scalar(0));

步骤3:使用方法填充矩阵

您可以通过将索引值传递给方法row()/ col()来检索矩阵的特定行/列。

并且,您可以使用setTo()方法的任何变体为这些值设置值。

//Retrieving the row with index 0 
Mat row0 = matrix.row(0); 
     
//setting values of all elements in the row with index 0 
row0.setTo(new Scalar(1)); 
     
//Retrieving the row with index 3 
Mat col3 = matrix.col(3);  
     
//setting values of all elements in the row with index 3 
col3.setTo(new Scalar(3));

您可以使用以下程序代码使用OpenCV库在Java中创建和显示简单矩阵。

import org.opencv.core.Core; 
import org.opencv.core.Mat;  
import org.opencv.core.CvType;  
import org.opencv.core.Scalar;   

class DisplayingMatrix { 
   public static void main(String[] args) {     
      //Loading the core library 
      System.loadLibrary(Core.NATIVE_LIBRARY_NAME);     

      //Creating a matrix 
      Mat matrix = new Mat(5, 5, CvType.CV_8UC1, new Scalar(0));  

      //Retrieving the row with index 0 
      Mat row0 = matrix.row(0);

      //setting values of all elements in the row with index 0 
      row0.setTo(new Scalar(1)); 

      //Retrieving the row with index 3 
      Mat col3 = matrix.col(3);  

      //setting values of all elements in the row with index 3 
      col3.setTo(new Scalar(3)); 

      //Printing the matrix 
      System.out.println("OpenCV Mat data:\n" + matrix.dump()); 
   } 
}

在执行上述程序时,您将获得以下输出-

OpenCV Mat data: 
[  1,   1,   1,   3,   1; 
   0,   0,   0,   3,   0; 
   0,   0,   0,   3,   0; 
   0,   0,   0,   3,   0; 
   0,   0,   0,   3,   0]

使用JavaSE API加载图像

java.awt.image.BufferedImage包的BufferedImage类用于存储图像,而import javax.imageio包的ImageIO类则提供读取和写入图像的方法。

您可以使用以下程序代码来使用JavaSE库加载和保存图像。

import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 
import javax.imageio.ImageIO;
  
public class LoadingImage_JSE_library {
   public static void main( String[] args ) throws IOException {
      //Input File 
      File input = new File("C:/EXAMPLES/OpenCV/sample.jpg");
          
      //Reading the image 
      BufferedImage image = ImageIO.read(input);
      
      //Saving the image with a different name
      File ouptut = new File("C:/OpenCV/sample.jpg");
      ImageIO.write(image, "jpg", ouptut);
         
      System.out.println("image Saved");
   } 
}

在执行上述程序时,您将获得以下输出-

image Saved

如果打开指定的路径,则可以观察保存的图像,如下所示:

使用JavaSE API加载图像