码迷,mamicode.com
首页 > 其他好文 > 详细

opecv获取透明图像轮廓

时间:2015-01-23 13:16:22      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:

获取轮廓

#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, 60);
    // 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

转化成透明

-(UIImage *)transparentImage:(UIImage *)image
{
    
    // Create a pixel buffer in an easy to use format
    CGImageRef imageRef = [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,
                                                 bitsPerComponent, 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,
                                             kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
    
    CGImageRef newImgRef = CGBitmapContextCreateImage(ctx);
    CGColorSpaceRelease(colorSpace);
    CGContextRelease(ctx); 
    free(m_PixelBuf);
    
    UIImage *finalImage = [UIImage imageWithCGImage:newImgRef];
    CGImageRelease(newImgRef); 
    
    return finalImage;
}

  

opecv获取透明图像轮廓

标签:

原文地址:http://www.cnblogs.com/bandy/p/4243847.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!