使用Mat.zeros在Kotlin中创建全零矩阵时出错。

huangapple go评论136阅读模式
英文:

Error to create matrix with zeros in kotlin using Mat.zeros

问题

以下是您要翻译的内容:

我正在使用OpenCV开发一个应用程序。我正在上传一张图像,并为其分配一个与原始图像大小相同的新矩阵,但在这个矩阵中,我需要将所有行和列都填充为0。然而,Mat.zeros函数没有在我的矩阵中赋值为0,而是输出了填充有值的结果:[D @ 13a5b9d,[D @ f3978b1,[D @ 30c17d0... 另外,我需要它是一个Mat()类型的矩阵,因为我将与OpenCV一起使用它。有人可以帮助我解决这个错误吗?

代码:

val srcOriginal = Imgcodecs.imread(currentPhotoPath)

val markers = Mat.zeros(srcOriginal.rows(), srcOriginal.cols(), CvType.CV_32S)

for (x in 0..markers.rows()) {
   for (y in 0..markers.cols()) {
      Log.i("teste", markers[x, y])
  }
}

期望输出:

[[0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 ...
 [0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]]

输出:

使用Mat.zeros在Kotlin中创建全零矩阵时出错。

英文:

I'm working on developing an application using opencv. I'm uploading an image and assigning a new matrix with the same size as the original image, but in this matrix I need to fill all the rows and columns with 0. However, the function Mat.zeros is not assigning 0 in my matrix and I have the output filled with values: [D @ 13a5b9d, [D @ f3978b1, [D @ 30c17d0 ...
Oh, and I need it to be a matrix of type Mat () because I will work with opencv. Can someone help me solve this error
Code:

val srcOriginal = Imgcodecs.imread(currentPhotoPath)

val markers = Mat.zeros(srcOriginal.rows(), srcOriginal.cols(), CvType.CV_32S)

for(x in 0..markers.rows()) {
   for(y in 0..markers.cols()) {
      Log.i("teste", markers[x,y])
  }
}

Output expected:

[[0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 ...
 [0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]]

output:

使用Mat.zeros在Kotlin中创建全零矩阵时出错。

答案1

得分: 1

你的代码实际上创建了一个填充有正确值的矩阵。问题是,markers[x, y] 返回的是一个 DoubleArray 类型的值,而不是 Double,因此你无法看到实际内容。如果你使用以下代码行打印这些值,你会看到矩阵中有零:

Log.i("teste", markers[x, y].contentToString())

// 或者使用 Java 的 Arrays 类进行替代
Log.i("teste", Arrays.toString(markers[x, y]))
英文:

Your code actually creates a matrix filled with the correct values. The thing is, markers[x,y] returns a value of type DoubleArray, not Double and hence you cannot see the actual content. If you print the values with the following line you will see that there are zeros in the matrix:

Log.i("teste", markers[x,y].contentToString())

// Alternative using the Java Arrays class
Log.i("teste", Arrays.toString(markers[x,y]))

huangapple
  • 本文由 发表于 2020年5月29日 23:51:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/62089894.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定