【问题标题】:Enlarge lines in image: through image processing and Dilation?放大图像中的线条:通过图像处理和 Dilation?
【发布时间】:2015-03-04 10:09:31
【问题描述】:

我这里有很多图标。床、椅子等简单的单色符号。现在我想让这些图像中的线条更粗。由于有 100 个图标,我害怕手动更改所有图像,我想知道是否有一种编程方式来实现这一点? 我知道图像处理中有膨胀。但我不知道这是否会很好地工作,也不知道如何在我的项目中实现它?

感谢您的帮助!

【问题讨论】:

    标签: objective-c image image-processing icons core-graphics


    【解决方案1】:

    我一直在玩这个,我已经创建了一些对我有用的东西。以下代码将扩张过滤器应用于图像 - 我将其作为 UIImage 的类别实现。我对它很满意,因为它适用于我 95% 的单色图标。我不得不手工重做具有一些非常精细结构的图标(意味着创建新图像)。

    #import "UIImage+Dilation.h"
    
    @implementation UIImage (Dilation)
    
    -(UIImage*) applyDilation {
        CGImageRef imageRef = [self CGImage];
    
        NSUInteger width = CGImageGetWidth(imageRef);
        NSUInteger height = CGImageGetHeight(imageRef);
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    
        NSUInteger bytesPerPixel = 4;
        NSUInteger bytesPerRow = bytesPerPixel * width;
        NSUInteger bitsPerComponent = 8;
        NSUInteger bitmapByteCount = bytesPerRow * height;
    
        unsigned char *rawData = (unsigned char*) calloc(bitmapByteCount,sizeof(unsigned char));
        unsigned char *rawDataCopy = (unsigned char*) calloc(bitmapByteCount, sizeof(unsigned char));
    
        CGContextRef context = CGBitmapContextCreate(rawData, width, height,
                                                 bitsPerComponent, bytesPerRow, colorSpace,
                                                 kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
    
        CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
    
        // make copy of original raw data for upcoming pixel comparison
        for (int i=0; i<bitmapByteCount; i++) rawDataCopy[i] = rawData[i];
    
       float brightness01, brightness10, brightness11, brightness12, brightness21, maxBrightness;
       int byteIndex01, byteIndex10, byteIndex11, byteIndex12, byteIndex21, tempIndex;
        unsigned char red, green, blue, alpha;
    
        byteIndex11 = 0;
    
        while (byteIndex11 < bitmapByteCount) {
            // determine byte indexes for structuring element
            byteIndex01 = (((int)(byteIndex11 - bytesPerRow) >= 0) ? (byteIndex11 - bytesPerRow) : -1);
            byteIndex10 = ((((int)(byteIndex11 - bytesPerPixel) >= 0) && ((byteIndex11 - bytesPerPixel) != bytesPerRow)) ? (byteIndex11 - bytesPerPixel) : -1);
            byteIndex12 = (((byteIndex11 + bytesPerPixel) < bitmapByteCount) ? (byteIndex11 + bytesPerPixel) : -1);
            byteIndex21 = (((int)(byteIndex11 + bytesPerRow) < bitmapByteCount) ? (byteIndex11 + bytesPerRow) : -1);
    
            // calculate brightness of each pixel and also determine max brightness
            brightness01 = ((byteIndex01 > 0) ? (((rawDataCopy[byteIndex01]*0.3f + rawDataCopy[byteIndex01 + 1]*0.59f + rawDataCopy[byteIndex01 + 2]*0.11f)/255.0f)*(rawDataCopy[byteIndex01 + 3]/255.0f)) : 0.0f);
            brightness10 = ((byteIndex10 > 0) ? (((rawDataCopy[byteIndex10]*0.3f + rawDataCopy[byteIndex10 + 1]*0.59f + rawDataCopy[byteIndex10 + 2]*0.11f)/255.0f)*(rawDataCopy[byteIndex10 + 3]/255.0f)) : 0.0f);
            tempIndex = ((brightness01 > brightness10) ? byteIndex01 : byteIndex10);
            maxBrightness = ((brightness01 > brightness10) ? brightness01 : brightness10);
            brightness12 = ((byteIndex12 > 0) ? (((rawDataCopy[byteIndex12]*0.3f + rawDataCopy[byteIndex12 + 1]*0.59f + rawDataCopy[byteIndex12 + 2]*0.11f)/255.0f)*(rawDataCopy[byteIndex12 + 3]/255.0f)) : 0.0f);
            tempIndex = ((brightness12 > maxBrightness) ? byteIndex12 : tempIndex);
            maxBrightness = ((brightness12 > maxBrightness) ? brightness12 : maxBrightness);
            brightness21 = ((byteIndex21 > 0) ? (((rawDataCopy[byteIndex21]*0.3f + rawDataCopy[byteIndex21 + 1]*0.59f + rawDataCopy[byteIndex21 + 2]*0.11f)/255.0f)*(rawDataCopy[byteIndex21 + 3]/255.0f)) : 0.0f);
            tempIndex = ((brightness21 > maxBrightness) ? byteIndex21 : tempIndex);
            maxBrightness = ((brightness21 > maxBrightness) ? brightness21 : maxBrightness);
            brightness11 = ((byteIndex11 > 0) ? (((rawDataCopy[byteIndex11]*0.3f + rawDataCopy[byteIndex11 + 1]*0.59f + rawDataCopy[byteIndex11 + 2]*0.11f)/255.0f)*(rawDataCopy[byteIndex11 + 3]/255.0f)) : 0.0f);
            tempIndex = ((brightness11 >= maxBrightness) ? byteIndex11 : tempIndex);
    
            // save components of brightest pixel
            red = rawDataCopy[tempIndex];
            green = rawDataCopy[tempIndex+1];
            blue = rawDataCopy[tempIndex+2];
            alpha = rawDataCopy[tempIndex+3];
    
            // copy component values to current center pixel for output image
            rawData[byteIndex11] = MIN(red, 255.0f);
            rawData[byteIndex11 + 1] = MIN(green, 255.0f);
            rawData[byteIndex11 + 2] = MIN(blue, 255.0f);
            rawData[byteIndex11 + 3] = alpha;
    
            byteIndex11 += bytesPerPixel;
        }
    
        imageRef = CGBitmapContextCreateImage(context);
    
        UIImage *result = [UIImage imageWithCGImage:imageRef scale:self.scale orientation:NULL];
    
        CFRelease(imageRef);
        CGContextRelease(context);
        free(rawData);
        free(rawDataCopy);
    
        return result;
    } 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-27
      • 2013-11-16
      • 2018-07-25
      • 2016-06-21
      • 1970-01-01
      • 2014-04-14
      • 2023-03-25
      相关资源
      最近更新 更多