LordBullington
LordBullington

Reputation: 219

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? 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!

Upvotes: 0

Views: 195

Answers (1)

LordBullington
LordBullington

Reputation: 219

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. I'm happy with it since it was working fine for 95% percent of my monochrome icons. 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;
} 

Structuring element, org image and result

Upvotes: 1

Related Questions