iOS Swift检测方块

时间:2020-09-17 22:55:37

标签: swift

我正在使用Swift检测图像上的正方形,但似乎无法检测到它们。似乎检测矩形有时不能确定这是否正确。我是Swift和图像检测的新手,因此,如果我还有其他方法可以检测正方形,我将不胜感激将其指向正确的方向。

根据我在搜索中发现的信息,发现了有关检测正方形/矩形和透视图的问题。不知道这是问题还是我缺乏对Swift和图像检测的了解。

Test image

lazy var rectangleDetectionRequest: VNDetectRectanglesRequest = {
        let rectDetectRequest = VNDetectRectanglesRequest(completionHandler: self.handleDetectedRectangles)
        // Customize & configure the request to detect only certain rectangles.
        rectDetectRequest.maximumObservations = 8 // Vision currently supports up to 16.
        rectDetectRequest.minimumConfidence = 0.6 // Be confident.
        rectDetectRequest.minimumAspectRatio = 0.3 // height / width
        return rectDetectRequest
    }()

fileprivate func handleDetectedRectangles(request: VNRequest?, error: Error?) {
        if let nsError = error as NSError? {
            self.presentAlert("Rectangle Detection Error", error: nsError)
            return
        }
        // Since handlers are executing on a background thread, explicitly send draw calls to the main thread.
        DispatchQueue.main.async {
            guard let drawLayer = self.pathLayer,
                let results = request?.results as? [VNRectangleObservation] else {
                    return
            }
            self.draw(rectangles: results, onImageWithBounds: drawLayer.bounds)
            drawLayer.setNeedsDisplay()
        }
    }

我还将minimumAspectRatio更改为1.0,从我发现的信息中将得到一个正方形,它仍然没有给出预期的结果。

0 个答案:

没有答案
相关问题