有什么方法可以检测 Swift 中的形状轮廓而不必分支到 OpenCV?

is there any way to detect shape contours in Swift without having to branch out to OpenCV?

显然,我们可以使用 CoreImage 将图像转换为灰色、去噪和模糊,但是当涉及到 Canny 边缘检测、轮廓检测和绘制形状的轮廓时,这似乎仅使用是不可能的swift 个特征。我是对的还是我在这里遗漏了什么?

2020 年 6 月,Apple 在 iOS14 的视觉框架中添加了新的轮廓检测功能。(目前处于测试阶段),因此您现在可以使用 Swift(w/o OpenCV ) 以检测轮廓。

您可以在 Apple 的文档中找到更多详细信息:https://developer.apple.com/documentation/vision/vndetectcontoursrequest

这个 WWDC2020 视频也很重要:https://developer.apple.com/videos/play/wwdc2020/10673/

现在您可以使用 VNDetectContoursRequest 通过 SwiftUI 检测 iOS 14 ,

func detectVisionContours(){
    
    let context = CIContext()
    if let sourceImage = UIImage.init(named: "coin")
    {
        var inputImage = CIImage.init(cgImage: sourceImage.cgImage!)
        
        let contourRequest = VNDetectContoursRequest.init()
        contourRequest.revision = VNDetectContourRequestRevision1
        contourRequest.contrastAdjustment = 1.0
        contourRequest.detectDarkOnLight = true            
        contourRequest.maximumImageDimension = 512


        let requestHandler = VNImageRequestHandler.init(ciImage: inputImage, options: [:])

        try! requestHandler.perform([contourRequest])
        let contoursObservation = contourRequest.results?.first as! VNContoursObservation
        
        self.points  = String(contoursObservation.contourCount)
        self.contouredImage = drawContours(contoursObservation: contoursObservation, sourceImage: sourceImage.cgImage!)

    } else {
        self.points = "Could not load image"
    }

}

要绘制等高线,请执行此操作,

public func drawContours(contoursObservation: VNContoursObservation, sourceImage: CGImage) -> UIImage {
    let size = CGSize(width: sourceImage.width, height: sourceImage.height)
    let renderer = UIGraphicsImageRenderer(size: size)
    
    let renderedImage = renderer.image { (context) in
    let renderingContext = context.cgContext

    let flipVertical = CGAffineTransform(a: 1, b: 0, c: 0, d: -1, tx: 0, ty: size.height)
    renderingContext.concatenate(flipVertical)

    renderingContext.draw(sourceImage, in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
    
    renderingContext.scaleBy(x: size.width, y: size.height)
    renderingContext.setLineWidth(5.0 / CGFloat(size.width))
    let redUIColor = UIColor.red
    renderingContext.setStrokeColor(redUIColor.cgColor)
    renderingContext.addPath(contoursObservation.normalizedPath)
    renderingContext.strokePath()
    }
    
    return renderedImage
}

参考:https://heartbeat.fritz.ai/new-in-ios-14-vision-contour-detection-68fd5849816e