20

I'm attempting to scale down an image using the Python OpenCV bindings (CV2, the new bindings):

ret, frame = cap.read()
print frame.shape
# prints (720, 1280, 3)
smallsize = (146,260)

smallframe = cv2.resize(frame, smallsize)
print smallframe.shape
# prints (260, 146, 3)

As you can see, the dimensions somehow end up being flipped on the scaled down image. Instead of returning an image with dimensions (WxH) 146x260, I get 260x146.

What gives?

1
  • Also, it should be noted when I set smallsize to (260, 146), everything works out correctly.
    – adewinter
    Jan 21, 2014 at 2:54

2 Answers 2

34

This was answered long ago but never accepted. Let me explain just a little more for anyone else who gets confused by this. The problem is that in python, OpenCV uses numpy. Numpy array shapes, functions, etc. assume (height, width) while OpenCV functions, methods, etc. use (width, height). You just need to pay attention.

Summary:

  • cv2.anything() --> use (width, height)
  • image.anything() --> use (height, width)
  • numpy.anything() --> use (height, width)
3
  • 1
    Perfect, thank you for explaining. That makes much more sense now. Any idea why numpy didn't go with the Width x Height convention? That seems to be the standard pretty much everywhere...
    – adewinter
    Feb 28, 2014 at 15:04
  • @adewinter Well that's a nice surprise that it helped. As for the reason for row-major numpy, I believe it's because matrices usually use that notation and numpy is generally for mathematics, not just images. However since OpenCV is very much about images, it uses the x(column)-major notation.
    – KobeJohn
    Feb 28, 2014 at 15:34
  • Glad to have the explanation, though it makes no sense to me. Using the math conventions of (rows, cols) for some functions but width x height from the image world just seems bound to muck things up!
    – Hendy
    Jan 9, 2020 at 1:44
2

Because the size takes the columns first, and the first dimension of the matrix is the rows. Have a look at the documentation here.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.