Ask Your Question
0

How to resolve the error mentioned below (Camera Cailbration | Calib3d.java)

asked 2017-10-26 06:25:59 -0600

ChukZ gravatar image

import java.io.File; import java.util.ArrayList; import org.opencv.calib3d.Calib3d; import org.opencv.core.Core; import org.opencv.core.CvType; import org.opencv.core.Mat; import org.opencv.core.MatOfPoint2f; import org.opencv.core.MatOfPoint3f; import org.opencv.core.Point3; import org.opencv.core.Size; import org.opencv.core.TermCriteria; import org.opencv.highgui.Highgui; import org.opencv.imgproc.Imgproc;

public class CalibChessBoard { int flagsCorner = Calib3d.CALIB_CB_ADAPTIVE_THRESH | Calib3d.CALIB_CB_FAST_CHECK | Calib3d.CALIB_CB_NORMALIZE_IMAGE; int flagsCalib = Calib3d.CALIB_ZERO_TANGENT_DIST | Calib3d.CALIB_FIX_PRINCIPAL_POINT | Calib3d.CALIB_FIX_K4 | Calib3d.CALIB_FIX_K5; TermCriteria criteria = new TermCriteria(TermCriteria.EPS + TermCriteria.MAX_ITER, 40, 0.001); Size winSize = new Size(5, 5), zoneSize = new Size(-1, -1); Size patternSize; ArrayList objectPoints, imagePoints = new ArrayList(); ArrayList vCorners; ArrayList vImg; Mat cameraMatrix = Mat.eye(3, 3, CvType.CV_64F); Mat distCoeffs = Mat.zeros(8, 1, CvType.CV_64F); ArrayList rvecs = new ArrayList(); ArrayList tvecs = new ArrayList();

CalibChessBoard() {
}

CalibChessBoard(Size patternSize) {
    this.patternSize = patternSize;
}

boolean getCorners(Mat gray, MatOfPoint2f corners) {
    if (!Calib3d.findChessboardCorners(gray, patternSize,
            corners, flagsCorner))
        return false;
    Imgproc.cornerSubPix(gray, corners, winSize, zoneSize,
                               criteria);
    return true;
}

MatOfPoint3f getCorner3f() {
    MatOfPoint3f corners3f = new MatOfPoint3f();
    double squareSize = 50;
    Point3[] vp = new Point3[(int) (patternSize.height * 
                                        patternSize.width)];
    int cnt = 0;
    for (int i = 0; i < patternSize.height; ++i)
        for (int j = 0; j < patternSize.width; ++j, cnt++)
            vp[cnt] = new Point3(j * squareSize, 
                                 i * squareSize, 0.0d);
    corners3f.fromArray(vp);
    return corners3f;
}

public static void main(String[] args) {
    test0();
}

static void test0() {
    CalibChessBoard cb = new CalibChessBoard(new Size(8, 6));
    cb.getAllCornors("/the/photo/folder");
    cb.calibrate();
}

void calibrate() {
    double errReproj = Calib3d.calibrateCamera(objectPoints, 
            imagePoints,vImg.get(0).size(), cameraMatrix, 
            distCoeffs, rvecs, tvecs,flagsCalib);
    System.out.println("done, \nerrReproj = " + errReproj);
    System.out.println("cameraMatrix = \n" + cameraMatrix.dump());
    System.out.println("distCoeffs = \n" + distCoeffs.dump());
}

void getAllCornors(String path) {
    vImg = new ArrayList();
    objectPoints = new ArrayList();
    imagePoints = new ArrayList();
    MatOfPoint3f corners3f = getCorner3f();
    for (File f : new File(path).listFiles()) {
        Mat mat = Highgui.imread(f.getPath(), 
                       Highgui.CV_LOAD_IMAGE_COLOR);
        if (mat == null || mat.channels() != 3)
            continue;
        System.out.println("fn = " + f.getPath());
        System.out.println("mat.channels() = " + mat.channels() 
                + ", " + mat.cols() + ", " + mat.rows());
        Mat gray = new Mat();
        Imgproc.cvtColor(mat, gray, Imgproc.COLOR_BGR2GRAY);
        MatOfPoint2f corners = new MatOfPoint2f();
        if (!getCorners(gray, corners))
            continue;
        objectPoints.add(corners3f);
        imagePoints.add(corners);
        vImg.add(mat);
    }
}

static {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
}

}

reference:"https://computervisionandj..."

In this code there is a issue in calibrate method. cannot resolve size in vImg.get(0).size(). How to resolve this bug in this code.

edit retag flag offensive close merge delete

Comments

^^ broken link !

(and please try to format your code properly)

berak gravatar imageberak ( 2017-10-26 06:51:09 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-10-26 06:48:34 -0600

berak gravatar image

updated 2017-10-26 06:49:33 -0600

if you do like :

ArrayList vImg = new ArrayList(); // wrong

you have a list of Object , so get(0) returns an Object , which has no size() member.

you need to declare vImg as an ArrayList<Mat> , to achieve the correct type resolution, like:

ArrayList<Mat> vImg = new ArrayList(); // correct
edit flag offensive delete link more

Comments

Thank you @berak. But still there is a problem. I have included one image to the image folder .Then output comes like this.There is an exception. fn = /home/chinthaka/Downloads/wow/wow/cb1.png mat.channels() = 3, 220, 229 Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 0 out-of-bounds for length 0 at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64) at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70) at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248) at java.base/java.util.Objects.checkIndex(Objects.java:372) at java.base/java.util.ArrayList.get(ArrayList.java:439)

ChukZ gravatar imageChukZ ( 2017-10-26 06:56:23 -0600 )edit

empty array. you have to check the size, before using get(0)

also: if (mat == null) <-- this will never happen with imread() ! please check: if (mat.empty()) instead.

berak gravatar imageberak ( 2017-10-26 06:58:08 -0600 )edit

Thank your support @berak. I have solved something because of your support so far. But still there is an error like this. Could you please tell me what is that.

In method ----> void getAllCornors(String path) I removed following thing. if (!getCorners(gray, corners)) continue; Now mat is not empty.But look the output.

fn = /home/chinthaka/Downloads/wow/shape2.jpg mat.channels() = 3, 3318, 2212

OpenCV Error: Unsupported format or combination of formats (imagePoints1 should contain vector of vectors of points of type Point2f) in collectCalibrationData, file /opt/opencv-3.2.0/modules/calib3d/src/calibration.cpp, line 3115

Error is in the first line of ------>void calibrate() method.

ChukZ gravatar imageChukZ ( 2017-10-27 03:42:08 -0600 )edit

" I removed following thing. if (!getCorners(gray, corners)) " -- that might have been a bad idea, simply.

it needs to find ALL corners in the image, else you cannot use it.

maybe you can do a "human" inspection of your chessboard images, and weed out those, where corners are obstructed (hand in the way?), specular highlights, board not completely visible, etc.

berak gravatar imageberak ( 2017-10-27 03:51:50 -0600 )edit

But @berak, If i put that one to the function , function doesn't work below things under it. " objectPoints.add(corners3f); imagePoints.add(corners); vImg.add(mat); " These are don't work if i put "....if (!getCorners(gray, corners)).."

So mat is not going to add to the vImg. So In >>>>void calibrate() method , again i will get an empty array. exception will look like java.lang.IndexOutOfBoundsException: Index 0 out-of-bounds for length 0.

So I am in this trouble.Please help me to solve this one

ChukZ gravatar imageChukZ ( 2017-10-27 04:02:59 -0600 )edit

again, i suspect, that your images are just bad. (add one of them to your question, so we can look at it ?)

from here, it looks, like none of them passed the getCorners() test. you will have to check vImg.size()>0 before calling the calibration routines..

berak gravatar imageberak ( 2017-10-27 04:08:10 -0600 )edit

@berak Here I have sent link to images i am now using. link text

ChukZ gravatar imageChukZ ( 2017-10-27 04:18:36 -0600 )edit

Yes I checked vImg.size()>0 .So vImg becomes empty.

ChukZ gravatar imageChukZ ( 2017-10-27 04:25:07 -0600 )edit

wait, you're supposed to print it out, laminate it on a board, and wave it in front of your webcam, like this:

please have another look at the tutorial

if you really tried with thos images from your link, it's no wonder, they were rejected.

berak gravatar imageberak ( 2017-10-27 04:28:21 -0600 )edit

@berak, That means this code only works for real time images not the imputed images???? But in this one Article ,The author is taking images from locally .Is n't it?

static void test0() { CalibChessBoard cb = new CalibChessBoard(new Size(8, 6)); cb.getAllCornors("/the/photo/folder"); cb.calibrate(); }

ChukZ gravatar imageChukZ ( 2017-10-27 04:43:15 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-10-26 06:25:59 -0600

Seen: 824 times

Last updated: Oct 26 '17