GPUImage GroupFilter,亮度和对比度iOS

时间:2015-01-26 15:26:23

标签: ios filter gpuimage brightness contrast

我在我的应用中使用了GPUImage,并且我试图改变图像的亮度和对比度。

我创建了一个带有源图片的GPUImageView,并设置了将使用UISlider修改的亮度滤镜和对比度滤镜

所以这是我的代码:

首先

·H

__weak IBOutlet GPUImageView* imageView;

的.m

GPUImagePicture* sourcePicture;
GPUImageBrightnessFilter *brightnessFilter;
GPUImageContrastFilter *contrastFilter;

当我的视图控制器被加载时,我接下来要做的事情:

UIImage* image = "[image fromAnotherViewController]";
sourcePicture = [[GPUImagePicture alloc] initWithImage:image smoothlyScaleOutput:YES];

brightnessFilter = [[GPUImageBrightnessFilter alloc] init];
contrastFilter = [[GPUImageContrastFilter alloc] init];

[brightnessFilter forceProcessingAtSize:imageView.sizeInPixels];
[contrastFilter forceProcessingAtSize:imageView.sizeInPixels];

[sourcePicture addTarget:brightnessFilter];
[brightnessFilter addTarget:contrastFilter];

[contrastFilter addTarget:imageViewBewo];

[sourcePicture processImage];

这里是对比度和亮度值改变方法

- (IBAction)brightnessValueChanged:(id)sender
{
    [brightnessFilter setBrightness:brightnessSlider.value];
    [sourcePicture processImage];
}

- (IBAction)contrastValueChanged:(id)sender
{
    [contrastFilter setContrast:contrastSlider.value];
    [sourcePicture processImage];
}

在此之前一切正常,所以我有一个按钮可以将滤镜应用到图像

- (IBAction) applyFilterAction:(id)sender
{
    [sourcePicture removeAllTargets];

    GPUImageLordKelvinFilter* filterEffect = [[GPUImageLordKelvinFilter alloc] init];

    [filterEffect forceProcessingAtSize:imageView.sizeInPixels];
    [brightnessFilter forceProcessingAtSize:imageView.sizeInPixels];
    [contrastFilter forceProcessingAtSize:imageView.sizeInPixels];

    [sourcePicture addTarget:brightnessFilter];
    [brightnessFilter addTarget:contrastFilter];
    [brightnessFilter addTarget:filterEffect];

    [filterEffect addTarget:imageView];

    [brightnessFilter setBrightness:brightnessSlider.value];
    [contrastFilter setContrast:contrastSlider.value];

    [sourcePicture processImage];
}

正确应用了滤镜,但是当我移动滑块时,imageView又回到原始图像,对比度和亮度正确修改但没有滤镜效果。

但是,如果我在应用滤镜效果的同时修改亮度或对比度,则会在滤镜效果中正确应用亮度或对比度,例如:

// in applyFilterAction: methods
.
. 
.
[filterEffect addTarget:imageView];

[brightnessFilter setBrightness:-0.5];
[contrastFilter setContrast:2.5];

[sourcePicture processImage];
.
.
.

任何人都可以给我任何帮助来应用过滤器并修改它们的值,我认为我在使用addTarget方法做错了,我正在搜索有关这方面的内容,但无法找到没什么

2 个答案:

答案 0 :(得分:1)

您的-applyFilterAction:方法正在破坏您的过滤器链,以引入您的新过滤器。如果这就是你想要的,那很好,但是这样做的事情就会变得有点奇怪。

在原始链中,你去

sourcePicture -> brightnessFilter -> contrastFilter > imageView

在新链中,现在是

sourcePicture -> brightnessFilter -> filterEffect -> imageView
                                  -> contrastFilter > imageView

您已在brightnessFilter中引入了分支,方法是将其定位为filterEffect和contrastFilter过滤器。 contrastFilter从未从定位imageView中删除,因此它会继续定位。

将两个目标发送到需要单个输入的内容(如GPUImageView)可能会导致未定义的行为。关于那种链接的东西是错误的。要么意味着在contrastFilter之后链接filterEffect,要么filterEffect和contrastFilter需要定位两个单独的GPUImageViews。在前一种情况下,您需要在设置链的其余部分之前将目标从contrastFilter移除到imageView。

最好在重置过滤器链之前在链中的每个过滤器上调用-removeAllTargets,以确保没有像这样的任何悬空连接。

答案 1 :(得分:0)

尝试这些..我希望它可以帮助你..

-(void)viewDidLoad
{        
 [sliderChange setMinimumValue:-0.5];
 [sliderChange setMaximumValue:0.5];
 [sliderChange setValue:0.0];
 brightnessFilter = [[GPUImageBrightnessFilter alloc] init];
}
-(IBAction)upDateSliderValue:(id)sender 
{
 GPUImagePicture *fx_image;
 fx_image = [[GPUImagePicture alloc] initWithImage:originalImage];
 [brightnessFilter setBrightness:self.sliderChange.value];
 [fx_image addTarget:brightnessFilter];
 [fx_image processImage];
 UIImage *final_image = [brightnessFilter imageFromCurrentlyProcessedOutput];
 self.selectedImageView.image = final_image;
}