2017-02-04 109 views
0

如何將RGB圖像轉換爲其灰度色彩空間?我可以找到很多的代碼,適用於iOS,但不適用於MacOS。而蘋果的單證都在目標C ....Swift 3色彩空間macOS不是IOS

let width = image.size.width 
    let height = image.size.height 
    let imageRect = NSMakeRect(0, 0, width, height); 
    let colorSpace = CGColorSpaceCreateDeviceGray(); 


    let bits = image.representations.first as! NSBitmapImageRep; 
    bitmap!.representationUsingType(NSBitmapImageFileType.NSPNGFileType, properties: nil) 


    let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.none.rawValue); 
    let context = CGContext(data: nil, width: Int(width), height: Int(height), bitsPerComponent: 8, bytesPerRow: 0, space: colorSpace, bitmapInfo: bitmapInfo.rawValue); 
    context.draw(image.cgImage!, in : imageRect);// and this line is wrong obviously.. 

這是我有什麼,所以far..just從複製和粘貼互聯網..但我不知道如何走得更遠...

回答

1

我發現了一個有趣的方式來做到這一點。我的代碼只是從以下三個來源複製。

how to create grayscale image from nsimage in swift?

Greyscale Image using COCOA and NSImage

Changing the Color Space of NSImage: The second reply

我的代碼:

func saveImage(image:NSImage, destination:URL) throws{ 
    let rep = greyScale(image); 
    var data = rep.representation(using: NSJPEGFileType, properties: [:]); 
    try data?.write(to: destination); 

} 

// rgb2gray 
func greyScale(image: NSImage) -> NSBitmapImageRep{ 
    let w = image.size.width 
    let h = image.size.height 
    let imageRect : NSRect! = NSMakeRect(0,0, w, h); 
    let colourSpace : ColourSpace! = CGColorSpaceCreateDeviceGray(); 
    let context : CGContext! = CGContext(data: nil, width: Int(w), 
             height: Int(h), bitsPerComponent: 8, 
             bytesPerRow: 0, space: colorSpace, 
             bitmapInfo: CGImageAlphaInfo.none.rawValue);   

    context.draw(nsImageToCGImage(image: image), in: imageRect); 
    let greyImage : CGImage! = context.makeImage(); 

    return NSBitmapImageRep(cgImage: greyImage); 
} 


func nsImageToCGImage(image: NSImage) -> CGImage{ 
    if let imageData = image.tiffRepresentation as NSData! { 
     let imageSource : CGImageSource! = CGImageSourceCreateWithData(imageData, 
             nil); 
     let image = CGImageSourceCreateImageAtIndex(imageSource, 0, nil); 
     return image; 
    } 
    return nil; 
} 

我仍然在試圖瞭解背後的原理。

0

你可以試試CIFilter。該煩惱的是,你要來回轉換NSImageCIImage之間:

import Cocoa 
import CoreImage 

let url = Bundle.main.url(forResource: "image", withExtension: "jpg")! 
let image = CIImage(contentsOf: url)! 

let bwFilter = CIFilter(name: "CIColorControls", withInputParameters: ["inputImage": image, "inputSaturation": 0.0])! 

if let ciImage = bwFilter.outputImage { 
    let rep = NSCIImageRep(ciImage: ciImage) 
    let nsImage = NSImage(size: rep.size) 
    nsImage.addRepresentation(rep) 
    // nsImage is now your black-and-white image 
} 
+0

[更改NSImage的顏色空間](http://stackoverflow.com/questions/3253521/changing-the-color-space-of-nsimage/42042573#42042573)我發現這篇文章教學如何更改顏色空間。我正在進行最後的掩蔽步驟。 – user3390652

+0

我努力以合適的方式努力做到這一點的原因是我發現蘋果的框架已經定義了一些色彩空間。我不需要擔心未來其他顏色空間的設置:) – user3390652