Browse Source

Added NSImage.ToArray extension method.

pull/703/head
Canming Huang 4 years ago
parent
commit
be6ba0e8d3
  1. 12
      Emgu.CV.NativeImage/CGImageExtension.cs
  2. 52
      Emgu.CV.NativeImage/NSImageExtension.cs

12
Emgu.CV.NativeImage/CGImageExtension.cs

@ -94,6 +94,13 @@ namespace Emgu.CV
}
}
/// <summary>
/// Convert a CGImage to a IOutputArray
/// </summary>
/// <param name="cgImage">The source CGImage</param>
/// <param name="mat">The destination array</param>
/// <param name="modes">The color format for the destination array</param>
/// <exception cref="NotImplementedException">Exception will be thrown if the ImreadModes is not supported.</exception>
public static void ToArray(this CGImage cgImage, IOutputArray mat, ImreadModes modes = ImreadModes.AnyColor)
{
Size sz = new Size((int)cgImage.Width, (int)cgImage.Height);
@ -147,12 +154,11 @@ namespace Emgu.CV
|| modes == ImreadModes.ReducedGrayscale8
|| modes == ImreadModes.LoadGdal)
{
throw new NotImplementedException(String.Format("Conversion from PNG using mode {0} is not supported", modes));
throw new NotImplementedException(String.Format("Conversion from CGImage using mode {0} is not supported", modes));
}
else
{
throw new Exception(String.Format("ImreadModes of {0} is not implemented.", modes.ToString()));
//CvInvoke.CvtColor(m, mat, ColorConversion.Rgba2Bgr);
throw new NotImplementedException(String.Format("ImreadModes of {0} is not implemented.", modes.ToString()));
}
}
}

52
Emgu.CV.NativeImage/NSImageExtension.cs

@ -13,30 +13,35 @@ using AppKit;
namespace Emgu.CV
{
/// <summary>
/// Provide extension method to convert IInputArray to and from NSImage
/// </summary>
public static class NSImageExtension
{
/// <summary>
/// Creating an Image from the NSImage
/// </summary>
public static Image<TColor, TDepth> ToImage<TColor, TDepth> (this NSImage nsImage)
public static Image<TColor, TDepth> ToImage<TColor, TDepth>(this NSImage nsImage)
where TColor : struct, IColor
where TDepth : new()
//: this( (int) uiImage.Size.Width, (int) uiImage.Size.Height)
{
using (CGImage cgImage = nsImage.CGImage) {
return cgImage.ToImage<TColor, TDepth> ();
using (CGImage cgImage = nsImage.CGImage)
{
return cgImage.ToImage<TColor, TDepth>();
}
}
/// <summary>
/// Convert this Image object to NSImage
/// </summary>
public static NSImage ToNSImage<TColor, TDepth> (this Image<TColor, TDepth> image)
public static NSImage ToNSImage<TColor, TDepth>(this Image<TColor, TDepth> image)
where TColor : struct, IColor
where TDepth : new()
{
using (CGImage cgImage = image.ToCGImage ()) {
return new NSImage (cgImage, new CGSize(cgImage.Width, cgImage.Height));
using (CGImage cgImage = image.ToCGImage())
{
return new NSImage(cgImage, new CGSize(cgImage.Width, cgImage.Height));
}
}
@ -48,9 +53,10 @@ namespace Emgu.CV
public static UMat ToUMat(this NSImage nsImage, ImreadModes mode = ImreadModes.AnyColor)
{
//UMat umat = new UMat ();
using (CGImage cgImage = nsImage.CGImage) {
using (CGImage cgImage = nsImage.CGImage)
{
//ConvertCGImageToArray (cgImage, this, mode);
return cgImage.ToUMat (mode);
return cgImage.ToUMat(mode);
}
}
@ -58,13 +64,29 @@ namespace Emgu.CV
/// Converts to NSImage.
/// </summary>
/// <returns>The NSImage.</returns>
public static NSImage ToNSImage (this UMat umat)
public static NSImage ToNSImage(this UMat umat)
{
using (CGImage cgImage = umat.ToCGImage())
{
using (CGImage cgImage = umat.ToCGImage ()) {
return new NSImage(cgImage, new CGSize(cgImage.Width, cgImage.Height));
}
}
/// <summary>
/// Convert a NSImage to a IOutputArray
/// </summary>
/// <param name="nsImage">The source NSImage</param>
/// <param name="mat">The destination array</param>
/// <param name="modes">The color format for the destination array</param>
/// <exception cref="NotImplementedException">Exception will be thrown if the ImreadModes is not supported.</exception>
public static void ToArray(this NSImage nsImage, IOutputArray mat, ImreadModes modes = ImreadModes.AnyColor)
{
using (CGImage cgImage = nsImage.CGImage)
{
cgImage.ToArray(mat, modes);
}
}
/// <summary>
/// Initializes a new instance of the <see cref="Emgu.CV.Mat"/> class from UIImage
/// </summary>
@ -72,8 +94,9 @@ namespace Emgu.CV
/// <param name="nsImage">The NSImage.</param>
public static Mat ToMat(this NSImage nsImage, ImreadModes mode = ImreadModes.AnyColor)
{
using (CGImage cgImage = nsImage.CGImage) {
return cgImage.ToMat (mode);
using (CGImage cgImage = nsImage.CGImage)
{
return cgImage.ToMat(mode);
}
}
@ -81,9 +104,10 @@ namespace Emgu.CV
/// Converts to NSImage.
/// </summary>
/// <returns>The NSImage.</returns>
public static NSImage ToNSImage (this Mat mat)
public static NSImage ToNSImage(this Mat mat)
{
using (CGImage cgImage = mat.ToCGImage())
{
using (CGImage cgImage = mat.ToCGImage ()) {
return new NSImage(cgImage, new CGSize(cgImage.Width, cgImage.Height));
}
}

Loading…
Cancel
Save