Get profile
#import <opencv2/opencv.hpp> #import <opencv2/imgcodecs/ios.h> #import <opencv2/imgproc/types_c.h > @implementation opencvhelper+ (UIImage *) Getimageoutline: (UIImage *) iputimg{ cv::mat cvimage; Uiimagetomat (iputimg, cvimage); Cv::mat Gray; Convert the image to grayscale; Cv::cvtcolor (Cvimage, Gray, Cv_rgba2gray); Apply Gaussian Filter to remove small edges cv::gaussianblur (gray, Gray, Cv::size (5,5), 1.2,1.2); Calculate edges with Canny cv::mat edges; Cv::canny (gray, edges, 0,); Fill image with white color //cvimage.setto (cv::scalar (255,255,255,1)); Change color on edges //Cvimage.setto (Cv::scalar (0,128,255,255), edges); Cv::mat cvoutimage= Cv::mat::zeros (Cvimage.size (), Cvimage.type ()); Cvoutimage.setto (Cv::scalar (255,255,255,0)); Cvoutimage.setto (Cv::scalar (0,139,139,1), edges); Return Mattouiimage (cvoutimage);} @end
Convert to Transparent
-(UIImage *) Transparentimage: (UIImage *) image{//Create a pixel buffer in an easy to use format Cgimageref imag Eref = [Image cgimage]; Nsuinteger width = cgimagegetwidth (imageref); Nsuinteger height = cgimagegetheight (imageref); Cgcolorspaceref colorspace = Cgcolorspacecreatedevicergb (); UInt8 * m_pixelbuf = malloc (sizeof (UInt8) * Height * width * 4); Nsuinteger bytesperpixel = 4; Nsuinteger Bytesperrow = bytesperpixel * width; Nsuinteger bitspercomponent = 8; Cgcontextref context = Cgbitmapcontextcreate (m_pixelbuf, width, height, bi Tspercomponent, Bytesperrow, ColorSpace, Kcgimagealphapremultipliedlast | KCGBITMAPBYTEORDER32BIG); Cgcontextdrawimage (context, CGRectMake (0, 0, width, height), imageref); Cgcontextrelease (context); alter the alpha int length = height * Width * 4; for (int i=0; i<length; i+=4) {if (M_pixelbuf[i+0] ==255) {m_pixelbuf[i+0] = 0; M_pixelbuf[i+1] = 0; M_PIXELBUF[I+2] = 0; M_PIXELBUF[I+3] = 0; }}//create a new image cgcontextref CTX = Cgbitmapcontextcreate (m_pixelbuf, width, height, Bitspercomponent, Bytesperrow, ColorSpace, KC Gimagealphapremultipliedlast | KCGBITMAPBYTEORDER32BIG); Cgimageref newimgref = Cgbitmapcontextcreateimage (CTX); Cgcolorspacerelease (ColorSpace); Cgcontextrelease (CTX); Free (M_PIXELBUF); UIImage *finalimage = [UIImage imagewithcgimage:newimgref]; Cgimagerelease (NEWIMGREF); return finalimage;}
Opecv Getting transparent image outlines