MLMultiArray

extension MLMultiArray
  • Converts the multi-array to a CGImage.

    The multi-array must have at least 2 dimensions for a grayscale image, or at least 3 dimensions for a color image.

    The default expected shape is (height, width) or (channels, height, width). However, you can change this using the axes parameter. For example, if the array shape is (1, height, width, channels), use axes: (3, 1, 2).

    If channel is not nil, only converts that channel to a grayscale image. This lets you visualize individual channels from a multi-array with more than 4 channels.

    Otherwise, converts all channels. In this case, the number of channels in the multi-array must be 1 for grayscale, 3 for RGB, or 4 for RGBA.

    Use the min and max parameters to put the values from the array into the range [0, 255], if not already:

    • min: should be the smallest value in the data; this will be mapped to 0.
    • max: should be the largest value in the data; will be mapped to 255.

    For example, if the range of the data in the multi-array is [-1, 1], use min: -1, max: 1. If the range is already [0, 255], then use the defaults.

    Declaration

    Swift

    public func cgImage(min: Double = 0,
                        max: Double = 255,
                        channel: Int? = nil,
                        axes: (Int, Int, Int)? = nil) -> CGImage?
  • Converts the multi-array into an array of RGBA or grayscale pixels.

    Note

    This is not particularly fast, but it is flexible. You can change the loops to convert the multi-array whichever way you please.

    Note

    The type of min and max must match the dataType of the MLMultiArray object.

    Declaration

    Swift

    public func toRawBytes<T: MultiArrayType>(min: T,
                                              max: T,
                                              channel: Int? = nil,
                                              axes: (Int, Int, Int)? = nil)
                    -> (bytes: [UInt8], width: Int, height: Int, channels: Int)?

    Return Value

    tuple containing the RGBA bytes, the dimensions of the image, and the number of channels in the image (1, 3, or 4).

  • Undocumented

    Declaration

    Swift

    public func image(min: Double = 0,
                      max: Double = 255,
                      channel: Int? = nil,
                      axes: (Int, Int, Int)? = nil) -> UIImage?