【问题标题】:Convert real image coordinates to aspect fit?将真实图像坐标转换为宽高比?
【发布时间】:2018-06-28 06:43:24
【问题描述】:

例如:用户选择 1000 * 1000 图像我发送到后端以检测对象,因此我正在获取图像中存在的对象的坐标。

Like this :(
                {
            x = 385;
            y = 249;
        },
                {
            x = 448;
            y = 242;
        },
                {
            x = 454;
            y = 276;
        },
                {
            x = 391;
            y = 284;
        }
    );

我将图像视图设置为宽高比,因此图像的实际大小会有所不同,因此我需要根据宽高比匹配大小更改坐标以适应宽高比匹配大小的图像。

这是我尝试过的代码。

var topLeftXOne = point[0]["x"] as! CGFloat
var topLeftYOne = point[0]["y"] as! CGFloat
var topRightXOne = point[1]["x"] as! CGFloat
var topRightYOne = point[1]["y"] as! CGFloat
var bottomtopRightXOne = point[2]["x"] as! CGFloat
var bottomRightYOne = point[2]["y"] as! CGFloat
                            
 var bottomLeftXOne = point[3]["x"] as! CGFloat
 var bottomLeftYOne = point[3]["y"] as! CGFloat
    
  let pointsView = ALPDView(frame: CGRect.zero)
  let x: CGRect = AVMakeRect(aspectRatio: image.size, insideRect: pointsView.imageView.frame)
    
 let scaleX = image.size.width / x.width
 let scaleY = image.size.height / x.height
                            
 topLeftXOne = topLeftXOne / scaleX
 topLeftYOne = topLeftYOne / scaleY
                            
 topRightXOne = topRightXOne / scaleX
 topRightYOne = topRightYOne / scaleY
                            
 bottomtopRightXOne = bottomtopRightXOne / scaleX
 bottomRightYOne = bottomRightYOne / scaleY
                            
 bottomLeftXOne = bottomLeftXOne / scaleX
 bottomLeftYOne = bottomLeftYOne / scaleY

但我没有得到正确的结果。

这个例如image ,我想更改坐标以适应宽高比图像大小。

【问题讨论】:

    标签: ios swift uiimage uibezierpath cgpoint


    【解决方案1】:

    最后我通过使用 ratio 方法实现了这一点。我取了图像大小的纵横比,并根据比率数学计算。

    让 x: CGRect = AVMakeRect(aspectRatio: image.size, insideRect: pointsView.imageView.frame)

    让 scaleX = image.size.width / x.width

    让 scaleY = image.size.height / x.height

                            var topLeftXOne = point[0]["x"] as! CGFloat
                            var topLeftYOne = point[0]["y"] as! CGFloat
    
                            var topRightXOne = point[1]["x"] as! CGFloat
                            var topRightYOne = point[1]["y"] as! CGFloat
    
                            var bottomtopRightXOne = point[2]["x"] as! CGFloat
                            var bottomRightYOne = point[2]["y"] as! CGFloat
    
                            var bottomLeftXOne = point[3]["x"] as! CGFloat
                            var bottomLeftYOne = point[3]["y"] as! CGFloat
    
                            let pointsView = ALPDView(frame: CGRect.init(x: 0, y: 0, width:self.view.frame.size.width , height: self.view.frame.size.height))
    
                            let additionValueForX = x.origin.x + 20
                            let additionValueForY = x.origin.y - 40
    
                            topLeftXOne = topLeftXOne / scaleX + additionValueForX
                            topLeftYOne = topLeftYOne / scaleY + additionValueForY
    
                            topRightXOne = topRightXOne / scaleX + additionValueForX
                            topRightYOne = topRightYOne / scaleY + additionValueForY
    
                            bottomtopRightXOne = bottomtopRightXOne / scaleX + additionValueForX
                            bottomRightYOne = bottomRightYOne / scaleY + additionValueForY
    
                            bottomLeftXOne = bottomLeftXOne / scaleX + additionValueForX
                            bottomLeftYOne = bottomLeftYOne / scaleY + additionValueForY
    

    【讨论】:

      猜你喜欢
      • 2014-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-22
      • 1970-01-01
      • 2021-09-18
      相关资源
      最近更新 更多