4

l'm a little bit confused on width and height parameter :

Is the height which is the first parameter or the second ?

HEIGHT,WIDTH= img.shape[0:2] or WIDTH,HEIGHt= img.shape[0:2]

and in resize function height=32 and width=100 or the inverse ?

image=cv2.resize(img, (32, 100), interpolation=cv2.INTER_NEAREST)
10
  • 1
    it's height first, this follows common array semantics where it's Row length followed by Column length Commented May 12, 2017 at 14:01
  • even for the function resize ? Commented May 12, 2017 at 14:04
  • What you have written is correct, the size passed takes the param order height, width which is what you've written Commented May 12, 2017 at 14:06
  • 1
    docs.opencv.org/trunk/da/d6e/… here in the function resize width first Commented May 12, 2017 at 14:07
  • 2
    l think for the function resize the parameters are (width,height) Commented May 12, 2017 at 14:09

3 Answers 3

12

With .shape it's HEIGHT, WIDTH = img.shape[0:2]. The reason for this, is it's a numpy matrix, where the first value means number of rows, and the second is number of columns.

When you resize it's img = cv2.resize(img, (WIDTH, HEIGHT)).

Sign up to request clarification or add additional context in comments.

Comments

1

You are right, you can verify by yourself... When you do something like:

Mat occludedSquare= imread("p4.jpg");

then you find a matrix like:

enter image description here

but the p4 image is actually: width: 339 high: 372

so OpenCV is associating rows → high and cols → width

enter image description here

Comments

0

It's quite common (for me) to be confused. So, just copy the snippet from now on:

img = cv2.imread('./my_img.png')

resized_img = cv2.resize(img, 
                         (img.shape[1], img.shape[0]) 
                        )
# HEIGHT, WIDTH = img.shape[0:2]
# resized_img   = cv2.resize(img, (WIDTH, HEIGHT))

1

enter image description here constructor

enter image description here

2

enter image description here constructor of the Size class

enter image description here

3

enter image description here

construct function of the Mat class

  1. Mat(int rows, int cols , int type)
  2. Mat(Size(int cols, int rows), int type)
  • Mat use (row,col)
  • Point and Size use (x,y), or notate as (width,height)

In other words, these access the same point:

  • mat.at<type>(y,x)(cpp)
    or your_img_ndarray[y][x](python)
  • mat.at<type>(cv::Point(x,y)) (cpp)

The opencv doc is mainly for C++ code:

enter image description here

  • dsize: size of output image if it equals zero (None in Python), it is computed as:

dsize = Size(round(fx*src.cols), round(fy*src.rows))

1. explicitly specify dsize=dst.size() : fx and fy will be computed from that.

resize(src, dst, dst.size(), 0, 0, interpolation);
  • fx: scale factor along the horizontal axis; when it equals 0, it is computed as (double)dsize.width/src.cols

2. specify fx and fy , let the function compute the destination image size.

resize(src, dst, Size(), 0.5, 0.5, interpolation);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.