You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

202 lines
6.7 KiB

  1. //----------------------------------------------------------------------------
  2. // Copyright (C) 2004-2023 by EMGU Corporation. All rights reserved.
  3. //----------------------------------------------------------------------------
  4. //#if __IOS__
  5. using System;
  6. using System.Diagnostics;
  7. using System.Drawing;
  8. using Emgu.CV;
  9. using Emgu.CV.CvEnum;
  10. using Emgu.CV.Structure;
  11. using CoreGraphics;
  12. using Emgu.CV.Cuda;
  13. using UIKit;
  14. namespace Emgu.CV
  15. {
  16. /// <summary>
  17. /// Provide extension method to convert IInputArray to and from UIImage
  18. /// </summary>
  19. public static class UIImageExtension
  20. {
  21. /// <summary>
  22. /// Creating an Image from the UIImage
  23. /// </summary>
  24. public static Image<TColor, TDepth> ToImage<TColor, TDepth>(this UIImage uiImage)
  25. where TColor : struct, IColor
  26. where TDepth : new()
  27. //: this( (int) uiImage.Size.Width, (int) uiImage.Size.Height)
  28. {
  29. using (CGImage cgImage = uiImage.CGImage ?? throw new InvalidOperationException())
  30. {
  31. return cgImage.ToImage<TColor, TDepth>();
  32. }
  33. }
  34. /// <summary>
  35. /// Convert this Image object to UIImage
  36. /// </summary>
  37. public static UIImage ToUIImage<TColor, TDepth>(this Image<TColor, TDepth> image)
  38. where TColor : struct, IColor
  39. where TDepth : new()
  40. {
  41. using (CGImage cgImage = image.ToCGImage())
  42. {
  43. return UIImage.FromImage(cgImage);
  44. }
  45. }
  46. /// <summary>
  47. /// Initializes a new instance of the <see cref="Emgu.CV.UMat"/> class from UIImage
  48. /// </summary>
  49. /// <param name="mode">The color conversion mode. By default, it convert the UIImage to BGRA color type to preserve all the image channels.</param>
  50. /// <param name="uiImage">The UIImage.</param>
  51. public static UMat ToUMat(this UIImage uiImage, ImreadModes mode = ImreadModes.AnyColor)
  52. {
  53. //UMat umat = new UMat ();
  54. using (CGImage cgImage = uiImage.CGImage ?? throw new InvalidOperationException())
  55. {
  56. //ConvertCGImageToArray (cgImage, this, mode);
  57. return cgImage.ToUMat(mode);
  58. }
  59. }
  60. /// <summary>
  61. /// Converts to UIImage.
  62. /// </summary>
  63. /// <returns>The UIImage.</returns>
  64. public static UIImage ToUIImage(this UMat umat)
  65. {
  66. using (CGImage tmp = umat.ToCGImage())
  67. {
  68. return UIImage.FromImage(tmp);
  69. }
  70. }
  71. /// <summary>
  72. /// Initializes a new instance of the <see cref="Emgu.CV.Mat"/> class from UIImage
  73. /// </summary>
  74. /// <param name="mode">The color conversion mode. By default, it convert the UIImage to BGRA color type to preserve all the image channels.</param>
  75. /// <param name="uiImage">The UIImage.</param>
  76. public static Mat ToMat(this UIImage uiImage, ImreadModes mode = ImreadModes.AnyColor)
  77. {
  78. using (CGImage cgImage = uiImage.CGImage ?? throw new InvalidOperationException())
  79. {
  80. return cgImage.ToMat(mode);
  81. }
  82. }
  83. /// <summary>
  84. /// Initializes a new instance of the <see cref="Emgu.CV.Mat"/> class from UIImage
  85. /// </summary>
  86. /// <param name="mode">The color conversion mode. By default, it convert the UIImage to BGRA color type to preserve all the image channels.</param>
  87. /// <param name="uiImage">The UIImage.</param>
  88. /// <param name="outputArray">The output array</param>
  89. public static void ToArray (this UIImage uiImage, IOutputArray outputArray, ImreadModes mode = ImreadModes.AnyColor)
  90. {
  91. using (CGImage cgImage = uiImage.CGImage ?? throw new InvalidOperationException())
  92. {
  93. cgImage.ToArray (outputArray, mode);
  94. }
  95. }
  96. /// <summary>
  97. /// Converts to UIImage.
  98. /// </summary>
  99. /// <returns>The UIImage</returns>
  100. public static UIImage ToUIImage(this Mat mat)
  101. {
  102. using (CGImage tmp = mat.ToCGImage())
  103. {
  104. return UIImage.FromImage(tmp);
  105. }
  106. }
  107. /// <summary>
  108. /// Converts to UIImage.
  109. /// </summary>
  110. /// <returns>The UIImage</returns>
  111. public static UIImage ToUIImage(this IInputArray inputArray)
  112. {
  113. using(InputArray ia = inputArray.GetInputArray())
  114. using(Mat mat = ia.GetMat())
  115. {
  116. return mat.ToUIImage ();
  117. }
  118. }
  119. /// <summary>
  120. /// Converts to UIImage.
  121. /// </summary>
  122. /// <returns>The UIImage.</returns>
  123. public static UIImage ToUIImage(this GpuMat gpuMat)
  124. {
  125. using (Mat tmp = new Mat())
  126. {
  127. gpuMat.Download(tmp);
  128. return tmp.ToUIImage();
  129. }
  130. }
  131. }
  132. /// <summary>
  133. /// Class that can be used to read a file to Mat using UIImage
  134. /// </summary>
  135. public class UIImageFileReaderMat : Emgu.CV.IFileReaderMat
  136. {
  137. /// <summary>
  138. /// Read the file into Mat using UIImage
  139. /// </summary>
  140. /// <param name="fileName">The image file to be read</param>
  141. /// <param name="mat">The Mat to read the file into</param>
  142. /// <param name="loadType">Image load type</param>
  143. /// <returns>True if successfully read the file into the Mat</returns>
  144. public bool ReadFile(String fileName, Mat mat, CvEnum.ImreadModes loadType)
  145. {
  146. try
  147. {
  148. //try again to load with UIImage
  149. using (UIImage tmp = UIImage.FromFile(fileName) ?? throw new InvalidOperationException())
  150. {
  151. (tmp.CGImage ?? throw new InvalidOperationException()).ToArray(mat, loadType);
  152. }
  153. return true;
  154. }
  155. catch (Exception e)
  156. {
  157. Debug.WriteLine(e);
  158. return false;
  159. }
  160. }
  161. }
  162. /*
  163. public class UIImageFileReaderImage : Emgu.CV.IFileReaderImage
  164. {
  165. public bool ReadFile<TColor, TDepth>(String fileName, Image<TColor, TDepth> image)
  166. where TColor : struct, IColor
  167. where TDepth : new()
  168. {
  169. try
  170. {
  171. //try again to load with UIImage
  172. using (UIImage tmp = UIImage.FromFile(fileName))
  173. {
  174. tmp.CGImage.ToImage<TColor, TDepth>(image);
  175. }
  176. return true;
  177. }
  178. catch (Exception e)
  179. {
  180. Debug.WriteLine(e);
  181. return false;
  182. }
  183. }
  184. }*/
  185. }
  186. //#endif