简体   繁体   English

如何在swift中创建nsimage的灰度图像?

[英]how to create grayscale image from nsimage in swift?

I created two applications: one for mac and one for iPhone. 我创建了两个应用程序:一个用于mac,一个用于iPhone。 iPhone sends the video frames it captured to mac using MultipeerConnectivity framework. iPhone使用MultipeerConnectivity框架将其捕获的视频帧发送到mac。 I have managed to find code for converting an UIimage to grayscale using this code: 我已经设法找到使用以下代码将UIimage转换为灰度的代码:

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: 在下面的代码中,它将视频帧发送到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() 委托方法只是使用session.sendData()发送数据

So, here comes to the Mac side. 所以,来到Mac方面。 When mac received NSData, I created an NSImage from the data and created a .png image file using this code: 当mac收到NSData时,我从数据创建了一个NSImage,并使用以下代码创建了一个.png图像文件:

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. 虽然图像如下图所示,但当我检查图像文件属性时,它是RGB格式。 How can I change the ColorSpace of my NSImage to grayscale instead of RGB? 如何将NSImage的ColorSpace更改为灰度而不是RGB?

enter image description here 在此输入图像描述

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: 由于它在传输到我的Mac时已经处于灰度级,我可以使用下面的代码将图像表示的ColorSpace转换为灰度并将其保存为.png文件:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM