# Reverse function of a padding functio

**URL:** <https://forum.opencv.org/t/reverse-function-of-a-padding-functio/12282>\
**Category:** Uncategorized\
**Tags:** programming\
**Created:** [March 8, 2023, 7:56pm UTC](https://forum.opencv.org/t/reverse-function-of-a-padding-functio/12282 "2023-03-08T19:56:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sylvain\_Ard](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/sylvain_ard/32/114_2.png) [@Sylvain\_Ard](https://forum.opencv.org/u/Sylvain_Ard)\
**Post date:** [March 8, 2023, 7:56pm UTC](https://forum.opencv.org/t/reverse-function-of-a-padding-functio/12282/1 "2023-03-08T19:56:00Z")

</div>

Hi I coded this function :

```auto
# Preprocess image for of top view training/ detection
def preprocess_img(img_file):
    new_height=1600
    new_width=1600
    im = cv2.imread(img_file, 1)
    im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
    height = im.shape[0]
    width = im.shape[1]
    if width>height:
        temp_height=round((width/new_width)*height)
        temp_width=new_width
    else
        temp_width=round((height/new_height)*width)
        temp_height=new_height
    im = cv2.resize(im, (temp_width, temp_height))#resizing
    temp = np.ones((new_height,new_ width,3), np.uint8)*255
    
    #padding
    # compute center offset
    x_center = (new_width - temp_width) // 2
    y_center = (new_height - temp_height) // 2

    # copy img image into center of result image
    temp[y_center:y_center+temp_height, 
           x_center:x_center+temp_width] = im
           
    
    im = cv2.GaussianBlur(temp, (5,5), 0)
    im = im/255
    return im

```

but I don’t succeeded to code the reverse function which take two arguments : the original image before prediction and therefore before preprocess and the rough predicted image and returns the predicted image in the original image size.  
Thank you for help  
Best regards

---

<div class="post-metadata">

**Author:** ![Sylvain\_Ard](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/sylvain_ard/32/114_2.png) [@Sylvain\_Ard](https://forum.opencv.org/u/Sylvain_Ard)\
**Post date:** [March 9, 2023, 10:46am UTC](https://forum.opencv.org/t/reverse-function-of-a-padding-functio/12282/2 "2023-03-09T10:46:46Z")

</div>

I wrote this function :

```auto
def postprocess_img(pred_img,original_img):
    org_width=original_img.shape[1]
    org_height=original_img.shape[0]
    pred_width=1600
    pred_height=1600
    if org_width>org_height :
        temp_width=pred_width
        temp_height=round((org_height/org_width)*pred_height)
    else:
        temp_width=round((org_width/org_height)*pred_width)
        temp_height=pred_height
    
    temp = np.ones((temp_height,temp_ width,3), np.uint8)*255
    # compute center offset
    x_center = (pred_width - temp_width) // 2
    y_center = (pred_height - temp_height) // 2

    # copy img image into center of result image
    temp[0:temp_height-1,0:temp_width-1] = 
        pred[y_center:y_center+temp_height, x_center:x_center+temp_width]
    
    im = cv2.resize(temp, (org_width, org_height))#resizing
    return im

```

is it good ?

---

<div class="post-metadata">

**Author:** ![Sylvain\_Ard](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/sylvain_ard/32/114_2.png) [@Sylvain\_Ard](https://forum.opencv.org/u/Sylvain_Ard)\
**Post date:** [March 9, 2023, 11:50am UTC](https://forum.opencv.org/t/reverse-function-of-a-padding-functio/12282/3 "2023-03-09T11:50:07Z")

</div>

I finally succeeded, there is the complete code :

```auto
import cv2
import numpy as np

# Preprocess image for of top view training/ detection
def preprocess_img(img_file):
    new_height=1600
    new_width=1600
    im = cv2.imread(img_file, 1)
    im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
    height = im.shape[0]
    width = im.shape[1]
    if width>height:
        temp_height=round((height/width)*new_height)
        temp_width=new_width
    else:
        temp_width=round((width/height)*new_width)
        temp_height=new_height
    im = cv2.resize(im, (temp_width, temp_height))#resizing
    temp = np.ones((new_height,new_width,3), np.uint8)*255
    
    #padding
    # compute center offset
    x_center = (new_width - temp_width) // 2
    y_center = (new_height - temp_height) // 2

    # copy img image into center of result image
    temp[y_center:y_center+temp_height, 
           x_center:x_center+temp_width] = im
           
    
    im = cv2.GaussianBlur(temp, (5,5), 0)
    im = im/255
    return im
    
# Postprocess image for of top view training/ detection
def postprocess_img(pred_img,original_img):
    pred_img=pred_img*255
    org_width=original_img.shape[1]
    org_height=original_img.shape[0]
    pred_width=1600
    pred_height=1600
    if org_width>org_height :
        temp_width=pred_width
        temp_height=round((org_height/org_width)*pred_height)
    else:
        temp_width=round((org_width/org_height)*pred_width)
        temp_height=pred_height
    
    temp = np.ones((temp_height,temp_width,3), np.uint8)*255
    # compute center offset
    x_center = (pred_width - temp_width) // 2
    y_center = (pred_height - temp_height) // 2

    # copy img image into center of result image
    temp[0:temp_height,0:temp_width] = pred_img[y_center:y_center+temp_height, x_center:x_center+temp_width]
    
    im = cv2.resize(temp, (org_width, org_height))#resizing
    return im
org = cv2.imread("image.jpg", 1)
im = preprocess_img("image.jpg")
cv2.imwrite("pred.jpg", im*255)
im2=postprocess_img(im,org)
cv2.imwrite("res.jpg", im2)

```
