选择给定颜色

时间:2015-05-21 19:30:22

标签: ios uiimage core-graphics core-image

我想实现一项功能,允许用户从图像中选择给定的颜色,并用透明色替换它。理想情况下,它的工作方式类似于Pixelmator,https://www.youtube.com/watch?v=taXGaQC0JBg,用户可以选择颜色并查看当前选择的图像部分以及可用于选择颜色容差的滑块。

我对替换颜色的主要怀疑是CGImageCreateWithMaskingColors()函数。也许CIColorCube可能会完成这项工作。 我不知道如何继续可视化颜色的选择。任何提示都将受到欢迎! 谢谢, 雅努什

编辑: 我的行动非常缓慢,但我取得了一些进展。我正在使用imageCreateWithMaskingColors函数来提取不需要的颜色:

    func imageWithMaskingColors(){
        //get uiimage
        let image:CGImageRef = self.inputImage.image!.CGImage
        let maskingColors :[CGFloat] = [0,200,0,255,0,255]


        let newciimage = CGImageCreateWithMaskingColors(image,maskingColors)

        let newImage = UIImage(CGImage: newciimage)
        self.outputImage.image = newImage
        let w = CGFloat(CGImageGetWidth(newciimage))
        let h = CGFloat(CGImageGetHeight(newciimage))
        let size = CGSizeMake(w,h)

        UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
        let context = UIGraphicsGetCurrentContext();

        newImage?.drawInRect(CGRectMake(0, 0, w, h))

        let result  = UIGraphicsGetImageFromCurrentImageContext();
        self.inputImage1 = result.CGImage
        UIGraphicsEndImageContext();


    }

Extracting Colors

在下一步中,我将应用CISourceOutCompositing CIFilter来获取选定区域,该区域在最后一步中被删除:  @IBAction func blendMode(){

    let context = CIContext(options: nil)
    let inputImage:CIImage = CIImage(CGImage:self.inputImage1)
    var filter = CIFilter(name: "CISourceOutCompositing")
    println(inputImage.debugDescription)

    //mix it with black
    let fileURL = NSBundle.mainBundle().URLForResource("black", withExtension: "jpg")
    var backgroundImage = CIImage(contentsOfURL: fileURL)
    filter.setValue(inputImage, forKey: kCIInputBackgroundImageKey)
     filter.setValue(backgroundImage, forKey: kCIInputImageKey)
    println(backgroundImage.debugDescription)
    let outputImage = filter.outputImage
    println(outputImage.debugDescription)
    let cgimg = context.createCGImage(outputImage, fromRect: outputImage.extent())

    blendImage1 = cgimg

    let newImage = UIImage(CGImage: cgimg)

    self.outputImage.image = newImage

}

Selected Area 在下一步中,我想在边框上添加一个虚线描边线,并删除所选图像(黑虎)的填充颜色。

我在图像中使用了GPUImage CannyEdgeDetectionFilter,但它没有给我提供完美的结果(黑色图像)

let gpaPicture = GPUImagePicture(CGImage: blendImage1)
        let canny = GPUImageCannyEdgeDetectionFilter()
        //canny.upperThreshold = CGFloat(1125)
        canny.lowerThreshold = CGFloat(1)
        gpaPicture.addTarget(canny)
        canny.useNextFrameForImageCapture()
        gpaPicture.processImage()
        let gpuResult = canny.imageByFilteringImage(UIImage(CGImage:blendImage1))

0 个答案:

没有答案