0

我有一个高度图(灰度图像,黑色作为最低区域,白色作为最高区域),我试图隔离高于或低于某个阈值的所有内容。我的目标是能够获取任何高度图,并返回一个新的 UIImage,其中复制低于特定高度(即比给定颜色暗)的所有内容,但高于(即更亮)的所有内容都是透明的。

在此处输入图像描述

现在我对此很陌生,所以如果有更好的方法来解决这个问题,我很想听听,但我的第一直觉是创建一个具有相同框架的新 UIImage,然后根据原稿中给定区域的亮度。就像是...

-(UIImage*)mapIsolatedFromThreshold:(CGFloat)threshold showAbove:(BOOL)above;

因此,threshold变量可以是0.7or0.2或其他,然后您可以设置aboveYES是否希望一切都高于阈值(即更亮),或者NO如果你希望一切都低于阈值(即更暗)。

问题是,我以前从未做过图像处理或绘图,所以我不知道如何设置该方法。我有一种模糊的感觉,它会涉及UIGraphicsBeginImageContext等等,但我无法找到任何基于另一张图像的亮度使用它的问题或教程。

我将如何制作这样的方法?或者是否有一些预先存在的系统来处理 iOS 中的高度图,这使得这一切比我处理它要容易得多?

4

1 回答 1

0

经过几个小时的搜寻,我能够拼凑出这种方法。如果其他人需要做类似的事情,就在这里。我仍然不完全理解它在做什么,一步一步,但最终的结果是我想要的。

-(UIImage*)imageWithLumaMaskFromDark:(CGFloat)lumaFloor toLight:(CGFloat)lumaCeil forImage:(UIImage*)image {
    CGImageRef rawImageRef = image.CGImage;

    const CGFloat colorMasking[6] = {lumaFloor, lumaCeil, lumaFloor, lumaCeil, lumaFloor, lumaCeil};

    UIGraphicsBeginImageContext(image.size);
    CGImageRef maskedImageRef = CGImageCreateWithMaskingColors(rawImageRef, colorMasking);
    {
        //if in iphone
        CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0.0, image.size.height);
        CGContextScaleCTM(UIGraphicsGetCurrentContext(), 1.0, -1.0);
    }

    CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, image.size.width, image.size.height), maskedImageRef);
    UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
    CGImageRelease(maskedImageRef);
    UIGraphicsEndImageContext();
    return result;
}

lumaFloorlumaCeil输入取 0 到 255 之间的值,超出该范围的所有内容都变得完全透明。

于 2015-08-13T20:39:57.373 回答