如何为仿射变换创建形状[3,2]的OpenCV Mat?

时间:2016-11-28 00:12:19

标签: c++ opencv javacpp

使用JavaCPP绑定OpenCV 3.1,用Scala编写的代码。这些方法几乎完全映射为C ++中的OpenCV 3.1。我试图创建一个仿射变换矩阵,这样我就可以使用地标来扭曲图像。但是,getAffineTransform方法失败,并出现以下错误:

java.lang.RuntimeException: /Users/saudet/projects/bytedeco/javacpp-presets/opencv/cppbuild/macosx-x86_64/opencv-3.1.0/modules/imgproc/src/imgwarp.cpp:6360: 
error: (-215) src.checkVector(2, CV_32F) == 3 && dst.checkVector(2, CV_32F) == 3 in function getAffineTransform

    at org.bytedeco.javacpp.opencv_imgproc.getAffineTransform(flandmarkTest.sc0.tmp)
    at #worksheet#.H$lzycompute(flandmarkTest.sc0.tmp:82)
    at #worksheet#.H(flandmarkTest.sc0.tmp:82)
    at #worksheet#.get$$instance$$H(flandmarkTest.sc0.tmp:82)
    at #worksheet#.#worksheet#(flandmarkTest.sc0.tmp:236)

这是我的代码,我感觉我没有正确设置Mat的值,但我该怎么做?

val landmarkM = new Mat()
landmarkM.put(new Scalar(outerEyeLeft(0),outerEyeLeft(1)))
landmarkM.put(new Scalar(outerEyeRight(0),outerEyeRight(1)))
landmarkM.put(new Scalar(nose(0),nose(1)))
val imgDim = img_grayscale.width()
val refM = new Mat()
refM.put(new Scalar(template(1)(0)*imgDim,template(1)(1)*imgDim))
refM.put(new Scalar(template(4)(0)*imgDim,template(4)(1)*imgDim))
refM.put(new Scalar(template(5)(0)*imgDim,template(5)(1)*imgDim))
refM.checkVector(2) // returns -1
landmarkM.checkVector(2) // returns -1
val H: Mat = getAffineTransform(landmarkM, refM)

1 个答案:

答案 0 :(得分:0)

使用以下构造函数解决了我的问题:

new Mat(3,2,CV_32F)

编辑:我必须走得更远并使用索引器:

val landmarkM = new Mat(3,2,CV_32F)
val ldIdx: FloatRawIndexer = landmarkM.createIndexer()
ldIdx.put(0L,0L,Math.round(outerEyeLeft(0)).toInt)
ldIdx.put(0L,1L,Math.round(outerEyeLeft(1)).toInt)
ldIdx.put(1L,0L,Math.round(outerEyeRight(0)).toInt)
ldIdx.put(1L,1L,Math.round(outerEyeRight(1)).toInt)
ldIdx.put(2L,0L,Math.round(nose(0)).toInt)
ldIdx.put(2L,1L,Math.round(nose(1)).toInt)
ldIdx.release()
相关问题