Swift で openCV が動作するようになり、findContours() を使用できるようになりました (print ステートメントを設定し、動作していることを確認しました)。
また、白黒のバイナリ イメージも正常に取得できました。
それにもかかわらず、 drawContour() を機能させる方法がわかりませんでした。これが私のコードです:
import UIKit
import opencv2
//import PythonKit
class VC_Silhouette: UIViewController {
@IBOutlet weak var silhouetteFront: UIImageView!
@IBOutlet weak var silhouetteWithContours: UIImageView!
override func loadView() {
// MyViewController.xib からインスタンスを生成し root view に設定する
let nib = UINib(nibName: "VC_Silhouette", bundle: .main)
self.view = nib.instantiate(withOwner: self).first as? UIView
}
override func viewDidLoad() {
super.viewDidLoad()
let image = UIImage(named: "kobe")!
let imgray = Mat()
let hierarchy = Mat()
var contours: [[Point]] = [[]]
self.silhouetteFront.image = image
Imgproc.cvtColor(src: Mat(uiImage: image), dst: imgray, code: ColorConversionCodes.COLOR_BGR2GRAY)
Imgproc.threshold(src: imgray, dst: imgray, thresh: 0, maxval: 255, type: ThresholdTypes.THRESH_BINARY)
self.silhouetteWithContours.image = imgray.toUIImage()
Imgproc.findContours(image: imgray, contours: &contours, hierarchy: hierarchy, mode: RetrievalModes.RETR_EXTERNAL, method: ContourApproximationModes.CHAIN_APPROX_NONE)
Imgproc.drawContours(image: Mat(uiImage: image), contours: contours, contourIdx: -1, color: Scalar(255, 0, 0), thickness: 5)
}
}
結果は次のとおりです。
これが私が達成したいことです----オブジェクトを囲む赤い線。
どこに問題があるのか 本当にわかりません。誰でも、助けてください。前もって感謝します。