public void Process() { Mat m = new Mat(new System.Drawing.Size(640, 480), DepthType.Cv8U, 3); Mat mProcessed = new Mat(); while (true) { if (_captureEnabled) { try { //Read the camera data to the mat //Must use VideoCapture.Read function for UWP to read image from capture. //Note that m is in 3 channel RGB color space, //our default color space is BGR for 3 channel Mat bool grabbed = _capture.Grab(); bool retrieved = _capture.Retrieve(m); //_capture.Read(m); WinrtInvoke.WinrtImshow(); if (!m.IsEmpty) { if (_cameraMatrix == null || _distCoeffs == null) { //Create a dummy camera calibration matrix for testing //Use your own if you have calibrated your camera _cameraMatrix = new Mat(new System.Drawing.Size(3, 3), DepthType.Cv32F, 1); int centerY = m.Width >> 1; int centerX = m.Height >> 1; //CvInvoke.SetIdentity(_cameraMatrix, new MCvScalar(1.0)); _cameraMatrix.SetTo(new float[] { 1f, 0f, (float)centerY, 0f, 1f, (float)centerX, 0f, 0f, 1f }); _distCoeffs = new Mat(new System.Drawing.Size(5, 1), DepthType.Cv32F, 1); _distCoeffs.SetTo(new float[] { -0.000003f, 0f, 0f, 0f, 0f }); mapx = new Matrix <float>(m.Height, m.Width); mapy = new Matrix <float>(m.Height, m.Width); CvInvoke.InitUndistortRectifyMap( _cameraMatrix, _distCoeffs, null, _cameraMatrix, m.Size, DepthType.Cv32F, mapx, mapy); } m.CopyTo(mProcessed); CvInvoke.Undistort(m, mProcessed, _cameraMatrix, _distCoeffs); //mProcess is in the same color space as m, which is RGB, //needed to change to BGR CvInvoke.CvtColor(mProcessed, mProcessed, ColorConversion.Rgb2Bgr); //Can apply simple image processing to the captured image, let just invert the pixels //CvInvoke.BitwiseNot(m, m); //render the processed image on the top image view CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() => { var wb = mProcessed.ToWritableBitmap(); image1.Source = wb; }); //The data in the mat that is read from the camera will //be drawn to the Image control WinrtInvoke.WinrtImshow(); } } catch (Exception e) { SetText(e.Message); } } else { if (_capture != null) { _capture.Dispose(); _capture = null; } Task t = Task.Delay(100); t.Wait(); } } }
public void Process() { while (true) { if (_capture != null) { bool grabbed = _capture.Grab(); //Read the camera data to the mat //Note that m is in 3 channel RGB color space, //Open CV's default color space is BGR for 3 channel Mat bool retrieved = _capture.Retrieve(_capturedMat); if (grabbed && retrieved) { try { if (!_capturedMat.IsEmpty) { //The data in the mat that is read from the camera will //be drawn to the Image control WinrtInvoke.WinrtImshow(); //Extra processing //Try to distort the image and render the processed result .... if (_cameraMatrix == null || _distCoeffs == null) { //Create a dummy camera calibration matrix for testing //Use your own if you have calibrated your camera _cameraMatrix = new Mat(new System.Drawing.Size(3, 3), DepthType.Cv32F, 1); int centerY = _capturedMat.Width >> 1; int centerX = _capturedMat.Height >> 1; //CvInvoke.SetIdentity(_cameraMatrix, new MCvScalar(1.0)); _cameraMatrix.SetTo(new float[] { 1f, 0f, (float)centerY, 0f, 1f, (float)centerX, 0f, 0f, 1f }); _distCoeffs = new Mat(new System.Drawing.Size(5, 1), DepthType.Cv32F, 1); _distCoeffs.SetTo(new float[] { -0.000003f, 0f, 0f, 0f, 0f }); mapx = new Matrix <float>(_capturedMat.Height, _capturedMat.Width); mapy = new Matrix <float>(_capturedMat.Height, _capturedMat.Width); CvInvoke.InitUndistortRectifyMap( _cameraMatrix, _distCoeffs, null, _cameraMatrix, _capturedMat.Size, DepthType.Cv32F, mapx, mapy); } _capturedMat.CopyTo(_processed); CvInvoke.Undistort(_capturedMat, _processed, _cameraMatrix, _distCoeffs); //mProcess is in the same color space as m, which is RGB, //needed to change to BGR CvInvoke.CvtColor(_processed, _processed, ColorConversion.Rgb2Bgr); //Can apply simple image processing to the captured image, let just invert the pixels //CvInvoke.BitwiseNot(m, m); //render the processed image on the top image view CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() => { var wb = _processed.ToWritableBitmap(); imageProcessed.Source = wb; }); } } catch (Exception e) { SetText(e.Message); } } } } }