【发布时间】:2016-07-23 10:48:51
【问题描述】:
是否可以使用 UIImage 函数将双精度数组转换为 UIImage?
var ImageDouble = [1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0]
UIImage 函数请求字符串。我尝试将单个元素上的数组转换为字符串,并将整个内容转换为字符串,但结果为零。有人可以帮忙吗?
[编辑:]
看来我并不清楚我的问题。为了更清楚,我正在开发一个软件,它可以从数学表达式中裁剪数字和运算符,并一个一个地向我展示所有块。现在,我该如何在 Swift 中处理它? :
第 1 步:我提供了一张灰度图像。我使用 UIImage 函数阅读它。我将其转换为自定义 RGBA 函数,如下所示,这样我就可以像在 MATLAB、Python 或 C++ 中那样处理像素级别。该功能的灵感来自许多网站的使用建议。
struct Pixel {
var value: UInt32
var red: UInt8 {
get { return UInt8(value & 0xFF) }
set { value = UInt32(newValue) | (value & 0xFFFFFF00) }
}
var green: UInt8 {
get { return UInt8((value >> 8) & 0xFF) }
set { value = (UInt32(newValue) << 8) | (value & 0xFFFF00FF) }
}
var blue: UInt8 {
get { return UInt8((value >> 16) & 0xFF) }
set { value = (UInt32(newValue) << 16) | (value & 0xFF00FFFF) }
}
var alpha: UInt8 {
get { return UInt8((value >> 24) & 0xFF) }
set { value = (UInt32(newValue) << 24) | (value & 0x00FFFFFF) }
}
}
struct RGBA {
var pixels: UnsafeMutableBufferPointer<Pixel>
var width: Int
var height: Int
init?(image: UIImage) {
guard let cgImage = image.CGImage else { return nil }
width = Int(image.size.width)
height = Int(image.size.height)
let bitsPerComponent = 8
let bytesPerPixel = 4
let bytesPerRow = width * bytesPerPixel
let imageData = UnsafeMutablePointer<Pixel>.alloc(width * height)
let colorSpace = CGColorSpaceCreateDeviceRGB()
var bitmapInfo: UInt32 = CGBitmapInfo.ByteOrder32Big.rawValue
bitmapInfo |= CGImageAlphaInfo.PremultipliedLast.rawValue & CGBitmapInfo.AlphaInfoMask.rawValue
guard let imageContext = CGBitmapContextCreate(imageData, width, height, bitsPerComponent, bytesPerRow, colorSpace, bitmapInfo) else { return nil }
CGContextDrawImage(imageContext, CGRect(origin: CGPointZero, size: image.size), cgImage)
pixels = UnsafeMutableBufferPointer<Pixel>(start: imageData, count: width * height)
}
func toUIImage() -> UIImage? {
let bitsPerComponent = 8
let bytesPerPixel = 4
let bytesPerRow = width * bytesPerPixel
let colorSpace = CGColorSpaceCreateDeviceRGB()
var bitmapInfo: UInt32 = CGBitmapInfo.ByteOrder32Big.rawValue
bitmapInfo |= CGImageAlphaInfo.PremultipliedLast.rawValue & CGBitmapInfo.AlphaInfoMask.rawValue
let imageContext = CGBitmapContextCreateWithData(pixels.baseAddress, width, height, bitsPerComponent, bytesPerRow, colorSpace, bitmapInfo, nil, nil)
guard let cgImage = CGBitmapContextCreateImage(imageContext) else {return nil}
let image = UIImage(CGImage: cgImage)
return image
}
}
第 2 步:现在我采用图像的一个通道 [红色:我采用哪个通道都没有关系,因为所有通道都有相同的输出] 并应用我自定义的内核大小为 5x5 的中值滤波器。
第 3 步:接下来,我使用 Nibblack 近似对图像进行二值化,其中我使用了平均滤波器和标准差。
Step-4:之后,我使用连通分量标记来分离出图像的不同连通分量。
第 5 步:最后,我需要裁剪标记的图像并调整其大小。对于从原始图像进行裁剪,我使用智能定位算法知道位置。对于调整大小,我想使用 Core Graphics Resizing filter。但是,为此我需要将当前输出 [二维数组或扁平化] 转换为 UIImage 或 CGImage。
这是我真正的问题:如何从 Array<Double> 或 Array<Int> 类型的二维数组或扁平数组转换为 UIImage 或 CGImage?
【问题讨论】:
-
你为什么使用双倍的图像?
-
因为有时,我们在应用平均过滤器后对图像进行的计算会得到双倍的结果。我也可以处理 Int。重点是将其转换为 UIImage。
-
您能描述一下您真正想要实现的目标吗?我在这里闻到了XY 的问题。
-
有可能!为此我深表歉意!但是,我想要实现的目标是:我希望能够转换为 Swift 现有的图像类型之一(UIImage 或 CGImage),以便我可以使用 CG 过滤器。但是我有一个 Double 数据类型的数组,其中的数组肯定是从原始图像中展平的,这是一个 2D 数组。
-
你能发布一个数组的真实例子吗?我们在这里谈论的数组有多大?也许一些关于如何获取数组的代码。
标签: ios arrays string swift image-processing