EnoxSoftware / HoloLensWithOpenCVForUnityExample

HoloLens With OpenCVforUnity Example (Support for Hololens1 and Hololens2)
MIT License
262 stars 50 forks source link

Calib3d.findChessboardCorners always returns true #9

Closed bobbycho closed 5 years ago

bobbycho commented 7 years ago

Hi,

Using OptimizationWebCamTextureToMatHelper for HoloLens, I call findChessboardCorners as Calib3d.findChessboardCorners(gray, boardDimensions, corners, flagsCorner) once the . It always returns true even though there is no checssboard in the gray mat.

Codes:

In Update() following as your example,

Mat rgbaMat = webCamTextureToMatHelper.GetDownScaleMat(webCamTextureToMatHelper.GetMat()); Imgproc.cvtColor(rgbaMat, grayMat, Imgproc.COLOR_RGBA2GRAY); MatOfPoint2f points = new MatOfPoint2f(); patternFound = getCorners(grayMat, points); <-- patternFound is always true;

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

Thanks for your help, Bobby

EnoxSoftware commented 7 years ago

Thank you for your inquiry. Does your code always return true even when executed in Editor?

Also, Does this code work correctly?

        Texture2D imgTexture = Resources.Load ("chessboard") as Texture2D;
        //Texture2D imgTexture = Resources.Load ("lena") as Texture2D;

        Mat imgMat = new Mat (imgTexture.height, imgTexture.width, CvType.CV_8UC1);

        Utils.texture2DToMat (imgTexture, imgMat);
        Debug.Log ("imgMat.ToString() " + imgMat.ToString ());

        Size patternSize = new Size (9, 6);
        MatOfPoint2f corners = new MatOfPoint2f ();
        bool flag = Calib3d.findChessboardCorners (imgMat, patternSize, corners);

        Debug.Log ("flag " + flag);

        Texture2D texture = new Texture2D (imgMat.cols (), imgMat.rows (), TextureFormat.RGBA32, false);
        Utils.matToTexture2D (imgMat, texture);

        gameObject.GetComponent<Renderer> ().material.mainTexture = texture;

chessboard

lena

bobbycho commented 7 years ago

Thank you for your guide. I will test your code right now.

Thanks again, Bobby

bobbycho commented 7 years ago

Although your code works correctly in the Editor, it does not work on HoloLens. It always returns true as I mentioned before. Thanks, Bobby

EnoxSoftware commented 7 years ago

Thank you very much for reporting. I succeeded in reproducing this bug. I am investigating the cause now.

For now, It is possible to determine if a corner has been detected using this code.

Size patternSize = new Size (9, 6);
MatOfPoint2f corners = new MatOfPoint2f ();
Calib3d.findChessboardCorners (imgMat, patternSize, corners);
if(corners.rows() == 0){
    return false;
}else{
    return true;
}