我尝试了很多搜索,但似乎无法弄清楚。我阅读的所有线程都表明每个人都在做同一件事。但这似乎对我不起作用。
我想创建一个新图像并将两个图像添加到此创建的图像。
Mat.zeros
,我将在上面添加我的两个图像(让我说A和B。我将零称为Mat G)。G.submat
。G.submat
。Mat.zeros
)。我猜
copyTo
并没有真正将G的ROI上的内容从A复制到G。我不确定自己在做什么错。
我正在使用Kotlin,它可以编译为Java。
@PostConstruct
fun init() {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
}
@Test
fun `merge image`() {
val frameLeft = Imgcodecs.imread("/home/nischit/Downloads/potato1.jpg")
val frameRight = Imgcodecs.imread("/home/nischit/Downloads/potato2.jpg")
val frameOutput = Mat.zeros(1920, 1080, frameLeft.depth())
println(frameLeft.depth())
println(frameRight.depth())
println(frameOutput.depth())
val tempLeft = Mat()
val tempRight = Mat()
scaleFrame(frameLeft, tempLeft)
scaleFrame(frameRight, tempRight)
println("tempLeft: ${tempLeft.cols()},${tempLeft.rows()}")
println("tempRight: ${tempRight.cols()},${tempRight.rows()}")
tempLeft.copyTo(frameOutput.submat(Rect(10, 10, tempLeft.cols(), tempLeft.rows())))
tempRight.copyTo(frameOutput.submat(Rect(10, 500, tempRight.cols(), tempRight.rows())))
saveImage(frameOutput, 2)
}
fun scaleFrame(frame: Mat, out: Mat, maxWidth: Int = MAX_WIDTH, maxHeight: Int = MAX_HEIGHT) {
val isFrameInPortraitMode = frame.height() > frame.width()
val scale = if (isFrameInPortraitMode) {
(maxHeight * 1.0 / frame.height())
} else {
(maxWidth * 1.0 / frame.width())
}
Imgproc.resize(frame, out, Size(frame.width() * scale, frame.height() * scale))
}
fun saveImage(frame: Mat, i: Int = 0) {
Imgcodecs.imwrite("/home/nischit/Downloads/potatoGenerated$i.jpg", frame)
}
最佳答案
好的,问题出在我创建的空白图片中的 channel ,即frameOutput
。所有输入都有3个通道,而此矩阵只有1个通道,因为我没有正确初始化它。
更换val frameOutput = Mat.zeros(1920, 1080, frameLeft.depth())
与val frameOutput = Mat.zeros(Size(1920.0, 1080.0), CvType.CV_8UC3)
一起工作。