gpt4 book ai didi

ios - 将 VNRectangleObservation 点转换为其他坐标系

转载 作者:搜寻专家 更新时间:2023-10-30 23:07:54 46 4
gpt4 key购买 nike

我需要转换 VNRectangleObservation 收到的 CGPoints (bottomLeft,bottomRight, topLeft, topRight) 到另一个坐标系(例如屏幕上 View 的坐标)。

我定义一个请求:

    // Rectangle Request
let rectangleDetectionRequest = VNDetectRectanglesRequest(completionHandler: handleRectangles)
rectangleDetectionRequest.minimumSize = 0.5
rectangleDetectionRequest.maximumObservations = 1

我在委托(delegate)调用中从相机获取 sampleBuffer,并执行检测请求:

func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {

guard let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else {return}
var requestOptions:[VNImageOption:Any] = [:]
if let cameraIntrinsicData = CMGetAttachment(sampleBuffer, kCMSampleBufferAttachmentKey_CameraIntrinsicMatrix, nil) {
requestOptions = [.cameraIntrinsics:cameraIntrinsicData]
}
let imageRequestHandler = VNImageRequestHandler(cvPixelBuffer: pixelBuffer, orientation: CGImagePropertyOrientation(rawValue:6)!, options: requestOptions)
do {
try imageRequestHandler.perform(self.requests)
} catch {
print(error)
}

}

稍后在 completionHandler 中我收到结果:

func handleRectangles (request:VNRequest, error:Error?) {

guard let results = request.results as? [VNRectangleObservation] else { return }

let flipTransform = CGAffineTransform(scaleX: 1, y: -1).translatedBy(x: 0, y: -self.previewView.frame.height)
let scaleTransform = CGAffineTransform.identity.scaledBy(x: self.previewView.frame.width, y: self.previewView.frame.height)

for rectangle in results {
let rectangleBounds = rectangle.boundingBox.applying(scaleTransform).applying(flipTransform)
// convertedTopLeft = conversion(rectangle.topLeft)
// convertedTopRight = conversion(rectangle.topRight)
// convertedBottomLeft = conversion(rectangle.bottomLeft)
// convertedBottomRight = conversion(rectangle.bottomRight)
}
}

这适用于 CGRect 的 boundingBox,但我需要将 CGPoints 转换为另一个 View 的坐标系。问题是我不知道如何从 sampleBuffer: CMSampleBuffer 的坐标系到 previewView 坐标系的转换。

谢谢!

最佳答案

这只是将转换应用于 CGPoint 本身的问题,其中大小是我需要转置四个点的目标 View 的 CGSize。

    let transform = CGAffineTransform.identity
.scaledBy(x: 1, y: -1)
.translatedBy(x: 0, y: -size.height)
.scaledBy(x: size.width, y: size.height)

let convertedTopLeft = rectangle.topLeft.applying(transform)
let convertedTopRight = rectangle.topRight.applying(transform)
let convertedBottomLeft = rectangle.bottomLeft.applying(transform)
let convertedBottomRight = rectangle.bottomRight.applying(transform)

关于ios - 将 VNRectangleObservation 点转换为其他坐标系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47928691/

46 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com