问题
I am working with opencv on android for the development of an image segmentation application, but with the watershed algorithm. I am opening an image and creating a mask with the same image size and passing 0 for all the rows and columns of that mask. However, in the next step, which is to go through an array of 0 and add as coordinates in that array, I have the error:
java.lang.NumberFormatException: For input string: "Mat [0 * 0 * CV_32FC1, isCont = true, isSubmat = false, nativeObj = 0x78a0dff700, dataAddr = 0x0] "
With that, it is not possible to pass the new values to an array, can someone help me with this?
Code:
// Load the image
val srcOriginal = Imgcodecs.imread(currentPhotoPath)
// Create a blank image of zeros (same dimension as img)
val markers = Mat.zeros(srcOriginal.rows(), srcOriginal.cols(), CvType.CV_32F)
// Example assigning a new value to a matrix index
for (i in 0 until markers.toInt()) {
markers.put(my_canvas.pointsToDrawY.get(i).toInt(), my_canvas.pointsToDrawY.get(i).toInt(), intArrayOf(0,0,255))
}
Error:
回答1:
So I finally understood the problem. The line
Mat.zeros(srcOriginal.rows(), srcOriginal.cols(), CvType.CV_32F)
says to create a Mat of srcOriginal.rows()
by srcOriginal.cols()
pixels.
Now you have to loop through it rows and columns to set their color values in RGB. In other words you have to set all the column values for 0th row, then all the column values for 1st row, and so on.
So you have to loop twice, one for row and one for column. You can either use two for loops. I'll extract them into an inline function so that they will be easier to manage afterwards.
// function declaration toplevel / or in class
inline fun loopThrough(rows: Int, cols: Int, block: (Int, Int) -> Unit) {
for(r in 0 until rows) {
for (c in 0 until cols) block(r, c)
}
}
// code here
val rows = srcOriginal.rows()
val cols = srcOriginal.cols()
val markers = Mat.zeros(rows, cols, CvType.CV_32F)
loopThrough(rows, cols) { row, col ->
markers.put(row, col, intArrayOf(0,0,255))
}
回答2:
I don't think you can use markers.toInt() in the for loop. Markers is a multidimensional array and can not be converted to an Integer.
来源:https://stackoverflow.com/questions/62067408/for-input-string-mat-00cv-32fc1-iscont-true-issubmat-false-nativeobj-0x