3

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)
Good Pen
  • 625
  • 6
  • 10
vincent75
  • 453
  • 1
  • 7
  • 16
  • it's height first, this follows common array semantics where it's Row length followed by Column length – EdChum May 12 '17 at 14:01
  • even for the function resize ? – vincent75 May 12 '17 at 14:04
  • What you have written is correct, the `size` passed takes the param order height, width which is what you've written – EdChum May 12 '17 at 14:06
  • 1
    http://docs.opencv.org/trunk/da/d6e/tutorial_py_geometric_transformations.html here in the function resize width first – vincent75 May 12 '17 at 14:07
  • 1
    l think for the function resize the parameters are (width,height) – vincent75 May 12 '17 at 14:09
  • That seems a little odd to me, when you look at the `shape` of an array the first value is the number of rows so it's height then width, and most size ops expect this order so I'm a bit surprised but in this case this maybe be reverse order, you may also find this post useful http://stackoverflow.com/questions/4195453/how-to-resize-an-image-with-opencv2-0-and-python2-6 – EdChum May 12 '17 at 14:14
  • @EdChum, thanks in the link you just gave : it's resize(width,height) but height,width=image.shape – vincent75 May 12 '17 at 14:27
  • Yeah, I'm a little surprised to be honest, not sure why, all other methods to my knowledge use the same axes order – EdChum May 12 '17 at 14:28
  • try an example to confirm that – vincent75 May 12 '17 at 14:30
  • If it's a _size_ is _width and height_, if it's a _shape_ it's _rows and cols_. That's pretty standard convention. Just remember that _width == cols_ and _height == rows_ – Miki May 12 '17 at 14:31

3 Answers3

9

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)).

tituszban
  • 4,797
  • 2
  • 19
  • 30
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

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
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);
Good Pen
  • 625
  • 6
  • 10