Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MotionDetector example to JavaFX? #1101

Closed
mjackstone6 opened this issue Dec 4, 2018 · 5 comments
Closed

MotionDetector example to JavaFX? #1101

mjackstone6 opened this issue Dec 4, 2018 · 5 comments
Labels

Comments

@mjackstone6
Copy link

mjackstone6 commented Dec 4, 2018

Hello,

I am currently working through the MotionDetector sample at "https://github.com/bytedeco/javacv/blob/master/samples/MotionDetector.java" but I am having trouble getting this into JavaFX.

The part I am having trouble with is displaying each frame into an ImageView rather than a CanvasFrame. I used a JavaFXFrameConverter to convert IplImage but I got an error where it only supports 3 channels so I modified the code to create an IplImage with 3 channels. When running the code, I run into "The crash happened outside the Java Virtual Machine in native code."

Is there a better a way to do this example properly in JavaFX?

I have modified the code into:

@FXML
public void play() throws FrameGrabber.Exception {
    OpenCVFrameGrabber grabber = new OpenCVFrameGrabber("test.mp4");
    OpenCVFrameConverter.ToIplImage converter = new OpenCVFrameConverter.ToIplImage();
    grabber.start();

    IplImage frame = converter.convert(grabber.grab());
    IplImage image = null;
    IplImage prevImage = null;
    IplImage diff = null;

    JavaFXFrameConverter frameConverter = new JavaFXFrameConverter();

    CvMemStorage storage = CvMemStorage.create();

    while ((frame = converter.convert(grabber.grab())) != null) {
        cvClearMemStorage(storage);

        cvSmooth(frame, frame, CV_GAUSSIAN, 9, 9, 2, 2);
        if (image == null) {
            image = IplImage.create(frame.width(), frame.height(), IPL_DEPTH_8U, 3);
            cvCvtColor(frame, image, CV_RGB2GRAY);
        } else {
            prevImage = image;
            image = IplImage.create(frame.width(), frame.height(), IPL_DEPTH_8U, 3);
            cvCvtColor(frame, image, CV_RGB2GRAY);
        }

        if (diff == null) {
            diff = IplImage.create(frame.width(), frame.height(), IPL_DEPTH_8U, 3);
        }

        if (prevImage != null) {
            // perform ABS difference
            cvAbsDiff(image, prevImage, diff);
            // do some threshold for wipe away useless details
            cvThreshold(diff, diff, 64, 255, CV_THRESH_BINARY);

            Frame f = converter.convert(diff);
            Image im = frameConverter.convert(f);

            Platform.runLater(() -> mediaView.setImage(im));

        }
    }
    grabber.stop();

}
@teocci
Copy link

teocci commented Dec 5, 2018

can you be more specific about your error? I mean in which line the exception occurred? what is the error you got? did you got something like this signal 11 (SIGSEGV)??

@saudet saudet added the bug label Dec 5, 2018
saudet added a commit to bytedeco/javacpp-presets that referenced this issue Dec 5, 2018
… factory methods that prevent premature deallocation (issue bytedeco/javacv#1101)
@saudet
Copy link
Member

saudet commented Dec 5, 2018

The cause of this issue is probably similar to bytedeco/javacpp#272. I've just fixed this for IplImage as well, so please give it a try with the snapshots: http://bytedeco.org/builds/

It is also possible to work around this by making sure that OpenCVFrameConverter doesn't get deallocated, by storing it in a field, for example.

In any case, IplImage is deprecated, so I would recommend switching to Mat anyway.

@mjackstone6
Copy link
Author

I've tried testing on a snapshot and also converting to a field but I get the same error.
Here is the log file: https://pastebin.com/ZrZnDBwA

All the examples I've seen are using IplImage so I'm confused at what I should replace methods like:
cvInRangeS and cvSmooth with?

@saudet
Copy link
Member

saudet commented Dec 5, 2018 via email

@saudet
Copy link
Member

saudet commented Jan 12, 2019

The fix has now been released with JavaCV 1.4.4, enjoy! And thanks for reporting this issue.

@saudet saudet closed this as completed Jan 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants