OpenCV和Java,读取图像

时间:2019-03-07 17:55:27

标签: java opencv

我想开发一个简单的程序,可以使用OpenCV将滤镜应用于图像。我正在使用OpenCV 4.0.1。代码很简单,我认为是正确的,但是问题是我什至无法读取图像。

public static void main(String[] args) {
    // TODO Auto-generated method stub
    System.out.println("button pressed!");
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    Mat frame = new Mat();
    frame = Imgcodecs.imread("./demo.jpg");
    System.out.println(frame.toString());
    BufferedImage img = bufferedImage(frame);
    JFrame window = new JFrame();
    window.setLayout(new FlowLayout());  
    JLabel lbl = new JLabel();
    lbl.setIcon((Icon) img);
    window.add(lbl);
    window.setVisible(true);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Mat frameBN = new Mat();
    System.out.println("Immagine importata, applico un filtro B/N");
    Imgproc.cvtColor(frame, frameBN, Imgproc.COLOR_BGR2GRAY);
    System.out.println("Filtro applicato, salvo immagine");
    Imgcodecs.imwrite("C:\\Users\\Giuseppe Ferrara\\Desktop\\photo-filter\\photo-filter\\bin\\foto\\demoBN.jpg",frame);
    System.out.println("immagine salvata!");

}

我包含了以下库:

import java.awt.Image;

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

import org.opencv.core.*;
import org.opencv.highgui.*;
import java.awt.*;
import javax.swing.*;
import java.awt.image.*;

我将不包括函数bufferedImage,因为问题不存在,实际上,当我打印Imgcodecs.imread("./demo.jpg")读取图像时说它的大小为0时,所以我认为它找不到图片,我该怎么解决?

0 个答案:

没有答案