Java help needed
from United States  [36 posts]
7 year
Hello There
I am trying to run following java code in Jscript to stitch video using video/camera but showing errors. Please help me out here as I have limited knowledge about it. I really appreciate if I can get little help in debugging these codes and run in RR using webcam.


public class ExampleVideoMosaic {
    public static void main( String args[] ) {

        // Configure the feature detector
        ConfigGeneralDetector confDetector = new ConfigGeneralDetector();
        confDetector.threshold = 1;
        confDetector.maxFeatures = 300;
        confDetector.radius = 3;

        // Use a KLT tracker
        PointTracker<GrayF32> tracker = FactoryPointTracker.klt(new int[]{1,2,4,8},confDetector,3,

        // This estimates the 2D image motion
        // An Affine2D_F64 model also works quite well.
        ImageMotion2D<GrayF32,Homography2D_F64> motion2D =
                FactoryMotion2D.createMotion2D(220,3,2,30,0.6,0.5,false,tracker,new Homography2D_F64());

        // wrap it so it output color images while estimating motion from gray
        ImageMotion2D<Planar<GrayF32>,Homography2D_F64> motion2DColor =
                new PlToGrayMotion2D<GrayF32,Homography2D_F64>(motion2D,GrayF32.class);

        // This fuses the images together
                stitch = FactoryMotion2D.createVideoStitchMS(0.5, motion2DColor, GrayF32.class);

        // Load an image sequence
        MediaManager media = DefaultMediaManager.INSTANCE;
        String fileName = UtilIO.pathExample("mosaic/airplane01.mjpeg");
        SimpleImageSequence<Planar<GrayF32>> video =
                media.openVideo(fileName, ImageType.pl(3, GrayF32.class));

        Planar<GrayF32> frame = video.next();

        // shrink the input image and center it
        Homography2D_F64 shrink = new Homography2D_F64(0.5,0,frame.width/4,0,0.5,frame.height/4,0,0,1);
        shrink = shrink.invert(null);

        // The mosaic will be larger in terms of pixels but the image will be scaled down.
        // To change this into stabilization just make it the same size as the input with no shrink.
        // process the first frame

        // Create the GUI for displaying the results + input image
        ImageGridPanel gui = new ImageGridPanel(1,2);
        gui.setImage(0,0,new BufferedImage(frame.width,frame.height,BufferedImage.TYPE_INT_RGB));
        gui.setImage(0,1,new BufferedImage(frame.width,frame.height,BufferedImage.TYPE_INT_RGB));
        gui.setPreferredSize(new Dimension(3*frame.width,frame.height*2));

        ShowImages.showWindow(gui,"Example Mosaic", true);

        boolean enlarged = false;

        // process the video sequence one frame at a time
        while( video.hasNext() ) {
            frame = video.next();
            if( !stitch.process(frame) )
                throw new RuntimeException("You should handle failures");

            // if the current image is close to the image border recenter the mosaic
            StitchingFromMotion2D.Corners corners = stitch.getImageCorners(frame.width,frame.height,null);
            if( nearBorder(corners.p0,stitch) || nearBorder(corners.p1,stitch) ||
                    nearBorder(corners.p2,stitch) || nearBorder(corners.p3,stitch) ) {

                // only enlarge the image once
                if( !enlarged ) {
                    enlarged = true;
                    // double the image size and shift it over to keep it centered
                    int widthOld = stitch.getStitchedImage().width;
                    int heightOld = stitch.getStitchedImage().height;

                    int widthNew = widthOld*2;
                    int heightNew = heightOld*2;

                    int tranX = (widthNew-widthOld)/2;
                    int tranY = (heightNew-heightOld)/2;

                    Homography2D_F64 newToOldStitch = new Homography2D_F64(1,0,-tranX,0,1,-tranY,0,0,1);

                    stitch.resizeStitchImage(widthNew, heightNew, newToOldStitch);
                    gui.setImage(0, 1, new BufferedImage(widthNew, heightNew, BufferedImage.TYPE_INT_RGB));
                corners = stitch.getImageCorners(frame.width,frame.height,null);
            // display the mosaic
            ConvertBufferedImage.convertTo(frame,gui.getImage(0, 0),true);
            ConvertBufferedImage.convertTo(stitch.getStitchedImage(), gui.getImage(0, 1),true);

            // draw a red quadrilateral around the current frame in the mosaic
            Graphics2D g2 = gui.getImage(0,1).createGraphics();


            // throttle the speed just in case it's on a fast computer

     * Checks to see if the point is near the image border
    private static boolean nearBorder( Point2D_F64 p , StitchingFromMotion2D<?,?> stitch ) {
        int r = 10;
        if( p.x < r || p.y < r )
            return true;
        if( p.x >= stitch.getStitchedImage().width-r )
            return true;
        if( p.y >= stitch.getStitchedImage().height-r )
            return true;

        return false;
Steven Gentner from United States  [273 posts] 7 year

JScript isn't Java. Its just a scripting language created for the web a couple years ago called Javascript. They are very VERY different.

You will not be able to run that code within RR. It has to be run outside in a traditional development environment with calls to RR using the API. There is a lot of extra framework (ConfigGeneralDetector, FactoryPointTracker, etc. etc, etc. ) that RR knows nothing about.


This forum thread has been closed due to inactivity (more than 4 months) or number of replies (more than 50 messages). Please start a New Post and enter a new forum thread with the appropriate title.

 New Post   Forum Index