OpenCV java。多个图像并排

时间:2014-05-11 13:52:01

标签: java opencv

有没有关于如何并排组合两个或多个图像的示例? 在JAVA中

我尝试调整c ++代码但没有成功。

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);

这始终会mimageA。方法A.copyTo(B)的结果总是B == A

几乎c ++中的每个例子都包含cvCopy(arg1, arg2);,看起来java模拟是A.copyTo(B)

但是当我使用A.copyTo(B)时,即使B较大,我总是得到宽度,高度和A含量的图像。

3 个答案:

答案 0 :(得分: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;
}

我没有尝试运行它,但我相信它会起作用。我假设matA和matB将具有相同大小相同类型。即使它不起作用,也必须有一些语法错误等。你不应该通过使用4 for for循环来放置像素值!

答案 1 :(得分:2)

您实际上可以使用hconcat / vconcat函数:

Mat dst = new Mat();
List<Mat> src = Arrays.asList(mat1, mat2);
Core.hconcat(src, dst);
//Core.vconcat(src, dst);

答案 2 :(得分:0)

最后事实证明,没有本机解决方案我能做的最好就是让我自己的功能将像素并排添加到新矩阵中。

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();
    for (int i = 0; i < aRows; i++) {
        for (int j = 0; j < aCols; j++) {
            m.put(i, j, matA.get(i, j));
        }
    }
    for (int i = 0; i < matB.rows(); i++) {
        for (int j = 0; j < matB.cols(); j++) {
            m.put(i, aCols + j, matB.get(i, j));
        }
    }
    return m;
}

这将生成一个新的Mat,左侧为matA,右侧为matB。 这是一个非常简单的实现,仅在matA.rows() >= matB.rows()且两者具有完全相同的通道数时才有效。 但无论如何,这是一个很好的起点。