【问题标题】:GPUImage Chroma Key filterGPUImage 色度键过滤器
【发布时间】:2012-11-19 18:10:56
【问题描述】:

我正在尝试使用 GPUImage 框架的色键过滤器。我遵循了 Filtershowcase 示例,但显然我错过了一些东西,因为它只显示视频,但没有绿屏抠像效果。这是我对摄像机/过滤器的初始化:

camera = [[GPUImageStillCamera alloc] initWithSessionPreset:AVCaptureSessionPreset640x480
                                             cameraPosition:AVCaptureDevicePositionBack];
camera.outputImageOrientation = UIInterfaceOrientationLandscapeLeft;
camera.horizontallyMirrorFrontFacingCamera = NO;
camera.horizontallyMirrorRearFacingCamera = NO;


chromaKeyFilter = [[GPUImageChromaKeyFilter alloc] init];
chromaKeyFilter.thresholdSensitivity = sliderThres.value;

UIImage *inputImage = [UIImage imageNamed:@"WID-small.jpg"];


GPUImagePicture *sourcePicture = [[GPUImagePicture alloc] initWithImage:inputImage
                                                    smoothlyScaleOutput:YES];


[camera addTarget:chromaKeyFilter];
[sourcePicture addTarget:chromaKeyFilter];
[sourcePicture processImage];

[chromaKeyFilter addTarget:viewCamera];


[camera startCameraCapture];

所以相机和 sourcePicture 都输入过滤器,过滤器比进入 viewCamera。但现在,我只看到纯视频,没有色键效果。 (我有一个改变阈值的滑块)。

更新与 GPUImageChromaKeyBlendFilter

camera = [[GPUImageVideoCamera alloc] initWithSessionPreset:AVCaptureSessionPreset640x480 cameraPosition:AVCaptureDevicePositionBack];
camera.outputImageOrientation = UIInterfaceOrientationLandscapeLeft;
chromaKeyFilter = [[GPUImageChromaKeyBlendFilter alloc] init];


UIImage *inputImage;
inputImage = [UIImage imageNamed:@"WID-small.jpg"];
GPUImagePicture *sourcePicture = [[GPUImagePicture alloc] initWithImage:inputImage smoothlyScaleOutput:YES];


[camera addTarget:chromaKeyFilter];
[sourcePicture processImage];
[sourcePicture addTarget:chromaKeyFilter];
[chromaKeyFilter addTarget:viewCamera];

[camera startCameraCapture];

使用此滤镜,它只是将其替换为黑色,而不是实际图像。

【问题讨论】:

    标签: objective-c ios cocoa-touch gpuimage


    【解决方案1】:

    Found this gem after a couple of days of trying....。显然你应该坚持(强参考)GPUImagePicture。我的印象是过滤器链内部会有很强的参考,但事实并非如此。将其保留为实例 var 解决了这个问题。

    【讨论】:

      【解决方案2】:

      我认为您需要 GPUImageChromaKeyBlendFilter,而不是 GPUImageChromaKeyFilter。

      混合采用两个源图像,并用第二个图像中的像素替换与第一个图像中的键控颜色匹配的像素。常规色度键控过滤器只是将与目标颜色匹配的像素的 Alpha 通道减少到 0.0,它不会混合到另一个图像中。

      【讨论】:

      • 谢谢,我做到了。但现在它用黑色代替了绿色,而不是图像。我已经更新了代码。我认为我在管道上做错了什么......
      • @0xSina - 您确定 WID-small.jpg 已作为资源正确复制到您的应用程序包中吗?如果是这样,请在添加所有目标后尝试移动 [sourcePicture processImage]; 行。
      • 是的,我对 UIImage 进行 NSLog 记录,它会打印出“”。我尝试将它放在所有目标之后,但它仍然不会显示图像。仅黑色用于键控区域。这很奇怪,因为我正在做你在 FilterShowcase 中所做的事情,它在那里工作,但在我的项目中却没有......
      • 从您在 github 上的评论中找出问题所在(尝试两天后)。谢谢!
      • @0xSina - 为什么要这样做?作为输入,保持它是应用程序的责任。如果框架保持对输入的强引用(或保留),那将导致保留周期并导致许多内存泄漏。下游的东西由上游保留,但数据源需要由应用程序保留,以便它们继续存在。
      【解决方案3】:

      与最初的问题类似,我想在自定义视图层次结构之上放置一个绿屏视频,包括。直播视频。事实证明,使用标准 GPUImage ChromaKey 过滤器是不可能的。它不是 alpha 混合,而是将绿色像素与背景像素混合。例如,红色背景变成黄色,蓝色变成青色。

      让它工作的方法包括两个步骤:

      1) 确保过滤视图具有透明背景:

      filterView.backgroundColor=[UIColor clearColor];
      

      2) 修改GPUImageChromaKeyFilter.m

      old: gl_FragColor = vec4(textureColor.rgb, textureColor.a * blendValue);
      
      new: gl_FragColor = vec4(textureColor.rgb * blendValue, 1.0 * blendValue);
      

      现在视频中的所有键控(例如绿色)像素都变得透明,并显示过滤器视图下方的任何内容,包括。 (实时)视频。

      【讨论】:

      【解决方案4】:

      你是否为滑块设置了目标和选择器?

      这对我有用

      [(GPUImageChromaKeyFilter *)filter setThresholdSensitivity:[(UISlider *)sender value]];
              [image setImage:[filter imageByFilteringImage:sourceImage]];
      

      希望对您有所帮助。

      【讨论】:

      • 是的,但是设置阈值无论如何都与它没有任何关系,即使我没有默认为 0.4
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多