2

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:

enter image description here

xiawi
  • 1,772
  • 4
  • 19
  • 21
Carlos Diego
  • 348
  • 5
  • 20

2 Answers2

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))
}
Animesh Sahu
  • 7,445
  • 2
  • 21
  • 49
  • Animesh this code snippet is a piece of code that I am trying to convert from python to kotlin. So far you are helping me a lot. Can you help me complete this mission? here this question https://stackoverflow.com/questions/62008889/how-to-convert-a-targeting-code-in-python-to-kotlin – Carlos Diego May 29 '20 at 14:10
  • @CarlosDiego don't know much about Python sorry, even if its syntax is simple, I find it confusing to me :( – Animesh Sahu May 30 '20 at 05:46
0

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.

user2199860
  • 788
  • 4
  • 14
  • so, how do I go through this mask of 0 to insert a new value and complete the conversion of that code in python to kotlin? so, how do I go through this mask of 0 to insert a new value and complete the conversion of that code in python to kotlin? Because it is done in python without a loop, and as in the application will be several values, I need to fill this mask with a loop. – Carlos Diego May 28 '20 at 15:26
  • Just walk it as a multidimensional array e.g. nested for loops. – user2199860 May 28 '20 at 15:28