1// This file is part of OpenCV project. 2// It is subject to the license terms in the LICENSE file found in the top-level directory 3// of this distribution and at http://opencv.org/license.html. 4 5#include "apple_conversions.h" 6#import <AppKit/AppKit.h> 7 8CV_EXPORTS NSImage* MatToNSImage(const cv::Mat& image); 9CV_EXPORTS void NSImageToMat(const NSImage* image, cv::Mat& m, bool alphaExist); 10 11NSImage* MatToNSImage(const cv::Mat& image) { 12 // Creating CGImage from cv::Mat 13 CGImageRef imageRef = MatToCGImage(image); 14 15 // Getting NSImage from CGImage 16 NSImage *nsImage = [[NSImage alloc] initWithCGImage:imageRef size:CGSizeMake(CGImageGetWidth(imageRef), CGImageGetHeight(imageRef))]; 17 CGImageRelease(imageRef); 18 19 return nsImage; 20} 21 22void NSImageToMat(const NSImage* image, cv::Mat& m, bool alphaExist) { 23 CGImageRef imageRef = [image CGImageForProposedRect:NULL context:NULL hints:NULL]; 24 CGImageToMat(imageRef, m, alphaExist); 25} 26