问题描述
到目前为止,我有一个应用程序允许用户在UIImageView元素上自由绘制(如草图板)。
I have an app so far that allows the user to free draw (like a sketch pad) on a UIImageView element.
我想获取原始RGB像素数据(作为0到255整数值)作为多维数组,因此我可以将其提供给机器学习算法。或者是否有其他方法可以将原始图像数据发送到单独的C ++函数?
I want to get the raw RGB pixel data (as 0 to 255 integer values) as a multidimensional array so I can feed it into a machine learning algorithm . Or is there some other way I can send the raw image data over to separate C++ function?
在Swift中有一种简单的方法吗?
Is there an easy way to do this in Swift?
推荐答案
将CGImage转换为强度值数组
我的函数需要 CGImage
并返回无符号8位整数数组以及图像的宽度和高度。我写了这段代码用于灰度图像。将其扩展为颜色应该是将Gray更改为RGB并将每像素的字节数更改为3,假设没有alpha通道。
Converting a CGImage to an array of intensity values
My function takes a CGImage
and returns an array of unsigned 8-bit integers as well as the width and height of the image. I wrote this code for use with grayscale images. Extending this to color should be a matter of changing Gray to RGB and changing the bytes per pixel to 3 assuming no alpha channel.
func pixelValues(fromCGImage imageRef: CGImage?) -> (pixelValues: [UInt8]?, width: Int, height: Int)
{
var width = 0
var height = 0
var pixelValues: [UInt8]?
if let imageRef = imageRef {
width = imageRef.width
height = imageRef.height
let bitsPerComponent = imageRef.bitsPerComponent
let bytesPerRow = imageRef.bytesPerRow
let totalBytes = height * bytesPerRow
let colorSpace = CGColorSpaceCreateDeviceGray()
var intensities = [UInt8](repeating: 0, count: totalBytes)
let contextRef = CGContext(data: &intensities, width: width, height: height, bitsPerComponent: bitsPerComponent, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: 0)
contextRef?.draw(imageRef, in: CGRect(x: 0.0, y: 0.0, width: CGFloat(width), height: CGFloat(height)))
pixelValues = intensities
}
return (pixelValues, width, height)
}
func image(fromPixelValues pixelValues: [UInt8]?, width: Int, height: Int) -> CGImage?
{
var imageRef: CGImage?
if var pixelValues = pixelValues {
let bitsPerComponent = 8
let bytesPerPixel = 1
let bitsPerPixel = bytesPerPixel * bitsPerComponent
let bytesPerRow = bytesPerPixel * width
let totalBytes = height * bytesPerRow
imageRef = withUnsafePointer(to: &pixelValues, {
ptr -> CGImage? in
var imageRef: CGImage?
let colorSpaceRef = CGColorSpaceCreateDeviceGray()
let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.none.rawValue).union(CGBitmapInfo())
let data = UnsafeRawPointer(ptr.pointee).assumingMemoryBound(to: UInt8.self)
let releaseData: CGDataProviderReleaseDataCallback = {
(info: UnsafeMutableRawPointer?, data: UnsafeRawPointer, size: Int) -> () in
}
if let providerRef = CGDataProvider(dataInfo: nil, data: data, size: totalBytes, releaseData: releaseData) {
imageRef = CGImage(width: width,
height: height,
bitsPerComponent: bitsPerComponent,
bitsPerPixel: bitsPerPixel,
bytesPerRow: bytesPerRow,
space: colorSpaceRef,
bitmapInfo: bitmapInfo,
provider: providerRef,
decode: nil,
shouldInterpolate: false,
intent: CGColorRenderingIntent.defaultIntent)
}
return imageRef
})
}
return imageRef
}
这篇关于在swift中从UIImage / CGImage获取像素数据作为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!