Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Binarization of real-time video

How to do binarization to a video using opencv and java.Please anyone guide me to do that.

Binarization of real-time video

How to do binarization to a video using opencv and java.Please anyone guide me to do that.

Current CODE

Binarization of real-time video

Here I have implemented the code. It extract frames from a video ,converts each frame to grayscale ---threshold ---binary and outputs. But it outputs frames one by one .I want to add extracted binary frames together and output a complete video in black and white. How to do binarization add frames to output a video using opencv and java.Please anyone guide me to do that.video???.

Current CODE

@FXML
public void takeframesbyframes(ActionEvent e) {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    String filePath = "/home/chinthaka/Downloads/wow/giphy.mp4";
    if (!Paths.get(filePath).toFile().exists()) {
        System.out.println("File " + filePath + " does not exist!");
        return;
    }
    VideoCapture camera = new VideoCapture(filePath);
    if (!camera.isOpened()) {
        System.out.println("Error! Camera can't be opened!");
        return;
    }
    Mat frame = new Mat();
    while (true) {
        if (camera.read(frame)) {
            System.out.println("Frame Obtained");
            System.out.println("Captured Frame Width " +
                    frame.width() + " Height " + frame.height());
            Imgcodecs.imwrite("camera.jpg", frame);
            System.out.println("OK");
            while (camera.read(frame)) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                }
                System.out.println("Frame Obtained");
                System.out.println("Captured Frame Width " +
                        frame.width() + " Height " + frame.height());
                Imgcodecs.imwrite("camera.jpg", frame);
                System.out.println("OK");

                //convert original color frame into gray scale frame.
                Imgproc.cvtColor(frame, frame, Imgproc.COLOR_BGR2GRAY);

                //convert gray scale frame to binary frame.
                threshold(frame, frame, 100, 255, THRESH_BINARY);

                bufferedImage = matToBufferedImage(frame);
                showWindow(bufferedImage);
                Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
                alert.setTitle("Extracting Frames");
                alert.setHeaderText("You are in a process.");
                alert.setContentText("Do you want to get next frame!!!");
                Optional<ButtonType> result = alert.showAndWait();
                if (result.get() == ButtonType.OK) {
                    alert.hide();
                } else {
                    break;
                }
            }
            camera.release();
            break;
        }
    }
}
private static BufferedImage matToBufferedImage(Mat frame) {
    int type = 0;
    if (frame.channels() == 1) {
        type = BufferedImage.TYPE_BYTE_GRAY;
    } else if (frame.channels() == 3) {
        type = BufferedImage.TYPE_3BYTE_BGR;
    }
    BufferedImage image = new BufferedImage(frame.width(), frame.height(), type);
    WritableRaster raster = image.getRaster();
    DataBufferByte dataBuffer = (DataBufferByte) raster.getDataBuffer();
    byte[] data = dataBuffer.getData();
    frame.get(0, 0, data);
    return image;
}
private void showWindow(BufferedImage img) {
    JFrame frame = new JFrame();
    frame.getContentPane().add(new JLabel(new ImageIcon(img)));
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.setSize(img.getWidth(), img.getHeight() + 100);
    frame.setTitle("Image captured");
    frame.setVisible(true);
}