UIImage and CIImage for Image Processing

UIImage and CIImage for Image Processing

画像処理におけるUIImageとCGImageとCIImageの効果的な使い分け
iOSDC 2019 発表資料
https://fortee.jp/iosdc-japan-2019/proposal/3c30c4b4-a647-4198-8e8c-e8100293ee93

Ee6254efe6c4bc4c08967c11d4939245?s=128

kotetu (kotetuco)

September 06, 2019
Tweet

Transcript

  1. 20.

    ॲཧͷྲྀΕ Ұൠతͳέʔε w Χϝϥ͔Βը૾Λऔಘ w ը૾ ೝࣝ ॲཧ΁ͷೖྗͷͨΊͷલॲཧ w ը૾

    ೝࣝ ॲཧ w ॲཧ݁Ռͷදࣔ Ωϟϓνϟͨ͠Χϝϥө૾ΛϑϨʔϜ͝ͱʹऔಘ J04%$ 
  2. 21.

    ॲཧͷྲྀΕ Ұൠతͳέʔε w Χϝϥ͔Βը૾Λऔಘ w ը૾ ೝࣝ ॲཧ΁ͷೖྗͷͨΊͷલॲཧ w ը૾

    ೝࣝ ॲཧ w ॲཧ݁Ռͷදࣔ ը૾ܗࣜͷม׵΍ϦαΠζɾάϨʔεέʔϧԽ ը૾ೝࣝͷ৔߹ ͳͲ J04%$ 
  3. 22.

    ॲཧͷྲྀΕ Ұൠతͳέʔε w Χϝϥ͔Βը૾Λऔಘ w ը૾ ೝࣝ ॲཧ΁ͷೖྗͷͨΊͷલॲཧ w ը૾

    ೝࣝ ॲཧ w ॲཧ݁Ռͷදࣔ ΅͔͠΍৭ຯͷมߋɺಛ௃఺ݕग़ͳͲ J04%$ 
  4. 23.

    ॲཧͷྲྀΕ Ұൠతͳέʔε w Χϝϥ͔Βը૾Λऔಘ w ը૾ ೝࣝ ॲཧ΁ͷೖྗͷͨΊͷલॲཧ w ը૾

    ೝࣝ ॲཧ w ॲཧ݁Ռͷදࣔ ॲཧͨ͠ޙͷը૾΍ೝࣝ݁ՌΛը໘ʹදࣔ J04%$ 
  5. 32.

    ը૾Λड͚औΔ class Capture: AVCaptureVideoDataOutputSampleBufferDelegate { func captureOutput(_ output: AVCaptureOutput, didOutput

    sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) { ... } } J04%$ 
  6. 33.

    ը૾Λड͚औΔ class Capture: AVCaptureVideoDataOutputSampleBufferDelegate { func captureOutput(_ output: AVCaptureOutput, didOutput

    sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) { ... } } ը૾σʔλ͸$.4BNQMF#VGGFSͰड͚औΔͷ Ͱผͳը૾Ϋϥε΁ม׵ͯ͋͛͠ͳ͍ͱ͍͚ͳ͍ J04%$ 
  7. 36.

    $**NBHF΁ม׵ func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection:

    AVCaptureConnection) { let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) let ciImage = CIImage(cvImageBuffer: imageBuffer!) } J04%$ 
  8. 37.

    $**NBHF΁ม׵ func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection:

    AVCaptureConnection) { let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) let ciImage = CIImage(cvImageBuffer: imageBuffer!) } $.4BNQMF#V⒎FS͔Β$7*NBHF#VGGFS΁ɹ ม׵ J04%$ 
  9. 38.

    $**NBHF΁ม׵ func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection:

    AVCaptureConnection) { let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) let ciImage = CIImage(cvImageBuffer: imageBuffer!) } $7*NBHF#V⒎FS͔Β$**NBHF΁ม׵ J04%$ 
  10. 43.

    6**NBHF΁ม׵ let context = CIContext() let cgImage = context.createCGImage(ciImage, from:

    ciImage.extent) let uiImage = UIImage(cgImage: cgImage!) J04%$ 
  11. 44.

    6**NBHF΁ม׵ let context = CIContext() let cgImage = context.createCGImage(ciImage, from:

    ciImage.extent) let uiImage = UIImage(cgImage: cgImage!) έʔεʹ΋ΑΔ͕ $(*NBHFΛܦ༝͢Δέʔε ͕ଟ͍ J04%$ 
  12. 64.

    $**NBHFͷ஗Ԇ࣮ߦ let filter = CIFilter(name: "CIGaussianBlur")! filter.setValue(ciImage, forKey: kCIInputImageKey) filter.setValue(10.0,

    forKey: kCIInputRadiusKey) let filteredImage = filter.outputImage! CIContext().createCGImage(filteredImage, from: filteredImage.extent) J04%$ 
  13. 65.

    $**NBHFͷ஗Ԇ࣮ߦ let filter = CIFilter(name: "CIGaussianBlur")! filter.setValue(ciImage, forKey: kCIInputImageKey) filter.setValue(10.0,

    forKey: kCIInputRadiusKey) let filteredImage = filter.outputImage! CIContext().createCGImage(filteredImage, from: filteredImage.extent) ͜͜Ͱ͸ը૾ॲཧ͸ߦΘΕͳ͍ J04%$ 
  14. 66.

    $**NBHFͷ஗Ԇ࣮ߦ let filter = CIFilter(name: "CIGaussianBlur")! filter.setValue(ciImage, forKey: kCIInputImageKey) filter.setValue(10.0,

    forKey: kCIInputRadiusKey) let filteredImage = filter.outputImage! CIContext().createCGImage(filteredImage, from: filteredImage.extent) ͜͜Ͱը૾ॲཧ͕ߦΘΕΔ J04%$ 
  15. 69.

    $*-BOD[PT4DBMF5SBOTGPSN extension CIImage { func resize(to scale: CGFloat) -> CIImage?

    { guard let filter = CIFilter(name: "CILanczosScaleTransform") else { return nil } filter.setDefaults() filter.setValue(self, forKey: kCIInputImageKey) filter.setValue(scale, forKey: kCIInputScaleKey) return filter.outputImage } } J04%$ 
  16. 70.

    ΞϑΟϯม׵ extension CIImage { func resize(to scale: CGFloat) -> CIImage?

    { let matrix = CGAffineTransform(scaleX: scale, y: scale) return self.transformed(by: matrix) } } J04%$ 
  17. 74.

    ϦαΠζॲཧ ʙJ04 extension UIImage { func resize(to size: CGSize) ->

    UIImage? { UIGraphicsBeginImageContext(size) defer { UIGraphicsEndImageContext() } let newRect = CGRect(origin: .zero, size: size) draw(in: newRect) return UIGraphicsGetImageFromCurrentImageContext() } } J04%$ 
  18. 75.

    ϦαΠζॲཧ J04ʙ extension UIImage { func resize(to size: CGSize) ->

    UIImage { let format = UIGraphicsImageRendererFormat() format.scale = 1 let renderer = UIGraphicsImageRenderer(size: size, format: format) return renderer.image(actions: { context in let newRect = CGRect(origin: .zero, size: size) self.draw(in: newRect) }) } } J04%$ 
  19. 76.

    ϦαΠζॲཧ J04ʙ extension UIImage { func resize(to size: CGSize) ->

    UIImage { let format = UIGraphicsImageRendererFormat() format.scale = 1 let renderer = UIGraphicsImageRenderer(size: size, format: format) return renderer.image(actions: { context in let newRect = CGRect(origin: .zero, size: size) self.draw(in: newRect) }) } } /PO0QUJPOBMʹͳΔʂ J04%$ 
  20. 82.

    ܭଌ݁Ռ 6**NBHF ํࣜ ฏۉॲཧ࣌ؒ 6*(SBQIJDT#FHJO*NBHF$POUFYU ʙJ04  6*(SBQIJDT*NBHF3FOEFSFS J04ʙ 

    શମͷॲཧ͔࣌ؒΒೝࣝॲཧΛࠩ͠Ҿ͍ͨ࣌ؒ ୯Ґ͸NT J04%$ 
  21. 83.

    ܭଌ݁Ռ $**NBHF ํࣜ ฏۉॲཧ࣌ؒ ΞϑΟϯม׵  $*-BOD[PT4DBMF5SBOTGPSN  $*#JDVCJD4DBMF5SBOTGPSN 

    શମͷॲཧ͔࣌ؒΒೝࣝॲཧΛࠩ͠Ҿ͍ͨ࣌ؒ ୯Ґ͸NT J04%$ 
  22. 85.

    ܭଌ݁Ռ 6**NBHF ํࣜ ฏۉॲཧ࣌ؒ 6*(SBQIJDT#FHJO*NBHF$POUFYU ʙJ04  6*(SBQIJDT*NBHF3FOEFSFS J04ʙ 

    શମͷॲཧ͔࣌ؒΒೝࣝॲཧΛࠩ͠Ҿ͍ͨ࣌ؒ ୯Ґ͸NT J04%$ 
  23. 86.

    ܭଌ݁Ռ $**NBHF ํࣜ ฏۉॲཧ࣌ؒ ΞϑΟϯม׵  $*-BOD[PT4DBMF5SBOTGPSN  $*#JDVCJD4DBMF5SBOTGPSN 

    શମͷॲཧ͔࣌ؒΒೝࣝॲཧΛࠩ͠Ҿ͍ͨ࣌ؒ ୯Ґ͸NT J04%$