Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create grayscale image from nsimage in swift?

I created two applications: one for mac and one for iPhone. iPhone sends the video frames it captured to mac using MultipeerConnectivity framework. I have managed to find code for converting an UIimage to grayscale using this code:

func convertToGrayScale(image: UIImage) -> UIImage {
        let imageRect:CGRect = CGRectMake(0, 0, image.size.width, image.size.height)
        let colorSpace = CGColorSpaceCreateDeviceGray()
        let width = image.size.width
        let height = image.size.height
        let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.None.rawValue)
        let context = CGBitmapContextCreate(nil, Int(width), Int(height), 8, 0, colorSpace, bitmapInfo.rawValue)
        CGContextDrawImage(context, imageRect, image.CGImage)
        let imageRef = CGBitmapContextCreateImage(context)
        let newImage = UIImage(CGImage: imageRef!)
        return newImage
    }

In the code below, it sends the video frame to Mac:

func captureOutput(captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!) {
        let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)
        CVPixelBufferLockBaseAddress(imageBuffer!, kCVPixelBufferLock_ReadOnly)
        let baseAddress = CVPixelBufferGetBaseAddress(imageBuffer!)
        let bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer!)
        let width = CVPixelBufferGetWidth(imageBuffer!)
        let height = CVPixelBufferGetHeight(imageBuffer!)
        CVPixelBufferUnlockBaseAddress(imageBuffer!, 0)
        let colorSpace = CGColorSpaceCreateDeviceRGB()
        let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedLast.rawValue)
        let context = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, bitmapInfo.rawValue)
        let quarzImage = CGBitmapContextCreateImage(context)
        let image = UIImage(CGImage: quarzImage!)
        let grayImage = convertToGrayScale(image)
        let data: NSData = UIImagePNGRepresentation(grayImage)!
        delegate?.recievedOutput(data)
    }

The delegate method is just sending the data using session.sendData()

So, here comes to the Mac side. When mac received NSData, I created an NSImage from the data and created a .png image file using this code:

func session(session: MCSession, didReceiveData data: NSData, fromPeer peerID: MCPeerID) {
        let image: NSImage = NSImage(data: data)!.imageRotatedByDegreess(270)
        let cgRef = image.CGImageForProposedRect(nil, context: nil, hints: nil)
        let representation = NSBitmapImageRep(CGImage: cgRef!)
        let pngData = representation.representationUsingType(NSBitmapImageFileType.NSPNGFileType, properties: [NSImageCompressionFactor: 1.0])
        pngData?.writeToFile("/Users/JunhongXu/Desktop/image/\(result.description).png", atomically: true)
        result[4]++
        self.delegate?.presentRecievedImage(image)
    }

Although the image is like the picture below, when I checked my image file property, it is in RGB format. How can I change the ColorSpace of my NSImage to grayscale instead of RGB?

enter image description here

like image 414
Junhong Xu Avatar asked Sep 26 '22 09:09

Junhong Xu


1 Answers

I have found a simple solution to my problem. Since it is already in grayscale when it transimitted to my Mac, I am able to use the code below to convert the image representation's ColorSpace to grayscale and save it as a .png file:

let newRep = representation.bitmapImageRepByConvertingToColorSpace(NSColorSpace.genericGrayColorSpace(), renderingIntent: NSColorRenderingIntent.Default)
let pngData = newRep!.representationUsingType(NSBitmapImageFileType.NSPNGFileType, properties: [NSImageCompressionFactor: 1.0])
pngData?.writeToFile("/Users/JunhongXu/Desktop/image/\(result.description).png", atomically: true)
like image 164
Junhong Xu Avatar answered Oct 11 '22 05:10

Junhong Xu