Exemplo n.º 1
0
 /// <summary>
 /// Raises the mat capture method dropdown value changed event.
 /// </summary>
 public void OnMatCaptureMethodDropdownValueChanged(int result)
 {
     if ((int)matCaptureMethod != result)
     {
         matCaptureMethod = (MatCaptureMethod)result;
         if (fpsMonitor != null)
         {
             fpsMonitor.consoleText = "";
         }
     }
 }
Exemplo n.º 2
0
 public void OnMatCaptureMethodDropdownValueChanged(int result)
 {
     matCaptureMethod = (MatCaptureMethod)result;
 }
Exemplo n.º 3
0
        /// <summary>
        /// Gets the current camera preview frame that converted to the correct direction in OpenCV Matrix format.
        /// </summary>
        private Mat GetMat(MatCaptureMethod matCaptureMethod = MatCaptureMethod.NatCam_CaptureFrame)
        {
            if (matrix.cols() != NatCam.Preview.width || matrix.rows() != NatCam.Preview.height)
            {
                return(null);
            }

            switch (matCaptureMethod)
            {
            default:
            case MatCaptureMethod.NatCam_CaptureFrame:
                // Get the preview data
                // Set `flip` flag to true because OpenCV uses inverted Y-coordinate system
                NatCam.CaptureFrame(pixelBuffer, true);

                Utils.copyToMat(pixelBuffer, matrix);

                break;

            case MatCaptureMethod.NatCam_CaptureFrame_OpenCVFlip:
                // Get the preview data
                NatCam.CaptureFrame(pixelBuffer, false);

                Utils.copyToMat(pixelBuffer, matrix);

                // OpenCV uses an inverted coordinate system. Y-0 is the top of the image, whereas in OpenGL (and so NatCam), Y-0 is the bottom of the image.
                Core.flip(matrix, matrix, 0);

                break;

            case MatCaptureMethod.BlitWithReadPixels:

                // When NatCam.PreviewMatrix function does not work properly. (Zenfone 2)
                // The workaround for a device like this is to use Graphics.Blit with Texture2D.ReadPixels and Texture2D.GetRawTextureData/GetPixels32 to download the pixel data from the GPU.
                // Blit the NatCam preview to a temporary render texture; set the RT active and readback into a Texture2D (using ReadPixels), then access the pixel data in the texture.
                // The texture2D's TextureFormat needs to be RGBA32(Unity5.5+), ARGB32, RGB24, RGBAFloat or RGBAHalf.
                Utils.textureToTexture2D(NatCam.Preview, texture);

                Utils.copyToMat(texture.GetRawTextureData(), matrix);

                // OpenCV uses an inverted coordinate system. Y-0 is the top of the image, whereas in OpenGL (and so NatCam), Y-0 is the bottom of the image.
                Core.flip(matrix, matrix, 0);

                break;

            case MatCaptureMethod.Graphics_CopyTexture:

                // When NatCam.PreviewMatrix function does not work properly. (Zenfone 2)
                if (SystemInfo.copyTextureSupport != UnityEngine.Rendering.CopyTextureSupport.None)
                {
                    Graphics.CopyTexture(NatCam.Preview, texture);

                    Utils.copyToMat(texture.GetRawTextureData(), matrix);

                    // OpenCV uses an inverted coordinate system. Y-0 is the top of the image, whereas in OpenGL (and so NatCam), Y-0 is the bottom of the image.
                    Core.flip(matrix, matrix, 0);
                }
                else
                {
                    if (fpsMonitor != null)
                    {
                        fpsMonitor.consoleText = "SystemInfo.copyTextureSupport: None";
                    }
                    return(null);
                }

                break;
            }

            return(matrix);
        }