Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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

kotetu (kotetuco)

September 06, 2019
Tweet

More Decks by kotetu (kotetuco)

Other Decks in Programming

Transcript

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

    ೝࣝ ॲཧ w ॲཧ݁Ռͷදࣔ Ωϟϓνϟͨ͠Χϝϥө૾ΛϑϨʔϜ͝ͱʹऔಘ J04%$ 
  2. ॲཧͷྲྀΕ Ұൠతͳέʔε w Χϝϥ͔Βը૾Λऔಘ w ը૾ ೝࣝ ॲཧ΁ͷೖྗͷͨΊͷલॲཧ w ը૾

    ೝࣝ ॲཧ w ॲཧ݁Ռͷදࣔ ը૾ܗࣜͷม׵΍ϦαΠζɾάϨʔεέʔϧԽ ը૾ೝࣝͷ৔߹ ͳͲ J04%$ 
  3. ॲཧͷྲྀΕ Ұൠతͳέʔε w Χϝϥ͔Βը૾Λऔಘ w ը૾ ೝࣝ ॲཧ΁ͷೖྗͷͨΊͷલॲཧ w ը૾

    ೝࣝ ॲཧ w ॲཧ݁Ռͷදࣔ ΅͔͠΍৭ຯͷมߋɺಛ௃఺ݕग़ͳͲ J04%$ 
  4. ॲཧͷྲྀΕ Ұൠతͳέʔε w Χϝϥ͔Βը૾Λऔಘ w ը૾ ೝࣝ ॲཧ΁ͷೖྗͷͨΊͷલॲཧ w ը૾

    ೝࣝ ॲཧ w ॲཧ݁Ռͷදࣔ ॲཧͨ͠ޙͷը૾΍ೝࣝ݁ՌΛը໘ʹදࣔ J04%$ 
  5. ը૾Λड͚औΔ class Capture: AVCaptureVideoDataOutputSampleBufferDelegate { func captureOutput(_ output: AVCaptureOutput, didOutput

    sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) { ... } } J04%$ 
  6. ը૾Λड͚औΔ class Capture: AVCaptureVideoDataOutputSampleBufferDelegate { func captureOutput(_ output: AVCaptureOutput, didOutput

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

    AVCaptureConnection) { let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) let ciImage = CIImage(cvImageBuffer: imageBuffer!) } J04%$ 
  8. $**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. $**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. 6**NBHF΁ม׵ let context = CIContext() let cgImage = context.createCGImage(ciImage, from:

    ciImage.extent) let uiImage = UIImage(cgImage: cgImage!) J04%$ 
  11. 6**NBHF΁ม׵ let context = CIContext() let cgImage = context.createCGImage(ciImage, from:

    ciImage.extent) let uiImage = UIImage(cgImage: cgImage!) έʔεʹ΋ΑΔ͕ $(*NBHFΛܦ༝͢Δέʔε ͕ଟ͍ J04%$ 
  12. $**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. $**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. $**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. $*-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. ΞϑΟϯม׵ extension CIImage { func resize(to scale: CGFloat) -> CIImage?

    { let matrix = CGAffineTransform(scaleX: scale, y: scale) return self.transformed(by: matrix) } } J04%$ 
  17. ϦαΠζॲཧ ʙ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. ϦαΠζॲཧ 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. ϦαΠζॲཧ 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. ܭଌ݁Ռ 6**NBHF ํࣜ ฏۉॲཧ࣌ؒ 6*(SBQIJDT#FHJO*NBHF$POUFYU ʙJ04  6*(SBQIJDT*NBHF3FOEFSFS J04ʙ 

    શମͷॲཧ͔࣌ؒΒೝࣝॲཧΛࠩ͠Ҿ͍ͨ࣌ؒ ୯Ґ͸NT J04%$ 
  21. ܭଌ݁Ռ $**NBHF ํࣜ ฏۉॲཧ࣌ؒ ΞϑΟϯม׵  $*-BOD[PT4DBMF5SBOTGPSN  $*#JDVCJD4DBMF5SBOTGPSN 

    શମͷॲཧ͔࣌ؒΒೝࣝॲཧΛࠩ͠Ҿ͍ͨ࣌ؒ ୯Ґ͸NT J04%$ 
  22. ܭଌ݁Ռ 6**NBHF ํࣜ ฏۉॲཧ࣌ؒ 6*(SBQIJDT#FHJO*NBHF$POUFYU ʙJ04  6*(SBQIJDT*NBHF3FOEFSFS J04ʙ 

    શମͷॲཧ͔࣌ؒΒೝࣝॲཧΛࠩ͠Ҿ͍ͨ࣌ؒ ୯Ґ͸NT J04%$ 
  23. ܭଌ݁Ռ $**NBHF ํࣜ ฏۉॲཧ࣌ؒ ΞϑΟϯม׵  $*-BOD[PT4DBMF5SBOTGPSN  $*#JDVCJD4DBMF5SBOTGPSN 

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