/// <summary> /// Handles a new CPU image. /// </summary> /// <param name="width">Width of the image, in pixels.</param> /// <param name="height">Height of the image, in pixels.</param> /// <param name="rowStride">Row stride of the image, in pixels.</param> /// <param name="pixelBuffer">Pointer to raw image buffer.</param> /// <param name="bufferSize">The size of the image buffer, in bytes.</param> private void OnImageAvailable( int width, int height, int rowStride, IntPtr pixelBuffer, int bufferSize) { if (!EdgeDetectionBackgroundImage.enabled) { return; } if (_edgeDetectionBackgroundTexture == null || _edgeDetectionResultImage == null || _edgeDetectionBackgroundTexture.width != width || _edgeDetectionBackgroundTexture.height != height) { _edgeDetectionBackgroundTexture = new Texture2D(width, height, TextureFormat.R8, false, false); _edgeDetectionResultImage = new byte[width * height]; _cameraImageToDisplayUvTransformation = Frame.CameraImage.ImageDisplayUvs; } if (_cachedOrientation != Screen.orientation || _cachedScreenDimensions.x != Screen.width || _cachedScreenDimensions.y != Screen.height) { _cameraImageToDisplayUvTransformation = Frame.CameraImage.ImageDisplayUvs; _cachedOrientation = Screen.orientation; _cachedScreenDimensions = new Vector2(Screen.width, Screen.height); } // Detect edges within the image. if (EdgeDetector.Detect( _edgeDetectionResultImage, pixelBuffer, width, height, rowStride)) { // Update the rendering texture with the edge image. _edgeDetectionBackgroundTexture.LoadRawTextureData(_edgeDetectionResultImage); _edgeDetectionBackgroundTexture.Apply(); EdgeDetectionBackgroundImage.material.SetTexture( "_ImageTex", _edgeDetectionBackgroundTexture); const string TOP_LEFT_RIGHT = "_UvTopLeftRight"; const string BOTTOM_LEFT_RIGHT = "_UvBottomLeftRight"; EdgeDetectionBackgroundImage.material.SetVector(TOP_LEFT_RIGHT, new Vector4( _cameraImageToDisplayUvTransformation.TopLeft.x, _cameraImageToDisplayUvTransformation.TopLeft.y, _cameraImageToDisplayUvTransformation.TopRight.x, _cameraImageToDisplayUvTransformation.TopRight.y)); EdgeDetectionBackgroundImage.material.SetVector(BOTTOM_LEFT_RIGHT, new Vector4( _cameraImageToDisplayUvTransformation.BottomLeft.x, _cameraImageToDisplayUvTransformation.BottomLeft.y, _cameraImageToDisplayUvTransformation.BottomRight.x, _cameraImageToDisplayUvTransformation.BottomRight.y)); } }
/// <summary> /// Handles a new CPU image. /// </summary> /// <param name="format">The format of the image.</param> /// <param name="width">Width of the image, in pixels.</param> /// <param name="height">Height of the image, in pixels.</param> /// <param name="pixelBuffer">Pointer to raw image buffer.</param> /// <param name="bufferSize">The size of the image buffer, in bytes.</param> private void _OnImageAvailable(TextureReaderApi.ImageFormatType format, int width, int height, IntPtr pixelBuffer, int bufferSize) { if (!EdgeDetectionBackgroundImage.enabled) { return; } if (format != TextureReaderApi.ImageFormatType.ImageFormatGrayscale) { Debug.Log("No edge detected due to incorrect image format."); return; } if (m_EdgeDetectionBackgroundTexture == null || m_EdgeDetectionResultImage == null || m_EdgeDetectionBackgroundTexture.width != width || m_EdgeDetectionBackgroundTexture.height != height) { m_EdgeDetectionBackgroundTexture = new Texture2D(width, height, TextureFormat.R8, false, false); m_EdgeDetectionResultImage = new byte[width * height]; _UpdateCameraImageToDisplayUVs(); } if (m_CachedOrientation != Screen.orientation || m_CachedScreenDimensions.x != Screen.width || m_CachedScreenDimensions.y != Screen.height) { _UpdateCameraImageToDisplayUVs(); m_CachedOrientation = Screen.orientation; m_CachedScreenDimensions = new Vector2(Screen.width, Screen.height); } // Detect edges within the image. if (EdgeDetector.Detect(m_EdgeDetectionResultImage, pixelBuffer, width, height)) { // Update the rendering texture with the edge image. m_EdgeDetectionBackgroundTexture.LoadRawTextureData(m_EdgeDetectionResultImage); m_EdgeDetectionBackgroundTexture.Apply(); EdgeDetectionBackgroundImage.material.SetTexture("_ImageTex", m_EdgeDetectionBackgroundTexture); const string TOP_LEFT_RIGHT = "_UvTopLeftRight"; const string BOTTOM_LEFT_RIGHT = "_UvBottomLeftRight"; EdgeDetectionBackgroundImage.material.SetVector(TOP_LEFT_RIGHT, new Vector4( m_CameraImageToDisplayUvTransformation.TopLeft.x, m_CameraImageToDisplayUvTransformation.TopLeft.y, m_CameraImageToDisplayUvTransformation.TopRight.x, m_CameraImageToDisplayUvTransformation.TopRight.y)); EdgeDetectionBackgroundImage.material.SetVector(BOTTOM_LEFT_RIGHT, new Vector4( m_CameraImageToDisplayUvTransformation.BottomLeft.x, m_CameraImageToDisplayUvTransformation.BottomLeft.y, m_CameraImageToDisplayUvTransformation.BottomRight.x, m_CameraImageToDisplayUvTransformation.BottomRight.y)); } }