mirror of https://github.com/emgucv/emgucv.git

15 changed files with 156 additions and 594 deletions
-
1.gitmodules
-
14Emgu.CV.Example/XamarinForms/iOS/Emgu.CV.XamarinForms.iOS.csproj
-
6Emgu.CV.NativeImage/CGImageExtension.cs
-
93Emgu.CV.NativeImage/UIImageExtension.cs
-
0Emgu.CV.World/iOS/ApiDefinition.cs
-
38Emgu.CV.World/iOS/Emgu.CV.World.IOS.csproj
-
16Emgu.CV.World/iOS/Properties/AssemblyInfo.cs
-
0Emgu.CV.World/iOS/libcvextern.linkwith.cs
-
4Emgu.CV/Core/Image.cs
-
4Emgu.CV/Core/Mat.cs
-
150Emgu.CV/Emgu.CV.projitems
-
109Emgu.CV/PInvoke/iOS/ImageiOS.cs
-
170Emgu.CV/PInvoke/iOS/MatiOS.cs
-
143Emgu.CV/PInvoke/iOS/UMatiOS.cs
-
2Solution/iOS/Emgu.CV.iOS.Example.sln
@ -0,0 +1,93 @@ |
|||
//----------------------------------------------------------------------------
|
|||
// Copyright (C) 2004-2019 by EMGU Corporation. All rights reserved.
|
|||
//----------------------------------------------------------------------------
|
|||
|
|||
#if __IOS__
|
|||
using System; |
|||
using System.Drawing; |
|||
using Emgu.CV; |
|||
using Emgu.CV.CvEnum; |
|||
using Emgu.CV.Structure; |
|||
using CoreGraphics; |
|||
using UIKit; |
|||
|
|||
namespace Emgu.CV |
|||
{ |
|||
public static class UIImageExtension |
|||
{ |
|||
/// <summary>
|
|||
/// Creating an Image from the UIImage
|
|||
/// </summary>
|
|||
public static Image<TColor, TDepth> ToImage<TColor, TDepth> (this UIImage uiImage) |
|||
where TColor : struct, IColor |
|||
where TDepth : new() |
|||
//: this( (int) uiImage.Size.Width, (int) uiImage.Size.Height)
|
|||
{ |
|||
using (CGImage cgImage = uiImage.CGImage) { |
|||
return cgImage.ToImage<TColor, TDepth> (); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Convert this Image object to UIImage
|
|||
/// </summary>
|
|||
public static UIImage ToUIImage<TColor, TDepth> (this Image<TColor, TDepth> image) |
|||
where TColor : struct, IColor |
|||
where TDepth : new() |
|||
{ |
|||
using (CGImage cgImage = image.ToCGImage ()) { |
|||
return UIImage.FromImage (cgImage); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="Emgu.CV.UMat"/> class from UIImage
|
|||
/// </summary>
|
|||
/// <param name="mode">The color conversion mode. By default, it convert the UIImage to BGRA color type to preserve all the image channels.</param>
|
|||
/// <param name="uiImage">The UIImage.</param>
|
|||
public static UMat ToUMat(this UIImage uiImage, ImreadModes mode = ImreadModes.AnyColor) |
|||
{ |
|||
//UMat umat = new UMat ();
|
|||
using (CGImage cgImage = uiImage.CGImage) { |
|||
//ConvertCGImageToArray (cgImage, this, mode);
|
|||
return cgImage.ToUMat (mode); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Converts to UIImage.
|
|||
/// </summary>
|
|||
/// <returns>The UIImage.</returns>
|
|||
public static UIImage ToUIImage (this UMat umat) |
|||
{ |
|||
using (CGImage tmp = umat.ToCGImage ()) { |
|||
return UIImage.FromImage (tmp); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="Emgu.CV.Mat"/> class from UIImage
|
|||
/// </summary>
|
|||
/// <param name="mode">The color conversion mode. By default, it convert the UIImage to BGRA color type to preserve all the image channels.</param>
|
|||
/// <param name="uiImage">The UIImage.</param>
|
|||
public static Mat ToMat(this UIImage uiImage, ImreadModes mode = ImreadModes.AnyColor) |
|||
{ |
|||
using (CGImage cgImage = uiImage.CGImage) { |
|||
return cgImage.ToMat (mode); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Converts to UIImage.
|
|||
/// </summary>
|
|||
/// <returns>The UIImage.</returns>
|
|||
public static UIImage ToUIImage (this Mat mat) |
|||
{ |
|||
using (CGImage tmp = mat.ToCGImage ()) { |
|||
return UIImage.FromImage (tmp); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
#endif
|
@ -0,0 +1,16 @@ |
|||
using System.Reflection; |
|||
using System.Runtime.CompilerServices; |
|||
|
|||
// Information about this assembly is defined by the following attributes.
|
|||
// Change them to the values specific to your project.
|
|||
|
|||
[assembly: AssemblyTitle("Emgu.CV.World")] |
|||
[assembly: AssemblyDescription("")] |
|||
|
|||
|
|||
|
|||
// The following attributes are used to specify the signing key for the assembly,
|
|||
// if desired. See the Mono documentation for more information about signing.
|
|||
|
|||
//[assembly: AssemblyDelaySign(false)]
|
|||
//[assembly: AssemblyKeyFile("")]
|
@ -1,109 +0,0 @@ |
|||
//----------------------------------------------------------------------------
|
|||
// Copyright (C) 2004-2019 by EMGU Corporation. All rights reserved.
|
|||
//----------------------------------------------------------------------------
|
|||
|
|||
#if __IOS__
|
|||
using System; |
|||
using System.Drawing; |
|||
using Emgu.CV; |
|||
using Emgu.CV.Structure; |
|||
using CoreGraphics; |
|||
using UIKit; |
|||
|
|||
namespace Emgu.CV |
|||
{ |
|||
public partial class Image<TColor, TDepth> |
|||
: CvArray<TDepth>, IEquatable<Image<TColor, TDepth>> |
|||
where TColor : struct, IColor |
|||
where TDepth : new() |
|||
{ |
|||
/// <summary>
|
|||
/// Creating an Image from the CGImage
|
|||
/// </summary>
|
|||
public Image(CGImage cgImage) |
|||
: this( (int) cgImage.Width, (int) cgImage.Height) |
|||
{ |
|||
ConvertFromCGImage(cgImage); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Copy the data from the CGImage to the current Image object
|
|||
/// </summary>
|
|||
private void ConvertFromCGImage(CGImage cgImage) |
|||
{ |
|||
//Don't do this, Xamarin.iOS won't be able to resolve: if (this is Image<Rgba, Byte>)
|
|||
if (typeof(TColor) == typeof(Rgba) && typeof(TDepth) == typeof(byte)) |
|||
{ |
|||
RectangleF rect = new RectangleF(PointF.Empty, new SizeF(cgImage.Width, cgImage.Height)); |
|||
using (CGColorSpace cspace = CGColorSpace.CreateDeviceRGB()) |
|||
using (CGBitmapContext context = new CGBitmapContext( |
|||
MIplImage.ImageData, |
|||
Width, Height, |
|||
8, |
|||
Width * 4, |
|||
cspace, |
|||
CGImageAlphaInfo.PremultipliedLast)) |
|||
context.DrawImage(rect, cgImage); |
|||
} else |
|||
{ |
|||
using (Image<Rgba, Byte> tmp = new Image<Rgba, Byte>(cgImage)) |
|||
ConvertFrom(tmp); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Convert this Image object to CGImage
|
|||
/// </summary>
|
|||
public CGImage ToCGImage() |
|||
{ |
|||
//Don't do this, Xamarin.iOS won't be able to resolve: if (this is Image<Rgba, Byte>)
|
|||
if (typeof(TColor) == typeof(Rgba) && typeof(TDepth) == typeof(Byte)) |
|||
{ |
|||
using (CGColorSpace cspace = CGColorSpace.CreateDeviceRGB()) |
|||
using (CGBitmapContext context = new CGBitmapContext( |
|||
MIplImage.ImageData, |
|||
Width, Height, |
|||
8, |
|||
Width * 4, |
|||
cspace, |
|||
CGImageAlphaInfo.PremultipliedLast)) |
|||
|
|||
{ |
|||
CGImage cgImage = context.ToImage(); |
|||
return cgImage; |
|||
} |
|||
} else |
|||
{ |
|||
using (Image<Rgba, Byte> tmp = Convert<Rgba, Byte>()) |
|||
{ |
|||
return tmp.ToCGImage(); |
|||
} |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Creating an Image from the UIImage
|
|||
/// </summary>
|
|||
public Image(UIImage uiImage) |
|||
: this( (int) uiImage.Size.Width, (int) uiImage.Size.Height) |
|||
{ |
|||
using (CGImage cgImage = uiImage.CGImage) |
|||
{ |
|||
ConvertFromCGImage(cgImage); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Convert this Image object to UIImage
|
|||
/// </summary>
|
|||
public UIImage ToUIImage() |
|||
{ |
|||
using (CGImage cgImage = ToCGImage()) |
|||
{ |
|||
return UIImage.FromImage(cgImage); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
#endif
|
@ -1,170 +0,0 @@ |
|||
//----------------------------------------------------------------------------
|
|||
// Copyright (C) 2004-2019 by EMGU Corporation. All rights reserved.
|
|||
//----------------------------------------------------------------------------
|
|||
|
|||
#if __IOS__
|
|||
using System; |
|||
using System.Drawing; |
|||
using Emgu.CV; |
|||
using Emgu.CV.Structure; |
|||
using CoreGraphics; |
|||
using Emgu.CV.CvEnum; |
|||
using UIKit; |
|||
|
|||
|
|||
namespace Emgu.CV |
|||
{ |
|||
public static partial class CvInvoke |
|||
{ |
|||
internal static void ConvertCGImageToArray(CGImage cgImage, IOutputArray mat, ImreadModes modes = ImreadModes.AnyColor) |
|||
{ |
|||
Size sz = new Size((int)cgImage.Width, (int)cgImage.Height); |
|||
using (Mat m = new Mat(sz, DepthType.Cv8U, 4)) |
|||
{ |
|||
RectangleF rect = new RectangleF(PointF.Empty, new SizeF(cgImage.Width, cgImage.Height)); |
|||
using (CGColorSpace cspace = CGColorSpace.CreateDeviceRGB()) |
|||
using (CGBitmapContext context = new CGBitmapContext( |
|||
m.DataPointer, |
|||
sz.Width, sz.Height, |
|||
8, |
|||
sz.Width * 4, |
|||
cspace, |
|||
CGImageAlphaInfo.PremultipliedLast)) |
|||
context.DrawImage(rect, cgImage); |
|||
if (modes == ImreadModes.Grayscale) |
|||
{ |
|||
CvInvoke.CvtColor(m, mat, ColorConversion.Rgba2Gray); |
|||
} |
|||
else if (modes == ImreadModes.AnyColor) |
|||
{ |
|||
CvInvoke.CvtColor(m, mat, ColorConversion.Rgba2Bgra); |
|||
} |
|||
else if (modes == ImreadModes.ReducedColor2) |
|||
{ |
|||
using (Mat tmp = new Mat()) |
|||
{ |
|||
CvInvoke.PyrDown(m, tmp); |
|||
CvInvoke.CvtColor(tmp, mat, ColorConversion.Rgba2Bgr); |
|||
} |
|||
} |
|||
else if (modes == ImreadModes.ReducedGrayscale2) |
|||
{ |
|||
using (Mat tmp = new Mat()) |
|||
{ |
|||
CvInvoke.PyrDown(m, tmp); |
|||
CvInvoke.CvtColor(tmp, mat, ColorConversion.Rgba2Gray); |
|||
} |
|||
} |
|||
else if (modes == ImreadModes.ReducedColor4 || modes == ImreadModes.ReducedColor8 || modes == ImreadModes.ReducedGrayscale4 || modes == ImreadModes.ReducedGrayscale8 || modes == ImreadModes.LoadGdal) |
|||
{ |
|||
throw new NotImplementedException(String.Format("Conversion from PNG using mode {0} is not supported", modes)); |
|||
} |
|||
else |
|||
{ |
|||
CvInvoke.CvtColor(m, mat, ColorConversion.Rgba2Bgr); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
public partial class Mat |
|||
{ |
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="Emgu.CV.Mat"/> class from CGImage
|
|||
/// </summary>
|
|||
/// <param name="mode">The color conversion mode. By default, it convert the UIImage to BGRA color type to preserve all the image channels.</param>
|
|||
/// <param name="cgImage">The CGImage.</param>
|
|||
public Mat(CGImage cgImage, ImreadModes mode = ImreadModes.AnyColor) |
|||
: this() |
|||
{ |
|||
CvInvoke.ConvertCGImageToArray(cgImage, this, mode); |
|||
} |
|||
|
|||
private static CGImage RgbaByteMatToCGImage(Mat bgraByte) |
|||
{ |
|||
using (CGColorSpace cspace = CGColorSpace.CreateDeviceRGB()) |
|||
using (CGBitmapContext context = new CGBitmapContext( |
|||
bgraByte.DataPointer, |
|||
bgraByte.Width, bgraByte.Height, |
|||
8, |
|||
bgraByte.Width * 4, |
|||
cspace, |
|||
CGImageAlphaInfo.PremultipliedLast)) |
|||
return context.ToImage(); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Converts to CGImage
|
|||
/// </summary>
|
|||
/// <returns>The CGImage.</returns>
|
|||
public CGImage ToCGImage() |
|||
{ |
|||
int nchannels = NumberOfChannels; |
|||
DepthType d = Depth; |
|||
if (nchannels == 4 && d == DepthType.Cv8U) |
|||
{ |
|||
//bgra
|
|||
using (Mat tmp = new Mat()) |
|||
{ |
|||
CvInvoke.CvtColor(this, tmp, ColorConversion.Bgra2Rgba); |
|||
return RgbaByteMatToCGImage(tmp); |
|||
} |
|||
} |
|||
else if (nchannels == 3 && d == DepthType.Cv8U) |
|||
{ |
|||
//bgr
|
|||
using (Mat tmp = new Mat()) |
|||
{ |
|||
CvInvoke.CvtColor(this, tmp, ColorConversion.Bgr2Rgba); |
|||
return RgbaByteMatToCGImage(tmp); |
|||
} |
|||
} |
|||
else if (nchannels == 1 && d == DepthType.Cv8U) |
|||
{ |
|||
using (Mat tmp = new Mat()) |
|||
{ |
|||
CvInvoke.CvtColor(this, tmp, ColorConversion.Gray2Rgba); |
|||
return RgbaByteMatToCGImage(tmp); |
|||
} |
|||
} |
|||
else |
|||
{ |
|||
throw new Exception(String.Format("Converting from Mat of {0} channels {1} to CGImage is not supported. Please convert Mat to 3 channel Bgr image of Byte before calling this function.", nchannels, d)); |
|||
} |
|||
} |
|||
|
|||
#if __IOS__
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="Emgu.CV.Mat"/> class from UIImage
|
|||
/// </summary>
|
|||
/// <param name="mode">The color conversion mode. By default, it convert the UIImage to BGRA color type to preserve all the image channels.</param>
|
|||
/// <param name="uiImage">The UIImage.</param>
|
|||
public Mat(UIImage uiImage, ImreadModes mode = ImreadModes.AnyColor) |
|||
: this () |
|||
{ |
|||
using(CGImage cgImage = uiImage.CGImage) |
|||
{ |
|||
CvInvoke.ConvertCGImageToArray(cgImage, this, mode); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Converts to UIImage.
|
|||
/// </summary>
|
|||
/// <returns>The UIImage.</returns>
|
|||
public UIImage ToUIImage() |
|||
{ |
|||
using (CGImage tmp = ToCGImage()) |
|||
{ |
|||
return UIImage.FromImage(tmp); |
|||
} |
|||
} |
|||
#else
|
|||
|
|||
#endif
|
|||
|
|||
} |
|||
|
|||
} |
|||
|
|||
#endif
|
@ -1,143 +0,0 @@ |
|||
//----------------------------------------------------------------------------
|
|||
// Copyright (C) 2004-2019 by EMGU Corporation. All rights reserved.
|
|||
//----------------------------------------------------------------------------
|
|||
|
|||
#if __UNIFIED__
|
|||
using System; |
|||
using System.Drawing; |
|||
using Emgu.CV; |
|||
using Emgu.CV.Structure; |
|||
using CoreGraphics; |
|||
using Emgu.CV.CvEnum; |
|||
#if __IOS__
|
|||
using UIKit; |
|||
#else
|
|||
using AppKit; |
|||
#endif
|
|||
|
|||
namespace Emgu.CV |
|||
{ |
|||
public partial class UMat |
|||
{ |
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="Emgu.CV.Mat"/> class from CGImage
|
|||
/// </summary>
|
|||
/// <param name="mode">The color conversion mode. By default, it convert the UIImage to BGRA color type to preserve all the image channels.</param>
|
|||
/// <param name="cgImage">The CGImage.</param>
|
|||
public UMat(CGImage cgImage, ImreadModes mode = ImreadModes.AnyColor) |
|||
: this() |
|||
{ |
|||
CvInvoke.ConvertCGImageToArray(cgImage, this, mode); |
|||
} |
|||
|
|||
private static CGImage RgbaByteMatToCGImage(Mat bgraByte) |
|||
{ |
|||
using (CGColorSpace cspace = CGColorSpace.CreateDeviceRGB()) |
|||
using (CGBitmapContext context = new CGBitmapContext( |
|||
bgraByte.DataPointer, |
|||
bgraByte.Width, bgraByte.Height, |
|||
8, |
|||
bgraByte.Width * 4, |
|||
cspace, |
|||
CGImageAlphaInfo.PremultipliedLast)) |
|||
return context.ToImage(); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Converts to CGImage
|
|||
/// </summary>
|
|||
/// <returns>The CGImage.</returns>
|
|||
public CGImage ToCGImage() |
|||
{ |
|||
int nchannels = NumberOfChannels; |
|||
DepthType d = Depth; |
|||
if (nchannels == 4 && d == DepthType.Cv8U) |
|||
{ |
|||
//bgra
|
|||
using (Mat tmp = new Mat()) |
|||
{ |
|||
CvInvoke.CvtColor(this, tmp, ColorConversion.Bgra2Rgba); |
|||
return RgbaByteMatToCGImage(tmp); |
|||
} |
|||
} |
|||
else if (nchannels == 3 && d == DepthType.Cv8U) |
|||
{ |
|||
//bgr
|
|||
using (Mat tmp = new Mat()) |
|||
{ |
|||
CvInvoke.CvtColor(this, tmp, ColorConversion.Bgr2Rgba); |
|||
return RgbaByteMatToCGImage(tmp); |
|||
} |
|||
} |
|||
else if (nchannels == 1 && d == DepthType.Cv8U) |
|||
{ |
|||
using (Mat tmp = new Mat()) |
|||
{ |
|||
CvInvoke.CvtColor(this, tmp, ColorConversion.Gray2Rgba); |
|||
return RgbaByteMatToCGImage(tmp); |
|||
} |
|||
} |
|||
else |
|||
{ |
|||
throw new Exception(String.Format("Converting from Mat of {0} channels {1} to CGImage is not supported. Please convert Mat to 3 channel Bgr image of Byte before calling this function.", nchannels, d)); |
|||
} |
|||
} |
|||
|
|||
#if __IOS__
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="Emgu.CV.UMat"/> class from UIImage
|
|||
/// </summary>
|
|||
/// <param name="mode">The color conversion mode. By default, it convert the UIImage to BGRA color type to preserve all the image channels.</param>
|
|||
/// <param name="uiImage">The UIImage.</param>
|
|||
public UMat(UIImage uiImage, ImreadModes mode = ImreadModes.AnyColor) |
|||
: this () |
|||
{ |
|||
using(CGImage cgImage = uiImage.CGImage) |
|||
{ |
|||
CvInvoke.ConvertCGImageToArray(cgImage, this, mode); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Converts to UIImage.
|
|||
/// </summary>
|
|||
/// <returns>The UIImage.</returns>
|
|||
public UIImage ToUIImage() |
|||
{ |
|||
using (CGImage tmp = ToCGImage()) |
|||
{ |
|||
return UIImage.FromImage(tmp); |
|||
} |
|||
} |
|||
#else
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="Emgu.CV.Mat"/> class from NSImage
|
|||
/// </summary>
|
|||
/// <param name="mode">The color conversion mode. By default, it convert the UIImage to BGRA color type to preserve all the image channels.</param>
|
|||
/// <param name="nsImage">The NSImage.</param>
|
|||
public UMat(NSImage nsImage, ImreadModes mode = ImreadModes.AnyColor) |
|||
: this() |
|||
{ |
|||
using (CGImage cgImage = nsImage.CGImage) |
|||
{ |
|||
CvInvoke.ConvertCGImageToArray(cgImage, this, mode); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Converts to NSImage.
|
|||
/// </summary>
|
|||
/// <returns>The NSImage.</returns>
|
|||
public NSImage ToNSImage() |
|||
{ |
|||
using (CGImage tmp = ToCGImage()) |
|||
{ |
|||
return new NSImage(tmp, new CGSize(tmp.Width, tmp.Height)); |
|||
} |
|||
} |
|||
#endif
|
|||
|
|||
} |
|||
} |
|||
|
|||
#endif
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue