Question

Is there any example of how to combine two or more images side by side? In JAVA

I tried to adapt c++ code but without success.

Mat m = new Mat(imageA.cols(), imageA.rows() + imageB.rows(), imageA.type());

m.adjustROI(0, 0, imageA.cols(), imageA.rows());
imageA.copyTo(m);

m.adjustROI(0, imageA.rows(), imageB.cols(), imageB.rows());
imageB.copyTo(m);

This will always give m as imageA. Method A.copyTo(B) always result like B == A

Almost every example in c++ contains cvCopy(arg1, arg2); it looks like java analog is A.copyTo(B)

But when I use A.copyTo(B), I always get image with width, height and content of A even if B was bigger.

Was it helpful?

Solution 2

private Mat addTo(Mat matA, Mat matB) {
    Mat m = new Mat(matA.rows(), matA.cols() +  matB.cols(), matA.type());
    int aCols = matA.cols();
    int aRows = matA.rows();
    m.rowRange(0, aRows-1).colRange(0, aCols-1) = matA;
    m.rowRange(0, aRows-1).colRange(aCols, (aCols*2)-1) = matB;
    return m;
}

I didn't try to run it, but I believe it will work. I assume matA and matB will have same size and same type. Even if it doesn't work, there must be some little syntax errors or etc. You shouldn't be putting pixels values by using 4 for loops!

OTHER TIPS

You can actually use hconcat/vconcat functions:

Mat dst = new Mat();
List<Mat> src = Arrays.asList(mat1, mat2);
Core.hconcat(src, dst);
//Core.vconcat(src, dst);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top