Skip to content

Mat initWithUIImage: does not work (Objective-C / Swift) #17532

@treastrain

Description

@treastrain
System information (version)
  • OpenCV => commit Objc binding #17165
  • Operating System / Platform => iOS Swift
  • Compiler => Xcode Version 11.5 (11E608c)
$ swift --version 
Apple Swift version 5.2.4 (swiftlang-1103.0.32.9 clang-1103.0.32.53)
Target: x86_64-apple-darwin19.5.0

$ cmake --version
cmake version 3.17.3

$ python --version
Python 2.7.18

$ xcode-select --version
xcode-select version 2373.
Detailed description

OpenCV's Objective-C / Swift support is very awesome!
Thanks to @komakai @alalek @vpisarev .

Now, I'm trying to create a Mat from a UIImage in an iOS App project by Swift.

let srcMat = Mat(uiImage: srcImage)

Then, the -[Mat initWithUIImage:]: unrecognized selector sent to instance error occurs in this line.

Steps to reproduce
  1. $ git clone https://github.com/opencv/opencv.git
  2. Run $ opencv/platforms/ios/build_framework.py <outputdir>
  3. Create an iOS project, add opencv2.framework & libc++.tbd in the project
  4. Write the code ViewController.swift
import UIKit
import opencv2

class ViewController: UIViewController {
    
    @IBOutlet weak var srcImageView: UIImageView!
    @IBOutlet weak var dstImageView: UIImageView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        let image = UIImage(named: "サンプル画像")!
        self.srcImageView.image = image
        self.dstImageView.image = self.convertColor(source: image)
    }
    
    func convertColor(source srcImage: UIImage) -> UIImage {
        let srcMat = Mat(uiImage: srcImage) // ERROR!
        let dstMat = Mat()
        Imgproc.cvtColor(src: srcMat, dst: dstMat, code: .COLOR_RGB2GRAY)
        return dstMat.toUIImage()
    }
}
-[Mat initWithUIImage:]: unrecognized selector sent to instance 0x280554620
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Mat initWithUIImage:]: unrecognized selector sent to instance 0x280554620'
*** First throw call stack:
(0x1b03e4300 // ... // 0x1b01d52dc)
libc++abi.dylib: terminating with uncaught exception of type NSException
Issue submission checklist
  • I report the issue, it's not a question
  • I checked the problem with documentation, FAQ, open issues,
    answers.opencv.org, Stack Overflow, etc and have not found solution
  • I updated to latest OpenCV version and the issue is still there
  • There is reproducer code and related data files: videos, images, onnx, etc

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions