如何有效地应用GPUImage滤镜? [英] How To Apply GPUImage filters efficiently?

查看:89
本文介绍了如何有效地应用GPUImage滤镜?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我为图像添加颜色,然后过一秒钟,我对其应用了Kuwahara滤镜,以获得水彩效果,但问题是应用滤镜会花费一些时间,如果我过多地改变了颜色,应用最终由于内存问题而崩溃.谁能帮助我如何以最佳方式使用过滤器.谢谢

In my project, I apply colors to an image then after a second I am applying a Kuwahara filter to it, for watercolor effect, but the thing is it takes time to apply the filter and if I change the colors too much the app eventually crashes due to memory issues. Can anyone help me how to use the filter in the best manner. Thanks

CODE

@objc func fillColorButtonTapped(_ sender : UIButton){

    self.processingModel.isImageMixedColor = false

    if let popoverController = self.mkColorPicker.popoverPresentationController{
        popoverController.delegate = self.mkColorPicker
        popoverController.permittedArrowDirections = .any
        popoverController.sourceView = sender
        popoverController.sourceRect = sender.bounds
    }

    self.present(self.mkColorPicker, animated: true, completion: nil)

    self.mkColorPicker.selectedColor = { [weak self] color in

        guard let strongSelf = self else {
            return
        }

        let image = ChangeColor.image(byReplacingColor: strongSelf.processingModel.pencileDefaultImage, withSourceColor: .black, withMinTolerance: 0.4, withMaxTolerance: 0.5, with: color)
        strongSelf.processingModel.croppedImageToWorkOn =  image
        UIView.transition(with: strongSelf.handAndFootImageView,
                          duration: 0.2,
                          options: .transitionCrossDissolve,
                          animations: {strongSelf.handAndFootImageView.image = strongSelf.processingModel.croppedImageToWorkOn},
                          completion: nil)

        strongSelf.addWaterColorEffect()

    }
}

func addWaterColorEffect(withRadius : Int = 5){


    CommonClass.delayWithSeconds(0.5, completion: {

        let filter = KuwaharaFilter()
        filter.radius = withRadius
        let imageToFilter = self.containerView.toImage()
        DispatchQueue.main.async {

            let imageToShow =  imageToFilter.filterWithOperation(filter)

            UIView.transition(with: self.handAndFootImageView,
                              duration: 0.6,
                              options: .transitionCrossDissolve,
                              animations: {self.handAndFootImageView.image = imageToShow },
                              completion: nil)

            self.processingModel.croppedImageToWorkOn =  imageToShow
        }


    })

}

推荐答案

这就是我根据自己的想法,使用GPUImage2快速设置过滤器的方法.图像进入彩色滤光片,然后进入溶解混合物(混合物0.0)的源1.然后将颜色滤镜发送到kuwahara滤镜中,然后发送到溶解混合液的来源2中.从那里,您可以在两者之间切换,并随心所欲地更改半径.

This is how I would simply setup the filters using GPUImage2 in swift based upon what I think you are after. Image into color filter, then into source 1 of dissolve blend (mix 0.0). Then send the color filter into the kuwahara filter and then into source 2 of the dissolve blend. From there you can just transition between the two and change the radius however much you want.

func setupFilters() {
    image --> colorFilter --> dissolveBlend
    colorFilter --> kuwaharaFilter --> dissolveBlend --> renderView
    dissolveBlend.mix = 0.0
}

func addWaterColorEffect(withRadius : Int = 5){
    kuwaharaFilter.radius = withRadius
    dissolveBlend.mix = 1.0 
    // This will not give you a transition but you can use a while loop or timer 
    // to change the mix over the course of whatever length of time you are seeking.
}

这篇关于如何有效地应用GPUImage滤镜?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆