2

Facing Syntax error while using this piece of code:

kernel_1 = np.ones((5, 5), np.uint8)

img_open = cv2.morphologyEx(img, op= cv2.MORPH_OPEN,kernel_1)

Error Message:

img_open = cv2.morphologyEx(img, op= cv2.MORPH_OPEN,kernel_1) ^ SyntaxError: positional argument follows keyword argument

えるまる
  • 2,409
  • 3
  • 24
  • 44
  • 1
    Possible duplicate of [Normal arguments vs. keyword arguments](https://stackoverflow.com/questions/1419046/normal-arguments-vs-keyword-arguments) – user14492 Oct 07 '19 at 12:11
  • You have the arguments backwards. Try `kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))` and `opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel, iterations=1)` – nathancy Oct 07 '19 at 20:17

3 Answers3

0

You cannot follow a positional argument after a keyword argument.

func(my_argu=True)  # keyword argument i.e. position doesn't matter. it always goes to my_argu
func(my_argh_1, my_arg_2)  # positional argument i.e. position/order matters

So to fuix you function without looking at the documentation:

cv.morphology(img, kernel_1, op=cv2.MORPH_OPEN)
or 
cv2.morphologyEx(img, cv2.MORPH_OPEN,kernel_1)
user14492
  • 2,116
  • 3
  • 25
  • 44
-1

The error is becuse you use the = statement for parameters before kernal_1

python will give an error if you do that an solution will be to try this:

kernel_1 = np.ones((5, 5), np.uint8)

img_open = cv2.morphologyEx(img, kernel_1,op=cv2.MORPH_OPEN)

i dont know if the locations of the parameters will be right but when it is this will solve your problem

and else you can also do this:

img_open = cv2.morphologyEx(img, op= cv2.MORPH_OPEN,{parameter_name}=kernel_1)

to solve this issue

Matthijs990
  • 637
  • 3
  • 26
  • This gives error: `----> 3 img_open =cv2.morphologyEx(img, kernel_1,op=cv2.MORPH_OPEN) TypeError: Argument given by name ('op') and position (2)` – VEDANSH SHARMA Oct 07 '19 at 12:40
-2

in img_open = cv2.morphologyEx(img, op= cv2.MORPH_OPEN,kernel_1)

op= cv.MORPH_OPEN is keyword argument and img, kernel_1 is positional argument.

python not allow keyword argument before positional argument

try cv2.morphologyEx(img, cv2.MORPH_OPEN,kernel_1)

kdw9502
  • 105
  • 6