Hi I coded this function :
# 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
I wrote this function :
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 ?
I finally succeeded, there is the complete code :
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)