2

I am trying to resize the image of size (320, 800) to (319,70). I am running this through two conda environments and both of them have the same version of opencv. The code is:

def pre_process(img, x1=319, x2=70, row_start=400, col_start=200):
    cv_img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    cv_img = cv2.bilateralFilter(cv_img,9,75,75)
    crop_img = cv_img[row_start:1080,col_start:1000]
    print(crop_img.shape)
    f_x = x1/crop_img.shape[1]
    f_y = x2/crop_img.shape[0]
    crop_img = cv2.resize(crop_img, None, fx = f_x, fy = f_y,interpolation = cv2.INTER_CUBIC)

img is of shape (720, 1280, 3)

It seems to work with python 3 but somehow, I get the following error with python 2:

cv2.error: OpenCV(3.4.1) 
/feedstock_root/build_artefacts/opencv_1520722613778/work/opencv- 
3.4.1/modules/imgproc/src/resize.cpp:4045: error: (-215) dsize.area()  0 || (inv_scale_x > 0 && inv_scale_y > 0) in function resize

I even looked at this answer for reference but it seems that I am getting the error the other way, instead of ssize, the problem is with dsize.

I have to use python 2 for some other processing and that's my constraint. Any suggestions on how can I solve this?

Sarvagya Gupta
  • 137
  • 1
  • 2
  • 8

1 Answers1

2

You already know to what dimension you want to resize the image.

You should rather use crop_img = cv2.resize(crop_img, (70, 319) , interpolation = cv2.INTER_CUBIC)

There are two ways to resize an image:

  1. The one used by you is to proportionally increase or decrease the image with a value. fx and fy are factors that multiply with the number of rows and column in the original image.
  2. The second way is to explicitly mention what size you want the new image to be in. Since you want the image to have a certain size you can use this method.
Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
  • Wow. This worked. Very weird though, why am I getting this error in python 2 and not in 3? Also, what's the difference between the two? (sorry, I don't have enough points to accept the answer) – Sarvagya Gupta Jun 21 '18 at 11:34
  • @SarvagyaGupta Difference between python 2 and 3 OR between the two methods in the answer? – Jeru Luke Jun 21 '18 at 12:49