Opencv-Java append image to existing image

asked 2014-04-01 08:57:01 -0600

IBalic gravatar image

I am trying to add an image below an original image. Then the new image will overwrite the old one. This method is run multiple times.

public void appendImage(Mat matToAppend){
        Mat orgMat = Highgui.imread("test.tiff");
        Mat newMat = new Mat();
        if(orgMat.total()==0){ // if no image exists
            orgMat = matToAppend;
            Highgui.imwrite("test.tiff", orgMat);
        }
        else{
            int width = 0;
            if(matToAppend.width() > orgMat.width()) // selects the biggest width
                width = matToAppend.width();
            else
                width = orgMat.width();
            int height = orgMat.height()+matToAppend.height(); // total height is the two images added together
            newMat = new Mat(new Size(width, height), orgMat.type(), new Scalar(255, 255, 255));

            Mat orgTarget = newMat.submat(new Rect(new Point(0, 0), new Point(orgMat.width(), orgMat.height())));
            orgMat.copyTo(orgTarget); //copy old image to upper area

            Mat newTarget = newMat.submat(new Rect(new Point(0, orgMat.height()), new Point(matToAppend.width(), newMat.height())));
            matToAppend.copyTo(newTarget); // copy new image to bottom area
            Highgui.imwrite("test.tiff", newMat);
        }
    }

What happening is that only the first image is placed at the top whereas the images appended afterwards are not seen. Running this method 4 times will result in a final image with the height of all images, but only the first image is placed at top, and the rest of the final image is white.

Why aren't the other images showing?

edit retag flag offensive close merge delete