Browse Source

Added NSImage.ToArray extension method.

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

22
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);
@ -141,18 +148,17 @@ namespace Emgu.CV
CvInvoke.CvtColor(tmp, mat, ColorConversion.Rgba2Gray);
}
}
else if (modes == ImreadModes.ReducedColor4
|| modes == ImreadModes.ReducedColor8
|| modes == ImreadModes.ReducedGrayscale4
|| modes == ImreadModes.ReducedGrayscale8
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));
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()));
}
}
}
@ -174,7 +180,7 @@ namespace Emgu.CV
using (CGColorSpace cspace = CGColorSpace.CreateDeviceRGB())
using (CGBitmapContext context = new CGBitmapContext(
bgraByte.DataPointer,
bgraByte.Width,
bgraByte.Width,
bgraByte.Height,
8,
bgraByte.Width * 4,

158
Emgu.CV.NativeImage/NSImageExtension.cs

@ -13,80 +13,104 @@ using AppKit;
namespace Emgu.CV
{
public static class NSImageExtension
{
/// <summary>
/// Creating an Image from the NSImage
/// </summary>
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> ();
}
}
/// <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)
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>();
}
}
/// <summary>
/// Convert this Image object to NSImage
/// </summary>
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));
}
}
/// <summary>
/// Convert this Image object to NSImage
/// </summary>
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));
}
}
/// <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="nsImage">The NSImage.</param>
public static UMat ToUMat(this NSImage nsImage, ImreadModes mode = ImreadModes.AnyColor)
{
//UMat umat = new UMat ();
using (CGImage cgImage = nsImage.CGImage) {
//ConvertCGImageToArray (cgImage, this, mode);
return cgImage.ToUMat (mode);
}
}
/// <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="nsImage">The NSImage.</param>
public static UMat ToUMat(this NSImage nsImage, ImreadModes mode = ImreadModes.AnyColor)
{
//UMat umat = new UMat ();
using (CGImage cgImage = nsImage.CGImage)
{
//ConvertCGImageToArray (cgImage, this, mode);
return cgImage.ToUMat(mode);
}
}
/// <summary>
/// Converts to NSImage.
/// </summary>
/// <returns>The NSImage.</returns>
public static NSImage ToNSImage (this UMat umat)
{
using (CGImage cgImage = umat.ToCGImage ()) {
return new NSImage(cgImage, new CGSize(cgImage.Width, cgImage.Height));
/// <summary>
/// Converts to NSImage.
/// </summary>
/// <returns>The NSImage.</returns>
public static NSImage ToNSImage(this UMat umat)
{
using (CGImage cgImage = umat.ToCGImage())
{
return new NSImage(cgImage, new CGSize(cgImage.Width, cgImage.Height));
}
}
}
/// <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="nsImage">The NSImage.</param>
public static Mat ToMat(this NSImage nsImage, ImreadModes mode = ImreadModes.AnyColor)
{
using (CGImage cgImage = nsImage.CGImage) {
return cgImage.ToMat (mode);
}
}
/// <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>
/// <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 static Mat ToMat(this NSImage nsImage, ImreadModes mode = ImreadModes.AnyColor)
{
using (CGImage cgImage = nsImage.CGImage)
{
return cgImage.ToMat(mode);
}
}
/// <summary>
/// Converts to NSImage.
/// </summary>
/// <returns>The NSImage.</returns>
public static NSImage ToNSImage (this Mat mat)
{
using (CGImage cgImage = mat.ToCGImage ()) {
return new NSImage(cgImage, new CGSize(cgImage.Width, cgImage.Height));
/// <summary>
/// Converts to NSImage.
/// </summary>
/// <returns>The NSImage.</returns>
public static NSImage ToNSImage(this Mat mat)
{
using (CGImage cgImage = mat.ToCGImage())
{
return new NSImage(cgImage, new CGSize(cgImage.Width, cgImage.Height));
}
}
}
/// <summary>
/// Converts to NSImage.

Loading…
Cancel
Save