美文网首页iOS开发攻城狮的集散地iOS开发经验收集iOS
【IOS】图片二值化和黑白(灰度)处理

【IOS】图片二值化和黑白(灰度)处理

作者: 雨影 | 来源:发表于2017-05-10 09:45 被阅读1019次
    image.png
    /**
     二值化
     */
    - (UIImage *)covertToGrayScale{
        
        CGSize size =[self size];
        int width =size.width;
        int height =size.height;
        
        //像素将画在这个数组
        uint32_t *pixels = (uint32_t *)malloc(width *height *sizeof(uint32_t));
        //清空像素数组
        memset(pixels, 0, width*height*sizeof(uint32_t));
        
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
        
        //用 pixels 创建一个 context
        CGContextRef context =CGBitmapContextCreate(pixels, width, height, 8, width*sizeof(uint32_t), colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedLast);
        CGContextDrawImage(context, CGRectMake(0, 0, width, height), [self CGImage]);
        
        int tt =1;
        CGFloat intensity;
        int bw;
        
        for (int y = 0; y <height; y++) {
            for (int x =0; x <width; x ++) {
                uint8_t *rgbaPixel = (uint8_t *)&pixels[y*width+x];
                intensity = (rgbaPixel[tt] + rgbaPixel[tt + 1] + rgbaPixel[tt + 2]) / 3. / 255.;
                
                bw = intensity > 0.45?255:0;
                
                rgbaPixel[tt] = bw;
                rgbaPixel[tt + 1] = bw;
                rgbaPixel[tt + 2] = bw;
                
            }
        }
    
        // create a new CGImageRef from our context with the modified pixels
        CGImageRef image = CGBitmapContextCreateImage(context);
        
        // we're done with the context, color space, and pixels
        CGContextRelease(context);
        CGColorSpaceRelease(colorSpace);
        free(pixels);
        // make a new UIImage to return
        UIImage *resultUIImage = [UIImage imageWithCGImage:image];
        // we're done with image now too
        CGImageRelease(image);
        
        return resultUIImage;
    }
    

    处理之后的效果:


    image.png
    /**
     转化灰度
     */
    - (UIImage *)grayImage{
       
        int width = self.size.width;
        int height = self.size.height;
        
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
        
        CGContextRef context = CGBitmapContextCreate (nil,
                                                      width,
                                                      height,
                                                      8,      // bits per component
                                                      0,
                                                      colorSpace,
                                                      kCGImageAlphaNone);
        
        CGColorSpaceRelease(colorSpace);
        
        if (context == NULL) {
            return nil;
        }
        
        CGContextDrawImage(context,
                           CGRectMake(0, 0, width, height), self.CGImage);
        
        UIImage *grayImage = [UIImage imageWithCGImage:CGBitmapContextCreateImage(context)];
        CGContextRelease(context);
        
        return grayImage;
    }
    

    处理之后的效果:


    image.png

    Demo地址:https://github.com/yuying2012/WJDStudyLibrary
    这是一个大工程,请从工程中寻找相关模块代码.

    相关文章

      网友评论

      • 空气清新剂:问下:你这里二值化时的阈值选择;有什么好点的建议没有?
        雨影:@空气清新剂 微调吧,好久之前写的了

      本文标题:【IOS】图片二值化和黑白(灰度)处理

      本文链接:https://www.haomeiwen.com/subject/xlmwtxtx.html