Ask Your Question
0

Binarization of real-time video

asked 2017-10-31 06:34:49 -0600

ChukZ gravatar image

updated 2017-11-01 05:21:10 -0600

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 add frames to output a video???.

@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);
}
edit retag flag offensive close merge delete

Comments

explain "binarization", please.

do you need something like: grayscale -> threshold -> save to video ?

and again -- show, what you tried so far. (we probably won't write your program)

berak gravatar imageberak ( 2017-10-31 06:52:07 -0600 )edit

@berak , I have edited the Question again and put my code here. Could you please help me now.

ChukZ gravatar imageChukZ ( 2017-11-01 00:50:41 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2017-11-01 05:38:04 -0600

berak gravatar image

updated 2017-11-01 05:41:32 -0600

i can't try (no java here), but i guess, you wanted something like this:

VideoCapture camera = new VideoCapture(filePath);
if (!camera.isOpened()) {
    System.out.println("Error! Camera can't be opened!");
    return;
}
int w=camera.get(CAP_PROP_FRAME_WIDTH);
int h=camera.get(CAP_PROP_FRAME_HEIGHT);
int fourcc = VideoWriter.fourcc('M','J','P','G');
VideoWriter writer = new VideoWriter("my.avi",
                             fourcc, 25, new Size(w,h), false); 
// the false is for writing grayscale (1 channel) images

Mat frame = new Mat();
while (true) {
    if (camera.read(frame)) {
    ...

    writer.write(binary_img);
    ...
    }
}
writer.release();

please also see docs

edit flag offensive delete link more

Comments

Thank You very much @berak. And program outputs a video(black and white).But outputted video looks like this output one......giphy.mp4 is input video and other is output video.How to fix that bug.

ChukZ gravatar imageChukZ ( 2017-11-01 06:54:35 -0600 )edit

i won't download your videos, but if you put an image of the "wrong" thing here, i'll look at it.

berak gravatar imageberak ( 2017-11-01 10:39:03 -0600 )edit

If we use following thing can i use false in VideoWriter writer = new VideoWriter("my.avi", fourcc, 25, new Size(w,h), false);

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

It doesn't work. It produces a video but it doesn't relate to the original one. But If i don't use grayscaole convert or binary convert and put true in VideoWriter , program exactly work to the colored video.

ChukZ gravatar imageChukZ ( 2017-11-02 00:01:48 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-10-31 06:34:49 -0600

Seen: 1,735 times

Last updated: Nov 01 '17