简体   繁体   English

相机拍摄的图像在OpenCV中为全黑

[英]Camera captured image is full black in OpenCV

I am a beginner in OpenCV - Java and trying to learn the basics by capturing an image from camera in laptop. 我是OpenCV-Java的初学者,并试图通过从便携式计算机中的相机捕获图像来学习基础知识。 I run the following code in eclipse and I can see the camera light flashing for a second, indicating that it really did start. 我在eclipse中运行以下代码,我可以看到相机指示灯闪烁了一秒钟,表明它确实启动了。 But the image stored is full black. 但是存储的图像是全黑的。

import org.opencv.core.*;
import org.opencv.videoio.VideoCapture;
import org.opencv.imgcodecs.*;

public class VideoCap {


    public static void main(String[] args){

        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        VideoCapture camera = new VideoCapture(0);
        if(!camera.isOpened()) {
                    System.out.println("Erro in opening camera");
        }

        else {
            Mat frame = new Mat();
            while(true) {
                if(camera.read(frame)) {
                    System.out.println("Camera obtained");
                    System.out.println("Captured frame width" + frame.width()
                    + " catured frame height " + frame.height() );
                    Imgcodecs.imwrite("cam.jpg", frame);
                    break;                  
                }
            }
        }
        camera.release();
    }
}

No errors on console, What might have gone wrong? 控制台上没有错误,可能出了什么问题?

You must change your code like this: 您必须像这样更改代码:

import org.opencv.core.*;
import org.opencv.imgcodecs.Imgcodecs;        
import org.opencv.videoio.VideoCapture;        

public class VideoCap {
    public static void main (String args[]){
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        VideoCapture camera = new VideoCapture(0);

        System.out.println("Welcome to OpenCV " + Core.VERSION);
        if(!camera.isOpened()){
            System.out.println("Error");
        }
        else {
            Mat frame = new Mat();
            camera.read(frame); 
            while(true){
                if (camera.read(frame)){
                    System.out.println("Frame Obtained");
                    System.out.println("Captured Frame Width " + 
                    frame.width() + " Height " + frame.height());
                    Imgcodecs.imwrite("c://capture/camera.jpg", frame);
                    System.out.println("OK");
                    break;
                }
            }   
        }
        camera.release();
    }
}  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM