html中rectangle坐标,从CIRectangleFeature坐标转换到查看坐标

如果你的路径仅关中Y尺寸:

我认为他们没有完全将CIDetector移植到UIKit。该特征的坐标位于Cocoa坐标系中。只要做container.height - point.y将会转换它。

我也给你的结构正确的名称。其余的东西在那里,我曾经弄清楚发生了什么。可能对你有用。

代码:

func analyzeImage(image: UIImage) -> [Quadrilateral]

{

guard let ciImage = CIImage(image: image)

else { return [] }

let flip = true // set to false to prevent flipping the coordinates

let context = CIContext(options: nil)

let detector = CIDetector(ofType: CIDetectorTypeRectangle, context: context, options: [CIDetectorAccuracy:CIDetectorAccuracyHigh])

let features = detector.featuresInImage(ciImage)

UIGraphicsBeginImageContext(ciImage.extent.size)

let currentContext = UIGraphicsGetCurrentContext()

var frames: [Quadrilateral] = []

for feature in features as! [CIRectangleFeature]

{

var topLeft = CGContextConvertPointToUserSpace(currentContext, feature.topLeft)

var topRight = CGContextConvertPointToUserSpace(currentContext, feature.topRight)

var bottomRight = CGContextConvertPointToUserSpace(currentContext, feature.bottomRight)

var bottomLeft = CGContextConvertPointToUserSpace(currentContext, feature.bottomLeft)

if flip {

topLeft = CGPoint(x: topLeft.x, y: image.size.height - topLeft.y)

topRight = CGPoint(x: topRight.x, y: image.size.height - topRight.y)

bottomLeft = CGPoint(x: bottomLeft.x, y: image.size.height - bottomLeft.y)

bottomRight = CGPoint(x: bottomRight.x, y: image.size.height - bottomRight.y)

}

let frame = Quadrilateral(topLeft: topLeft, topRight: topRight, bottomLeft: bottomLeft, bottomRight: bottomRight)

frames.append(frame)

}

UIGraphicsEndImageContext()

return frames

}

四边形结构:

struct Quadrilateral {

var topLeft : CGPoint = CGPointZero

var topRight : CGPoint = CGPointZero

var bottomLeft : CGPoint = CGPointZero

var bottomRight : CGPoint = CGPointZero

var path : UIBezierPath {

get {

let tempPath = UIBezierPath()

tempPath.moveToPoint(topLeft)

tempPath.addLineToPoint(topRight)

tempPath.addLineToPoint(bottomRight)

tempPath.addLineToPoint(bottomLeft)

tempPath.addLineToPoint(topLeft)

return tempPath

}

}

init(topLeft topLeft_I: CGPoint, topRight topRight_I: CGPoint, bottomLeft bottomLeft_I: CGPoint, bottomRight bottomRight_I: CGPoint) {

topLeft = topLeft_I

topRight = topRight_I

bottomLeft = bottomLeft_I

bottomRight = bottomRight_I

}

var frame : CGRect {

get {

let highestPoint = max(topLeft.y, topRight.y, bottomLeft.y, bottomRight.y)

let lowestPoint = min(topLeft.y, topRight.y, bottomLeft.y, bottomRight.y)

let farthestPoint = max(topLeft.x, topRight.x, bottomLeft.x, bottomRight.x)

let closestPoint = min(topLeft.x, topRight.x, bottomLeft.x, bottomRight.x)

// you might want to set origin to (0,0)

let origin = CGPoint(x: closestPoint, y: lowestPoint)

let size = CGSize(width: farthestPoint, height: highestPoint)

return CGRect(origin: origin, size: size)

}

}

var size : CGSize {

get {

return frame.size

}

}

var origin : CGPoint {

get {

return frame.origin

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值