3

Im on Python 2.7 (x86) ,Open CV 3.1.0, Windows 7 (x64)

Trying some simple template matching on Open CV

The image

enter image description here

The code

dark_elixir_sample = cv2.imread('dark_elixir_sample.png')

w, h = dark_elixir_sample.shape[::-1]

I'm trying to get w,h for further usage in my script, however the script returns the following error.

The error

ValueError: too many values to unpack

3 Answers3

6

Your image shape returns 3 dimensions

im.shape
>>> (24, 28, 3)

If you only want the first 2 do:

w, h = im.shape[:-1]
>>> (24, 28)

or

w, h, _ = im.shape
# w is 24, h is 28

Because otherwise, you are trying to unpack 3 values into only 2 variables, that won't work in Python hence your error. The _ is like a convention in Python for variables you don't want to use, or a "throwaway".

Community
  • 1
  • 1
bakkal
  • 50,069
  • 10
  • 111
  • 100
0
dark_elixir_sample = cv2.imread('dark_elixir_sample.png',0)
w, h = dark_elixir_sample.shape[::-1]
Zoe
  • 23,712
  • 16
  • 99
  • 132
Hemant
  • 1
  • 1
0

you are doing

w, h = im.shape

what is happening-

w, h = imageHeight,imageWidth, number (3 for rgb, 2 for gray)

solution

w, h, _ = in.shape[::-1]

or convert into gray image