我使用GPUImage过滤器创建简单的图像亮度,对比度和饱和度调整工具。视图具有一个图像视图和每个过滤器的3个滑块。起初我以为我会应用每个过滤器,因为每个滑块的值都会改变,但后来我意识到过滤器必须被链接。所以我写下面的代码。此代码的问题是只有亮度滤镜应用于图像。
- (IBAction)brightnessSlider:(UISlider *)sender {
brightnessValue = sender.value;
UIImage *inputImage = [UIImage imageNamed:@"2.jpg"];
GPUImagePicture *stillImageSource = [[GPUImagePicture alloc] initWithImage:inputImage];
GPUImageBrightnessFilter *brightnessFilter = [GPUImageBrightnessFilter new];
GPUImageContrastFilter *contrastFilter = [GPUImageContrastFilter new];
GPUImageSaturationFilter *saturationFilter = [GPUImageSaturationFilter new];
[brightnessFilter setBrightness:brightnessValue];
[contrastFilter setContrast:contrastValue];
[saturationFilter setSaturation:saturationValue];
[stillImageSource addTarget:brightnessFilter];
[brightnessFilter addTarget:contrastFilter];
[contrastFilter addTarget:saturationFilter];
[brightnessFilter useNextFrameForImageCapture];
[stillImageSource processImage];
UIImage *currentFilteredVideoFrame = [brightnessFilter imageFromCurrentFramebuffer];
imageView.image = currentFilteredVideoFrame;
}
- (IBAction)contrastSlider:(UISlider *)sender
{
contrastValue = sender.value;
UIImage *inputImage = [UIImage imageNamed:@"2.jpg"];
GPUImagePicture *stillImageSource = [[GPUImagePicture alloc] initWithImage:inputImage];
GPUImageBrightnessFilter *brightnessFilter = [GPUImageBrightnessFilter new];
GPUImageContrastFilter *contrastFilter = [GPUImageContrastFilter new];
GPUImageSaturationFilter *saturationFilter = [GPUImageSaturationFilter new];
[brightnessFilter setBrightness:brightnessValue];
[contrastFilter setContrast:contrastValue];
[saturationFilter setSaturation:saturationValue];
[stillImageSource addTarget:brightnessFilter];
[brightnessFilter addTarget:contrastFilter];
[contrastFilter addTarget:saturationFilter];
[brightnessFilter useNextFrameForImageCapture];
[stillImageSource processImage];
UIImage *currentFilteredVideoFrame = [brightnessFilter imageFromCurrentFramebuffer];
imageView.image = currentFilteredVideoFrame;
}
- (IBAction)saturationSlider:(UISlider *)sender
{
saturationValue = sender.value;
UIImage *inputImage = [UIImage imageNamed:@"2.jpg"];
GPUImagePicture *stillImageSource = [[GPUImagePicture alloc] initWithImage:inputImage];
GPUImageBrightnessFilter *brightnessFilter = [GPUImageBrightnessFilter new];
GPUImageContrastFilter *contrastFilter = [GPUImageContrastFilter new];
GPUImageSaturationFilter *saturationFilter = [GPUImageSaturationFilter new];
[brightnessFilter setBrightness:brightnessValue];
[contrastFilter setContrast:contrastValue];
[saturationFilter setSaturation:saturationValue];
[stillImageSource addTarget:brightnessFilter];
[brightnessFilter addTarget:contrastFilter];
[contrastFilter addTarget:saturationFilter];
[brightnessFilter useNextFrameForImageCapture];
[stillImageSource processImage];
UIImage *currentFilteredVideoFrame = [brightnessFilter imageFromCurrentFramebuffer];
imageView.image = currentFilteredVideoFrame;
}
现在我决定用更简单的方式实现整个事情,但我仍然一无所获:
UIImage *inputImage = [UIImage imageNamed:@"2.jpg"];
GPUImagePicture *stillImageSource = [[GPUImagePicture alloc] initWithImage:inputImage];
//Set Brightness to 60
GPUImageBrightnessFilter *brightnessFilter = [GPUImageBrightnessFilter new];
[brightnessFilter setBrightness:60.0];
//Set Contrast to 12
GPUImageContrastFilter *contrastFilter = [GPUImageContrastFilter new];
[contrastFilter setContrast:12];
[brightnessFilter addTarget:contrastFilter];
[stillImageSource addTarget:contrastFilter];
[contrastFilter useNextFrameForImageCapture];
[stillImageSource processImage];
UIImage *outputImage1 = [contrastFilter imageFromCurrentFramebuffer];
imageView.image = outputImage1;
答案 0 :(得分:1)
您不需要为每个滑块更改使用相同的添加目标代码。您应该只更改所需过滤器的值并在代码中的其他位置设置所有过滤器;你最终将加载图像的位置。这将使您的代码更容易维护,并使这个问题缩短。
现在到了实际问题。查看GPUImage github issue。您已按以下顺序嵌套过滤器:
静止图像>亮度>对比度>饱和度
然后你从Brightness滤镜中获取图像。你需要通过饱和度过滤器。所以,您的连锁店需要像:
静止图像>亮度>对比度>饱和度>输出图像