Capturing and editing video stream with JavaCV cause malloc error

398 views
Skip to first unread message

JanG

unread,
Aug 16, 2013, 1:07:12 PM8/16/13
to jav...@googlegroups.com

I'm trying to add some picture ( for example watermark) to video stream which I'm capturing and editing on the fly with JavaCV.

I have latest 2.4.6 OpenCV version build on my Mac and have downloaded latest JavaCV version ( I have only some libs).

This is my grabbing code where I'm also calling another function to add external picture to the grabbed frame( like on example http://code.google.com/p/javacv/wiki/OpenCV2_Cookbook_Examples_Chapter_2).

    OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0);
    grabber.start();
    IplImage grabbedImage = grabber.grab();

    CanvasFrame canvasFrame = new CanvasFrame("Cam");
    canvasFrame.setCanvasSize(grabbedImage.width(), grabbedImage.height());

    System.out.println("framerate = " + grabber.getFrameRate());
    grabber.setFrameRate(grabber.getFrameRate());
    IplImage nn = new IplImage();
      while (canvasFrame.isVisible() && (grabbedImage = grabber.grab()) != null) {
        nn = ImageDecorator.addOverlayImage(grabbedImage, "/Users/user/app/eclipse/JavaCV/resources/logo.png");
        canvasFrame.showImage(nn);

    }

    grabber.stop();
    canvasFrame.dispose();
}

And here is addOverlayImage operation:

      public static IplImage addOverlayImage(IplImage bg, String overlayPath){


    IplImage mask = cvLoadImage(overlayPath, CV_LOAD_IMAGE_GRAYSCALE);
    IplImage overlay = cvLoadImage(overlayPath, CV_LOAD_IMAGE_COLOR);


    IplROI roi = new IplROI();
    roi.xOffset(385);
    roi.yOffset(270);
    roi.width(overlay.width());
    roi.height(overlay.height());


    IplImage backImageWithRoi = bg.roi(roi);

    cvCopy(overlay, backImageWithRoi, mask );
    roi = null;
    backImageWithRoi = null;
    return bg;
}

When I run it, I can see first frame from the camera with attached my logo.png ( what is also strange it is not centered as it was when I'm not calling addOverlayImage ) and it hangs on this frame. It seams that during second loop on while I'm getting following error.

fframerate = 0.0
OpenCV Error: Assertion failed (dst.data == dst0.data) in cvCvtColor, file /Users/User/app/video/opencv/modules/imgproc/src/color.cpp, line 3912
Exception in thread "main" java.lang.RuntimeException: /Users/User/app/video/opencv/modules/imgproc/src/color.cpp:3912: error: (-215) dst.data == dst0.data in function cvCvtColor

    at com.googlecode.javacv.cpp.opencv_highgui.cvGrabFrame(Native Method)
    at com.googlecode.javacv.OpenCVFrameGrabber.grab(OpenCVFrameGrabber.java:239)
    at CameraCapture.main(CameraCapture.java:26)

This is line 26.

  while (canvasFrame.isVisible() && (grabbedImage = grabber.grab()) != null) {


 

I have tried to use copy of my frame image and I have changed addOverlayImage adding new image.

public static IplImage addOverlayImage(IplImage bg, String overlayPath){


        IplImage mask = cvLoadImage(overlayPath, CV_LOAD_IMAGE_GRAYSCALE);
        IplImage overlay = cvLoadImage(overlayPath, CV_LOAD_IMAGE_COLOR);
        **IplImage image = bg.clone();**

        IplROI roi = new IplROI();
        roi.xOffset(385);
        roi.yOffset(270);
        roi.width(overlay.width());
        roi.height(overlay.height());


        IplImage backImageWithRoi = image.roi(roi);

        cvCopy(overlay, backImageWithRoi, mask );
        roi = null;
        backImageWithRoi = null;
        return image;


    }

With this change I'm getting this error :(

framerate = 0.0
java(15232,0x109853000) malloc: *** error for object 0x7fa800000000: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug

Could anybody please help me to find the reason of it and tell me what I'm doing wrong at this code ?

Samuel Audet

unread,
Aug 17, 2013, 5:27:00 AM8/17/13
to jav...@googlegroups.com
Hello,

On 08/17/2013 02:07 AM, JanG wrote:
> Could anybody please help me to find the reason of it and tell me what I'm doing wrong at this code ?

To set the ROI, can you try to use cvSetImageROI(), instead of directly
modifying the roi field? That is probably what is causing problems..

Samuel
Reply all
Reply to author
Forward
0 new messages