📅  最后修改于: 2020-11-23 03:36:45             🧑  作者: Mango
在前面的章节中,我们讨论了如何使用OpenCV Java库读取和保存图像。除此之外,我们还可以使用GUI库(例如AWT / Swings和JavaFX)在单独的窗口中显示加载的图像。
要读取图像,我们使用imread()方法。此方法返回以Matrix形式读取的图像。但是,要将此图像与GUI库(AWT / Swings和JavaFX)一起使用,应将其转换为包java.awt.image.BufferedImage的BufferedImage类的对象。
以下是将OpenCV的Mat对象转换为BufferedImage对象的步骤。
首先,您需要将矩阵转换为字节矩阵。您可以使用Imgcodecs类的imencode ()方法来实现。以下是此方法的语法。
imencode(ext, image, matOfByte);
此方法接受以下参数-
Ext-一个String参数,指定图像格式(.jpg,.png等)
图像–图像的Mat对象
matOfByte -MatOfByte类的空对象
如下所示,使用此方法对图像进行编码。
//Reading the image
Mat image = Imgcodecs.imread(file);
//instantiating an empty MatOfByte class
MatOfByte matOfByte = new MatOfByte();
//Converting the Mat object to MatOfByte
Imgcodecs.imencode(".jpg", image, matOfByte);
使用toArray()方法将MatOfByte对象转换为字节数组。
byte[] byteArray = matOfByte.toArray();
通过将上一步中创建的字节数组传递给ByteArrayInputStream类的构造函数,以准备InputStream对象。
//Preparing the InputStream object
InputStream in = new ByteArrayInputStream(byteArray);
将上一步中创建的Input Stream对象传递到ImageIO类的read()方法。这将返回一个BufferedImage对象。
//Preparing the BufferedImage
BufferedImage bufImage = ImageIO.read(in);
要使用AWT / Swings框架显示图像,首先,请使用imread()方法读取图像,然后按照上述步骤将其转换为BufferedImage 。
然后,实例化JFrame类,并将创建的缓冲图像添加到JFrame的ContentPane中,如下所示-
//Instantiate JFrame
JFrame frame = new JFrame();
//Set Content to the JFrame
frame.getContentPane().add(new JLabel(new ImageIcon(bufImage)));
frame.pack();
frame.setVisible(true);
例
以下程序代码显示了如何使用OpenCV库读取图像并通过旋转窗口显示图像。
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.imgcodecs.Imgcodecs;
public class DisplayingImagesUsingSwings {
public static void main(String args[]) throws Exception {
//Loading the OpenCV core library
System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
//Reading the Image from the file and storing it in to a Matrix object
String file = "C:/EXAMPLES/OpenCV/sample.jpg";
Mat image = Imgcodecs.imread(file);
//Encoding the image
MatOfByte matOfByte = new MatOfByte();
Imgcodecs.imencode(".jpg", image, matOfByte);
//Storing the encoded Mat in a byte array
byte[] byteArray = matOfByte.toArray();
//Preparing the Buffered Image
InputStream in = new ByteArrayInputStream(byteArray);
BufferedImage bufImage = ImageIO.read(in);
//Instantiate JFrame
JFrame frame = new JFrame();
//Set Content to the JFrame
frame.getContentPane().add(new JLabel(new ImageIcon(bufImage)));
frame.pack();
frame.setVisible(true);
System.out.println("Image Loaded");
}
}
在执行上述程序时,您将获得以下输出-
Image Loaded
除此之外,您还可以看到一个显示加载图像的窗口,如下所示:
要使用JavaFX显示图像,首先,使用imread()方法读取图像并将其转换为BufferedImage 。然后,将BufferedImage转换为WritableImage,如下所示。
WritableImage writableImage = SwingFXUtils.toFXImage(bufImage, null);
将此WritableImage对象传递给ImageView类的构造函数。
ImageView imageView = new ImageView(writableImage);
例
以下程序代码显示了如何使用OpenCV库读取图像并通过JavaFX窗口显示图像。
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.stage.Stage;
import javax.imageio.ImageIO;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.imgcodecs.Imgcodecs;
public class DisplayingImagesJavaFX extends Application {
@Override
public void start(Stage stage) throws IOException {
WritableImage writableImage = loadImage();
//Setting the image view
ImageView imageView = new ImageView(writableImage);
//Setting the position of the image
imageView.setX(50);
imageView.setY(25);
//setting the fit height and width of the image view
imageView.setFitHeight(400);
imageView.setFitWidth(500);
//Setting the preserve ratio of the image view
imageView.setPreserveRatio(true);
//Creating a Group object
Group root = new Group(imageView);
//Creating a scene object
Scene scene = new Scene(root, 600, 400);
//Setting title to the Stage
stage.setTitle("Loading an image");
//Adding scene to the stage
stage.setScene(scene);
//Displaying the contents of the stage
stage.show();
}
public WritableImage loadImage() throws IOException {
//Loading the OpenCV core library
System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
//Reading the Image from the file and storing it in to a Matrix object
String file ="C:/EXAMPLES/OpenCV/sample.jpg";
Mat image = Imgcodecs.imread(file);
//Encoding the image
MatOfByte matOfByte = new MatOfByte();
Imgcodecs.imencode(".jpg", image, matOfByte);
//Storing the encoded Mat in a byte array
byte[] byteArray = matOfByte.toArray();
//Displaying the image
InputStream in = new ByteArrayInputStream(byteArray);
BufferedImage bufImage = ImageIO.read(in);
System.out.println("Image Loaded");
WritableImage writableImage = SwingFXUtils.toFXImage(bufImage, null);
return writableImage;
}
public static void main(String args[]) {
launch(args);
}
}
在执行上述程序时,您将获得以下输出-
Image Loaded
除此之外,您还可以看到一个显示加载图像的窗口,如下所示: