0

So, it seems the API changed over time.

I get as far as

import org.bytedeco.javacpp.opencv_core.{Mat, Point, Scalar}
import org.bytedeco.javacpp.{opencv_core, opencv_imgcodecs, opencv_imgproc}

val mat         = opencv_imgcodecs.imread("test-in.jpg")
val greyMat     = new Mat()
val lines       = new Mat()
opencv_imgproc.cvtColor(mat, greyMat, opencv_imgproc.CV_BGR2GRAY, 1)
val rho         = 1.0
val theta       = 1.0.toRadians
val thresh      = 50
val minLineLen  = 80
val maxLineGap  = 50

opencv_imgproc.HoughLinesP(greyMat, lines, rho, theta, thresh,
  minLineLen, maxLineGap)

for (i <- 0 until lines.rows()) {
  val pt1   = ??? : Point
  val pt2   = ??? : Point
  val colr  = new Scalar(0, 0, 255, 128)

  opencv_imgproc.line(mat, pt1, pt2, colr, 1, opencv_core.LINE_AA, 0)
}

opencv_imgcodecs.imwrite("test-out.jpg", mat)

But I don't know how to extract the points from the lines matrix. For example, this old post suggests there such thing as a MatOfInt4 which I don't find. Also, there is an answer that says I can do lines.get(0, x) on a Mat, a method that doesn't exist.

Then I find another variant that uses a UByteRawIndexer, but the cells in my matrix have three instead of four elements (this is perhaps because I call HoughLinesP and not HoughLines).

So how the heck do I get points from the HoughLinesP call? JavaCV is 1.2, OpenCV is 3.1.


If I query the lines matrix for its size, I get rows = 19167, cols = 1; I create an Indexer which appears to be a UByteRawIndexer, I get sizes = [1958, 2196, 3]. None of this makes sense to me. lines also has depth = 5 and type = 13.

Community
  • 1
  • 1
0__
  • 66,707
  • 21
  • 171
  • 266

1 Answers1

0

Seems I created the indexer from the wrong matrix (input image instead of lines), that's why it was so large and of type unsigned-byte.

Creating the indexer from the lines matrix gives an IntRawIndexer with rows corresponding to number of lines, one column, and third dimension is of size 4, giving x1, y1, x2, y2:

  val indexer: IntRawIndexer = lines.createIndexer()

  for (i <- 0 until indexer.rows().toInt /* lines.rows() */) {
    val x1    = indexer.get(i, 0, 0)
    val y1    = indexer.get(i, 0, 1)
    val x2    = indexer.get(i, 0, 2)
    val y2    = indexer.get(i, 0, 3)

    println(s"x1 = $x1, y1 = $y1, x2 = $x2, y2 = $y2")

    val pt1 = new Point(x1, y1)
    val pt2 = new Point(x2, y2)
    val colr  = new Scalar(0, 0, 255, 128)

    opencv_imgproc.line(mat, pt1, pt2, colr, 1, opencv_core.LINE_AA, 0)
  }
0__
  • 66,707
  • 21
  • 171
  • 266