vincent75
vincent75

Reputation: 473

WIDTH and HEIGHT parameters in OpenCV

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)

Upvotes: 4

Views: 9620

Answers (3)

Good Pen
Good Pen

Reputation: 801

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

Upvotes: 0

tituszban
tituszban

Reputation: 5152

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

Upvotes: 12

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

Upvotes: 1

Related Questions