Java 如果使用 nu.pattern.OpenCV.loadShared() 加载 openCV,则打开 CV 在 VideoCapture 上挂起以获取文件;
Java open CV hangs on VideoCapture for file if openCV is loaded with nu.pattern.OpenCV.loadShared();
我有一个来自 https://ratiler.wordpress.com/2014/09/08/detection-de-mouvement-avec-javacv/ 的示例代码,在使用 nu.pattern.OpenCV.loadShared();
:
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.videoio.VideoCapture;
import org.opencv.imgproc.Imgproc;
public class JavaCVPrjt01 {
static {
//System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
nu.pattern.OpenCV.loadShared();
}
public static void main(String[] args) {
JFrame jframe = new JFrame("HUMAN MOTION DETECTOR FPS");
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel vidpanel = new JLabel();
jframe.setContentPane(vidpanel);
jframe.setSize(1280,720) ;
jframe.setVisible(true);
Mat frame = new Mat();
Mat outerBox = new Mat();
Size sz = new Size(1280,720);
VideoCapture camera = new VideoCapture("D:/Downloads/video4.mp4");
while (true) {
if (camera.read(frame)) {
Imgproc.resize(frame,frame, sz );
outerBox = new Mat(frame.size(), CvType.CV_8UC1);
Imgproc.cvtColor(frame, outerBox, Imgproc.COLOR_BGR2GRAY);
Imgproc.GaussianBlur(outerBox, outerBox, new Size(3, 3), 0);
ImageIcon image = new ImageIcon(Mat2bufferedImage(outerBox));
vidpanel.setIcon(image);
vidpanel.repaint();
}
}
}
public static BufferedImage Mat2bufferedImage(Mat image) {
MatOfByte bytemat = new MatOfByte();
Imgcodecs.imencode(".jpg", image, bytemat);
byte[] bytes = bytemat.toArray();
InputStream in = new ByteArrayInputStream(bytes);
BufferedImage img = null;
try {
img = ImageIO.read(in);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return img;
}
}
当然,还添加了所需的依赖项:
<dependency>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>opencv</artifactId>
<version>3.2.0-1.3</version>
</dependency>
<dependency>
<groupId>org.openpnp</groupId>
<artifactId>opencv</artifactId>
<version>3.2.0-1</version>
</dependency>
所以,代码在
之前执行
VideoCapture camera = new VideoCapture("D:/Downloads/video4.mp4");
它挂在哪里,我没有任何线索可以检查那里的 JNI 级别有什么问题。
许可问题导致无法将 ffmpeg 添加到
<dependency>
<groupId>org.openpnp</groupId>
<artifactId>opencv</artifactId>
<version>3.2.0-1</version>
</dependency>
(看起来它出现在所有版本中,我已经尝试了所有可用的 maven)
https://github.com/openpnp/opencv/issues/55
UPD.
所以仍然需要显式加载dll。就我而言
static {
//System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
System.load("D:\Downloads\opencv\build\java\x64\opencv_java2411.dll");
System.load("D:\Downloads\opencv\build\x64\vc12\bin\opencv_ffmpeg2411_64.dll");
}
opencv_java
和 opencv_ffmpeg
的显式加载成功了。没有显式加载 opencv_ffmpeg
,VideoCapture's
.read(frame)
总是返回 false,没有提及任何缺失的原生 ffmpeg
依赖问题。
并且 pom.xml
中所需的 java 依赖项是:
<dependency>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>opencv</artifactId>
<version>2.4.11-0.11</version>
</dependency>
<dependency>
<groupId>org.openpnp</groupId>
<artifactId>opencv</artifactId>
<version>2.4.11-2</version>
</dependency>
(请注意我已经降级了opencv版本)
我有一个来自 https://ratiler.wordpress.com/2014/09/08/detection-de-mouvement-avec-javacv/ 的示例代码,在使用 nu.pattern.OpenCV.loadShared();
:
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.videoio.VideoCapture;
import org.opencv.imgproc.Imgproc;
public class JavaCVPrjt01 {
static {
//System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
nu.pattern.OpenCV.loadShared();
}
public static void main(String[] args) {
JFrame jframe = new JFrame("HUMAN MOTION DETECTOR FPS");
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel vidpanel = new JLabel();
jframe.setContentPane(vidpanel);
jframe.setSize(1280,720) ;
jframe.setVisible(true);
Mat frame = new Mat();
Mat outerBox = new Mat();
Size sz = new Size(1280,720);
VideoCapture camera = new VideoCapture("D:/Downloads/video4.mp4");
while (true) {
if (camera.read(frame)) {
Imgproc.resize(frame,frame, sz );
outerBox = new Mat(frame.size(), CvType.CV_8UC1);
Imgproc.cvtColor(frame, outerBox, Imgproc.COLOR_BGR2GRAY);
Imgproc.GaussianBlur(outerBox, outerBox, new Size(3, 3), 0);
ImageIcon image = new ImageIcon(Mat2bufferedImage(outerBox));
vidpanel.setIcon(image);
vidpanel.repaint();
}
}
}
public static BufferedImage Mat2bufferedImage(Mat image) {
MatOfByte bytemat = new MatOfByte();
Imgcodecs.imencode(".jpg", image, bytemat);
byte[] bytes = bytemat.toArray();
InputStream in = new ByteArrayInputStream(bytes);
BufferedImage img = null;
try {
img = ImageIO.read(in);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return img;
}
}
当然,还添加了所需的依赖项:
<dependency>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>opencv</artifactId>
<version>3.2.0-1.3</version>
</dependency>
<dependency>
<groupId>org.openpnp</groupId>
<artifactId>opencv</artifactId>
<version>3.2.0-1</version>
</dependency>
所以,代码在
之前执行VideoCapture camera = new VideoCapture("D:/Downloads/video4.mp4");
它挂在哪里,我没有任何线索可以检查那里的 JNI 级别有什么问题。
许可问题导致无法将 ffmpeg 添加到
<dependency>
<groupId>org.openpnp</groupId>
<artifactId>opencv</artifactId>
<version>3.2.0-1</version>
</dependency>
(看起来它出现在所有版本中,我已经尝试了所有可用的 maven)
https://github.com/openpnp/opencv/issues/55
UPD.
所以仍然需要显式加载dll。就我而言
static {
//System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
System.load("D:\Downloads\opencv\build\java\x64\opencv_java2411.dll");
System.load("D:\Downloads\opencv\build\x64\vc12\bin\opencv_ffmpeg2411_64.dll");
}
opencv_java
和 opencv_ffmpeg
的显式加载成功了。没有显式加载 opencv_ffmpeg
,VideoCapture's
.read(frame)
总是返回 false,没有提及任何缺失的原生 ffmpeg
依赖问题。
并且 pom.xml
中所需的 java 依赖项是:
<dependency>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>opencv</artifactId>
<version>2.4.11-0.11</version>
</dependency>
<dependency>
<groupId>org.openpnp</groupId>
<artifactId>opencv</artifactId>
<version>2.4.11-2</version>
</dependency>
(请注意我已经降级了opencv版本)