Reputation: 11577
PS: The result i wanted is in [Bool] format. ie, 0 for backgrounds and 1 for any color other than background.
I have an UIImage with 32 bits per pixel. How to make binary image (ie, 1bit .bmp image)
I'm using the code below: But its giving all zeros
public extension UIImage {
func pixelData() -> [UInt8]? {
let size = self.size
let dataSize = size.width * size.height
var pixelData = [UInt8](repeating: 0, count: Int(dataSize))
let colorSpace = CGColorSpaceCreateDeviceRGB()
let context = CGContext(data: &pixelData,
width: Int(size.width),
height: Int(size.height),
bitsPerComponent: 1,
bytesPerRow:Int(size.width),
space: colorSpace,
bitmapInfo: CGImageAlphaInfo.noneSkipLast.rawValue)
guard let cgImage = self.cgImage else { return nil }
context?.draw(cgImage, in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
return pixelData
}
}
Upvotes: 0
Views: 1531
Reputation: 3283
I have created, maybe a little far fetched, a solution.
First, I have created a pixel struct, describing what a pixel represents.
struct Pixel {
var r: Float
var g: Float
var b: Float
var a: Float
var row: Int
var col: Int
init(r: UInt8, g: UInt8, b: UInt8, a: UInt8, row: Int, col: Int) {
self.r = Float(r)
self.g = Float(g)
self.b = Float(b)
self.a = Float(a)
self.row = row
self.col = col
}
var color: UIColor {
return UIColor(
red: CGFloat(r/255.0),
green: CGFloat(g/255.0),
blue: CGFloat(b/255.0),
alpha: CGFloat(a/255.0)
)
}
var description: String {
return "\(r), \(g), \(b), \(a)"
}
}
I then created a UIImage extension.
extension UIImage {
var pixelData: [Pixel] {
let bmp = self.cgImage!.dataProvider!.data
var data: UnsafePointer<UInt8> = CFDataGetBytePtr(bmp)
var r, g, b, a: UInt8
var pixels = [Pixel]()
for row in 0 ..< Int(self.size.width) {
for col in 0 ..< Int(self.size.height) {
r = data.pointee
data = data.advanced(by: 1)
g = data.pointee
data = data.advanced(by: 1)
b = data.pointee
data = data.advanced(by: 1)
a = data.pointee
data = data.advanced(by: 1)
pixels.append(Pixel(r: r, g: g, b: b, a: a, row: row, col: col))
}
}
return pixels
}
var monochrome: UIImage? {
let context = CIContext(options: nil)
// let currentFilter = CIFilter(name: "CIPhotoEffectNoir")
let currentFilter = CIFilter(name: "CIColorMonochrome")
currentFilter?.setValue(CIImage(image: self), forKey: kCIInputImageKey)
guard let output = currentFilter?.outputImage,
let cgImage = context.createCGImage(output, from: output.extent) else {
print("Failed to create output image")
return nil
}
return UIImage(cgImage: cgImage, scale: scale, orientation: imageOrientation)
}
}
I did create the monochrome filter, you can see the commented code, you could use that to create a grayscale filter instead.
After that, all I had to do is test it and parse it:
let image = UIImage(named: "Test2")!
let grayScale: [Bool] = image.pixelData.map {
var white: CGFloat = 0
var alpha: CGFloat = 0
$0.color.getWhite(&white, alpha: &alpha)
return white > 0.5
}
var trues = 0
var falses = 0
grayScale.forEach {
if $0 {
trues += 1
} else {
falses += 1
}
}
The last step could be transformed into an extension too. Hope this helps!
Upvotes: 2