7

I am using CIFilter for editing video and images with Swift 3.0.

Its working fine, for whole image or video.But I want to apply filter to only selected area. I found, It can be done through ROI But, all the documentation i found, is related to custom filters only.

Is there any way to use existing CIFIlters with ROI function? i.e. applying filter on selected area only?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
PlusInfosys
  • 3,416
  • 1
  • 19
  • 33
  • Not that I'm aware of. ROI is meant for either `CIWarpKernel` or `CIKernel`, both of which need to work (a) pixel-by-pixel while (b) being aware of surrounding pixels. You *could* try to create a subview/subarea of the full image/video (maybe using `CICrop` or CIPerspectiveCorrect` and then (a) apply your filter to it and then (b) combine the two CIImages into one. (It *might* work but also may be too poor performance-wise.) –  May 06 '18 at 17:29
  • I want to do this for video. Applying filter to cropped image and combining into one is obviously poor performance wise. Cant still use that for image, but defiantly can't in video. – PlusInfosys May 07 '18 at 12:26
  • The two most credible/official resources I know would be Apple and Simon Gladman, the author of CoreImage for Swift. I just checked both, and it appears that `apply(extent:roiCallback:arguments:)` is an instance method of `CIKernel`. https://stackoverflow.com/questions/50174495/cifilter-applying-to-selected-area-using-swift-roi?noredirect=1#comment87445127_50174495 The link in your question is also related to using GLSL code inside a CIKernel. Depending on what type of effect you are going for, writing such a thing *may* be somewhat trivial. –  May 07 '18 at 23:31
  • Uh, ignore the link - I really screwed up with the copy/paste. :-) Here's the proper link: https://developer.apple.com/documentation/coreimage/cikernel/1438243-apply Anyways, good luck! –  May 07 '18 at 23:41
  • I did check that link. All i could understand is, I need to subclass CIFilter. i.e customise CIFilter. Where as I wan to use existing filters. – PlusInfosys May 08 '18 at 10:32
  • Last attempt at helping. I think you are trying something that cannot be done - applying an existing `CIFilter` to a "region of interest". How about reversing things? How are you grabbing the video? If you *can't* use CI for a ROI, how about the opposite? Let's say you are using `AVFoundation`. Can you subclass (if needed) **it** to pass the "ROI" that you want "filtered" in it's entirety? Once again, good luck. –  May 08 '18 at 15:29
  • I am not capturing videos from application, but allowing users to select it from photo library. So this too will not work. Thanks @dfd – PlusInfosys May 09 '18 at 06:26

1 Answers1

-2

You could;

  1. Convert your UIImage into CIImage.
  2. Then you would be able to extract the region you want your filter to be applied, after applying the filter you will have the CIImage as output.
  3. Merge your source CIImage and the filter-applied-CIImage from step 2.
  4. Finally convert your CIImage from step 3 to a UIImage.

If done properly all this can be nicely abstracted inside a function.

  • I have already specified, I want to Apply ROI on video. Process you have explained will create performance issues. – PlusInfosys May 15 '18 at 06:47