Search code examples
javaopencvcomputer-visionjavacv

get coordinates of corners when calling findChessboardCorners


Hey, I am using JavaCV from this library org.bytedeco.javacpp.opencv_core in version 1.3.2 as a gradle dependency.

I am running this code to get the corners on the chessboard but I have troubles getting the coordinates to each of the detected corners:

  Mat imageCorners = new Mat();
  Size boardSize = new Size(9,6);
  boolean found = findChessboardCorners(capturedFrame,boardSize,imageCorners,CV_CALIB_CB_ADAPTIVE_THRESH+CV_CALIB_CB_NORMALIZE_IMAGE);

But for the 54 corners the method detects, the matrix which stores the corners has just a size of 54x1.

if (found) {
    FloatRawIndexer sI = imageCorners.createIndexer();
    for (int y = 0; y < imageCorners.rows(); y++) {
      for (int x = 0; x < imageCorners.cols(); x++) {
        logger.debug("Row: " + y + " Column " + x);
        logger.debug(sI.get(y, x));
      }
    }
  }

The logfile looks like this:

14:57:50.057 [main] DEBUG JavaCVTransformation2 - Row: 0 Column 0
14:57:50.059 [main] DEBUG JavaCVTransformation2 - 164.02007
14:57:50.059 [main] DEBUG JavaCVTransformation2 - Row: 1 Column 0
14:57:50.059 [main] DEBUG JavaCVTransformation2 - 224.07906
14:57:50.059 [main] DEBUG JavaCVTransformation2 - Row: 2 Column 0
14:57:50.059 [main] DEBUG JavaCVTransformation2 - 283.54288
14:57:50.059 [main] DEBUG JavaCVTransformation2 - Row: 3 Column 0
14:57:50.059 [main] DEBUG JavaCVTransformation2 - 343.4154
14:57:50.060 [main] DEBUG JavaCVTransformation2 - Row: 4 Column 0
14:57:50.060 [main] DEBUG JavaCVTransformation2 - 402.7718
14:57:50.060 [main] DEBUG JavaCVTransformation2 - Row: 5 Column 0
14:57:50.060 [main] DEBUG JavaCVTransformation2 - 462.38278
14:57:50.060 [main] DEBUG JavaCVTransformation2 - Row: 6 Column 0
14:57:50.060 [main] DEBUG JavaCVTransformation2 - 522.2342
14:57:50.060 [main] DEBUG JavaCVTransformation2 - Row: 7 Column 0
14:57:50.060 [main] DEBUG JavaCVTransformation2 - 580.99805
14:57:50.061 [main] DEBUG JavaCVTransformation2 - Row: 8 Column 0
14:57:50.061 [main] DEBUG JavaCVTransformation2 - 640.7774
14:57:50.061 [main] DEBUG JavaCVTransformation2 - Row: 9 Column 0
14:57:50.061 [main] DEBUG JavaCVTransformation2 - 151.04564

Drawing it works perfectly fine, so somewhere there have to be two coordinates to each corner, I think.

drawChessboardCorners(capturedFrame, boardSize, imageCorners, found);

Is there a way to get the coordinates of the corners when calling findChessboardCorners()?


Solution

  • I did not found a solution for the method findChessboardCorners, but I found another method svFindChessboardCorners from import static org.bytedeco.javacpp.opencv_calib3d.*; with which it worked.

      IplImage iplImage = frameConverter.convertToIplImage(capturedFrame);
    
      CvSize boardSize = new CvSize(9, 6);
      float[] corners = new float[108];
      int found = cvFindChessboardCorners(iplImage, boardSize, corners);
    

    This method will store the corner coordinates on the float array, so accessing the first point would be something like this:

    Point2f srcp1 = new Point2f(corners[0], corners[1]);