IOS OpenCV Image Grayscale Processing

Source: Internet
Author: User

Sometimes in the development process, the graph can not meet our needs, at this time we need to grayscale processing of images, such as QQ Avatar online, offline and other different states.

solutions that you can try:

First: Let the UI re-slice the diagram
The second kind: Cut graph does not work, implements with the code (regenerates the picture with the grayscale)

First introduce the full name of OPENCV,OPENCV: Open Source computer Vision Library. OpenCV is a BSD-licensed (open source) distributed, cross-platform computer Vision library that can run on Linux, Windows, Android, and Mac OS operating systems. It is lightweight and efficient-consisting of a series of C functions and a small number of C + + classes, and provides interfaces to Python, Ruby, Matlab, and many more general-purpose algorithms for image processing and computer vision. Haha, is not very verbose, simple point is a processing image of the visual library.

Implementation steps:
The first way: OpenCV3 implementation

    • First step: Import the OPENCV development package
    • Step Two: function implementation
      The use of OPENCV is relatively simple, the implementation code is as follows:
- (UIImage*)imageToGrayImage:(UIImage*)image{    //image源文件    // 1.将iOS的UIImage转成c++图片(数据:矩阵)    Mat mat_image_gray;    UIImageToMat(image, mat_image_gray);    // 2. 将c++彩色图片转成灰度图片    // 参数一:数据源    // 参数二:目标数据    // 参数三:转换类型    Mat mat_image_dst;    cvtColor(mat_image_gray, mat_image_dst, COLOR_BGRA2GRAY);    // 3.灰度 -> 可显示的图片    cvtColor(mat_image_dst, mat_image_gray, COLOR_GRAY2BGR);    // 4. 将c++处理之后的图片转成iOS能识别的UIImage    return MatToUIImage(mat_image_gray);}

Second approach: Using the iOS system default development library implementation
This way can be said to be cumbersome enough, disgusting, hahaha

#pragma mark-implemented with system-brought libraries- (UIImage*) Systemimagetograyimage: (UIImage*) image{intwidth = image. Size. Width;intHeight = image. Size. Height;///Step one: Create a color space (white is a piece of color memory space)Cgcolorspaceref COLORREF = Cgcolorspacecreatedevicegray ();///Step Two: Color space context (saving image data information)    //Parameter one: address (memory address) pointing to this memory area    //Parameter two: To open up the size of the memory, picture width    //Parameter three: Picture High    //Parameter four: number of pixel bits (color space, for example: 32-bit pixel format and RGB color space, 8-bit)    //Parameter five: number of bits of memory occupied by each line of the picture    //Parameter VI: Color space    //Parameter VII: whether the picture contains a channel (ARGB four channels)Cgcontextref context = Cgbitmapcontextcreate (Nil, width, height,8,0, COLORREF, Kcgimagealphanone);//Free memoryCgcolorspacerelease (COLORREF);if(Context = =Nil) {return  Nil; }//Render picture    //Parameter One: Context object    //Parameter two: Render area    //Source pictureCgcontextdrawimage (Context, CGRectMake (0,0, width, height), image. Cgimage);;//Convert the painted color space to CgimageCgimageref Grayimageref = cgbitmapcontextcreateimage (context);//Convert A/C + + picture to a picture that iOS can display    UIImage*dstimage = [UIImageIMAGEWITHCGIMAGE:GRAYIMAGEREF];//Free memoryCgcontextrelease (context); Cgimagerelease (GRAYIMAGEREF);returnDstimage;}

IOS OpenCV Image Grayscale Processing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.