1

Is it possible to increase the size of a bounding box on a rectangle detection, I am detecting a rectangle fine and cropping is perfect around the size of the box, but I would like to slightly increase the size of the bounding box before it crops, that way I get a little more than just the rectangle in the crop.

Here is the code I have.

let imageSize = changedImage.extent.size

    // Verify detected rectangle is valid.
    let boundingBox = detectedRectangle.boundingBox.scaled(to: imageSize)
    guard changedImage.extent.contains(boundingBox)
        else { return }

    let topLeft = detectedRectangle.topLeft.scaled(to: imageSize)
    let topRight = detectedRectangle.topRight.scaled(to: imageSize)
    let bottomLeft = detectedRectangle.bottomLeft.scaled(to: imageSize)
    let bottomRight = detectedRectangle.bottomRight.scaled(to: imageSize)

        let correctedImage = changedImage
        .cropped(to: boundingBox)
Tony Merritt
  • 1,177
  • 11
  • 35
  • Are you trying to increase via points or percentage? – MQLN Dec 02 '17 at 18:59
  • I would like to go by percent and add around 25% all around – Tony Merritt Dec 02 '17 at 19:00
  • What should happen if the "25% bigger" boundingBox is bigger than the image? Or if it causes *part* of the boundingBox to extend outside the image extent? – DonMag Dec 02 '17 at 19:23
  • Possible duplicate of [How can I increase the size of a CGRect by a certain percent value?](https://stackoverflow.com/questions/26918886/how-can-i-increase-the-size-of-a-cgrect-by-a-certain-percent-value) – DonMag Dec 02 '17 at 19:30
  • The bounding box will not be bigger it is cropping the rectangle from a full screen image and the rectangle is small – Tony Merritt Dec 02 '17 at 19:40
  • I looked at the duplicate post and tried the inset by but it still returns an image the same size – Tony Merritt Dec 03 '17 at 16:27

0 Answers0