我这里有很多图标。简单的单色符号的床,椅子等。现在我想在这些图像中使线条更粗。由于有100个图标,我害怕手动更改所有图像,我想知道是否有一种编程方式来实现这一目标? 我知道图像处理中存在扩张。但我不知道这是否能很好地工作,也不知道如何在我的项目中实现这一点?
感谢您的帮助!
答案 0 :(得分:1)
我正在玩这个,我已经创造了一些适合我的东西。以下代码将Dilate过滤器应用于图像 - 我将其实现为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;
}