简体   繁体   English

放大图像中的线条:通过图像处理和膨胀?

[英]Enlarge lines in image: through image processing and Dilation?

I have a lot of icons here. 我这里有很多图标。 Simple monochrome symbols of a bed, a chair etc. Now I would like to make the lines thicker in these images. 床,椅子等的简单单色符号。现在,我想在这些图像中使线条更粗。 Since there are 100 icons and I'm afraid of changing all the images by hand, I wonder whether there is a programmatically way of achieving this? 由于有100个图标,而且我担心用手更改所有图像,因此我想知道是否存在以编程方式实现此目的的方法? I know that there is Dilation in Image Processing. 我知道图像处理中有膨胀。 But I neither know if this would work nicely nor how to implement this in my project? 但是我既不知道这是否行得通,又不知道如何在我的项目中实现它?

Thank you for your help! 谢谢您的帮助!

I was playing around with this and I already created something that is working for me. 我正在玩这个游戏,并且已经创建了一些对我有用的东西。 The following code is applying Dilate filter to an image - I implemented it as a category to UIImage. 以下代码将Dilate滤镜应用于图像-我将其实现为UIImage的类别。 I'm happy with it since it was working fine for 95% percent of my monochrome icons. 我对此感到满意,因为它对我95%的单色图标都可以正常工作。 The icons which had some very fine structures I had to redo by hand (meaning creating new images). 我必须手动重做具有非常精细结构的图标(意味着创建新图像)。

#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;
} 

结构元素,组织图像和结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM