gpt4 book ai didi

ios - 如何通过GPUImage调整图像的亮度和对比度?

转载 作者:行者123 更新时间:2023-12-01 16:47:08 24 4
gpt4 key购买 nike

我编写了一种使用亮度因子和对比度因子过滤图像的方法,如下所示:

- (UIImage*)image:(UIImage*)image withBrightness:(float)brightness contrast:(float)contrast{
GPUImagePicture *imagePicture = [[GPUImagePicture alloc] initWithImage:image];
GPUImageBrightnessFilter *brightnessFilter = [[GPUImageBrightnessFilter alloc] init];
GPUImageContrastFilter *contrastFilter = [[GPUImageContrastFilter alloc] init];

[imagePicture addTarget:brightnessFilter];
[brightnessFilter addTarget:contrastFilter];

brightnessFilter.brightness = brightness; // set brightness
contrastFilter.contrast = contrast; // set contrast

[imagePicture processImage];
UIImage *filteredImage = [contrastFilter imageFromCurrentlyProcessedOutput];
return filteredImage;
}

调用此实现将引发错误: <Error>: CGContextDrawImage: invalid context 0x0

如何使用GPUImage正确实现亮度和对比度调整?

最佳答案

您可能应该创建一个组过滤器,请检查此示例代码

    GPUImageFilterGroup *groupFilter = [[GPUImageFilterGroup alloc]init];
[groupFilter addTarget:edit.currentFilter];

//create a brightness filter
GPUImageBrightnessFilter *brightnessFilter = [[GPUImageBrightnessFilter alloc]init];
[brightnessFilter setBrightness:brightnessValue];

//create a contrast filter
GPUImageContrastFilter *contrastFilter = [[GPUImageContrastFilter alloc]init];
[contrastFilter setContrast:contrastValue];

//create a exposure filter
GPUImageExposureFilter *exposureFilter =[[GPUImageExposureFilter alloc]init];
[exposureFilter setExposure:exposureValue];

//add brightness,contrast,exposure filters to group filter
[groupFilter addFilter:brightnessFilter];
[groupFilter addFilter:contrastFilter];
[groupFilter addFilter:exposureFilter];


[edit.currentFilter addTarget:brightnessFilter];
[brightnessFilter addTarget: contrastFilter];
[contrastFilter addTarget: exposureFilter];
//setting initial and terminal filters
[(GPUImageFilterGroup *) groupFilter setInitialFilters:[NSArray arrayWithObject:edit.currentFilter]];
[(GPUImageFilterGroup *) groupFilter setTerminalFilter:exposureFilter];


//apply it to the GPUImagePicture

关于ios - 如何通过GPUImage调整图像的亮度和对比度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19134112/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com