无法获得使用 Swift 显示的 OpenCV 的 drawContours 绘制的线条

问题描述

我在 Swift 中使用了 openCV,并且能够使用 findContours()(我设置了一个打印语句并知道它正在工作)。

我也成功得到了黑白二值图像。

尽管如此,我还是不知道如何让 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,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),thickness: 5)
    }
}

结果如下:

Result

这就是我想要实现的----物体周围的红线。

What I want to achieve -- the red line

我真的不知道问题出在哪里。 任何人,请帮助。 提前致谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)