将真实图像坐标转换为纵横比?
Convert real image coordinates to aspect fit?
例如:用户选择我发送到后端的 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 ,我想更改坐标以适合纵横比图像大小。
最后我用比率法实现了。我取了图片尺寸的纵横比,按照ratio math计算。
让 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
例如:用户选择我发送到后端的 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 ,我想更改坐标以适合纵横比图像大小。
最后我用比率法实现了。我取了图片尺寸的纵横比,按照ratio math计算。
让 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