void Update() { if (!_cameraTexture || !_dirtyCameraTexture) { return; } // Init. AdaptResources(); // Update mat texture ( If the texture looks correct in Unity, then it needs to be flipped for OpenCV ). TrackingToolsHelper.TextureToMat(_cameraTexture, !_flipCameraTexture, ref _camTexMat, ref _tempTransferColors, ref _tempTransferTexture); // Convert to grayscale if more than one channel, else copy (and convert bit rate if necessary). TrackingToolsHelper.ColorMatToLumanceMat(_camTexMat, _camTexGrayMat); // During testing, undistort before. if (_state == State.Testing) { Calib3d.undistort(_camTexGrayMat, _camTexGrayUndistortMat, _intrinsicsCalibrator.sensorMat, _intrinsicsCalibrator.distortionCoeffsMat); } // Find chessboard. Mat chessboardSourceMat = _state == State.Calibrating ? _camTexGrayMat : _camTexGrayUndistortMat; bool foundBoard = TrackingToolsHelper.FindChessboardCorners(chessboardSourceMat, _chessPatternSize, ref _chessCornersImageMat); if (foundBoard) { TrackingToolsHelper.DrawFoundPattern(chessboardSourceMat, _chessPatternSize, _chessCornersImageMat); } // During calibration, undistort after. if (_state == State.Calibrating) { if (_intrinsicsCalibrator.sampleCount > correctDistortionSampleCountThreshold) { Calib3d.undistort(_camTexGrayMat, _camTexGrayUndistortMat, _intrinsicsCalibrator.sensorMat, _intrinsicsCalibrator.distortionCoeffsMat); } else { _camTexGrayMat.copyTo(_camTexGrayUndistortMat); } } // State dependent updates. switch (_state) { case State.Calibrating: UpdateCalibration(foundBoard); break; case State.Testing: UpdateTesting(foundBoard); break; } // UI. Utils.fastMatToTexture2D(_camTexGrayUndistortMat, _processedCameraTexture); // Will flip as default. _previewFlasher.Update(); UpdateSampleCounterUI(); _dirtyCameraTexture = false; }
void Update() { if (Input.GetKeyDown(_interactibaleHotKey)) { if (!interactable && _cameraTexture == null) { Debug.LogWarning(logPrepend + "Missing camera texture.\n"); return; } interactable = !interactable; } if (!_cameraTexture) { return; } // Adapt resources. AdaptResources(); if (_dirtyTexture) { // Undistort. if (_cameraTexture is WebCamTexture) { Utils.webCamTextureToMat(_cameraTexture as WebCamTexture, _camTexMat, flip: false); } else if (_cameraTexture is Texture2D) { Utils.fastTexture2DToMat(_cameraTexture as Texture2D, _camTexMat, flip: false); } else { Debug.LogWarning(logPrepend + "Only Texture2D and WenCamTexture is supported.\n"); return; } TrackingToolsHelper.ColorMatToLumanceMat(_camTexMat, _camTexGrayMat); Calib3d.undistort(_camTexGrayMat, _camTexGrayUndistortMat, _cameraMatrix, _distCoeffs); Utils.fastMatToTexture2D(_camTexGrayUndistortMat, _undistortedCameraTexture); _dirtyTexture = false; } if (_interactable) { UpdateInteraction(); } if (_dirtyPoints) { UpdateCameraTransform(); _dirtyPoints = false; } }