title: Python OpenCV resize (interpolation)
body: <p>I am resizing an image <code>init_input</code> along with a bounding box of a target object <code>g</code> within that image to perform object localisation. I am currently resizing the bounding box by determining how many pixels will be up-sampled and adding half of them to each of the bounding box coordinates. </p>
  
  <p>My code is as follows:</p>
  
  <pre><code>def next_state(init_input, b_prime, g):
      """ 
      Returns the observable region of the next state.
  
      Formats the next state's observable region, defined
      by b_prime, to be of dimension (224, 224, 3). Adding 16
      additional pixels of context around the original bounding box.
      The ground truth box must be reformatted according to the
      new observable region.
  
      :param init_input:
          The initial input volume of the current episode.
  
      :param b_prime:
          The subsequent state's bounding box. RED
  
      :param g:
          The ground truth box of the target object. YELLOW
      """
  
      # Determine the pixel coordinates of the observable region for the following state
      context_pixels = 16
      x1 = max(b_prime[0] - context_pixels, 0)
      y1 = max(b_prime[1] - context_pixels, 0)
      x2 = min(b_prime[2] + context_pixels, IMG_SIZE)
      y2 = min(b_prime[3] + context_pixels, IMG_SIZE)
  
      # Determine observable region
      observable_region = cv2.resize(init_input[y1:y2, x1:x2], (224, 224))
  
      # Difference between crop region and image dimensions
      x1_diff = x1
      y1_diff = y1
      x2_diff = IMG_SIZE - x2
      y2_diff = IMG_SIZE - y2
  
      # Resize ground truth box
      g[0] = int(g[0] - 0.5 * x1_diff)  # x1
      g[1] = int(g[1] - 0.5 * y1_diff)  # y1
      g[2] = int(g[2] + 0.5 * x2_diff)  # x2
      g[3] = int(g[3] + 0.5 * y2_diff)  # y2
  
      return observable_region, g 
  </code></pre>
  
  <p>The problem I am having is that this method is not accurate. As evident in the example below, the bounding box is still off. My idea is that this is due to the way interpolation works within the resizing of the image (therefore there is a skew between the pixels taken which is not <code>0.5</code>). Any advice on how to fix this would be much appreciated. </p>
  
  <p><a href="https://i.stack.imgur.com/dgqIP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dgqIP.png" alt="Example"></a></p>
  
  <h2>Working example thanks to lenik</h2>
  
  <p><a href="https://i.stack.imgur.com/O0WQI.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/O0WQI.png" alt="Working"></a></p>