Ask Your Question
0

How to solve this error? "AttributeError: 'module' object has no attribute 'wrapAffine'"

asked 2016-06-07 15:41:37 -0600

omee gravatar image

updated 2016-06-07 23:46:23 -0600

I am trying to run the following code:

    import cv2
    import numpy as np

    img = cv2.imread('images/input.png') 
    num_rows, num_cols = img.shape[:2]

    translation_matrix = np.float32([[1, 0, 70], [0, 1, 110]])
    img_translation = cv2.wrapAffine(img, translation_matrix, (num_cols, num_rows)) #I get the error in this line
    cv2.imshow('Translation', img_translation)
    cv2.waitKey()

But when I run the code I get this "C:\Python27\python.exe C:/OpenCV_Python/Python_Test.py Traceback (most recent call last): File "C:/OpenCV_Python/Python_Test.py", line 8, in <module> img_translation = cv2.wrapAffine(img, translation_matrix, (num_cols, num_rows)) AttributeError: 'module' object has no attribute 'wrapAffine'"

I am using OpenCV 3.1.0 and Python 2.7.11

Please help me. Thanks a lot in advance.

This code is from a book named "OpenCV with Python by Example" by Prateek Joshi. This book was first published in September 2015.

edit retag flag offensive close merge delete

Comments

hmm, why did you try to "correct" the error in your example ? it does not make any sense, and will confuse readers

berak gravatar imageberak ( 2016-06-07 23:26:46 -0600 )edit

I mistakenly asked the question with a typo that's why i had to correct it. I am extremely sorry.

omee gravatar imageomee ( 2016-06-07 23:29:37 -0600 )edit

don't be sorry, just edit it back to the original, and let's keep the "follow-up" problems with the answer, right ?

berak gravatar imageberak ( 2016-06-07 23:33:31 -0600 )edit

okay. I am editing back to its original form.

omee gravatar imageomee ( 2016-06-07 23:39:54 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2016-06-07 21:32:03 -0600

berak gravatar image

updated 2016-06-07 23:15:36 -0600

this is a simple typo, it must be: warpAffine() not wrapAffine()

>>> import cv2
>>> help(cv2.warpAffine)
Help on built-in function warpAffine:

warpAffine(...)
    warpAffine(src, M, dsize[, dst[, flags[, borderMode[, borderValue]]]]) -> dst
edit flag offensive delete link more

Comments

I am extremely sorry i asked the question with a typo. Please accept my apologies. I have fixed it now. And yet I still get this error. And its also happening for other programs too.

I run this program:

import cv2
import numpy as np
img = cv2.imread('images/input.jpg')
num_rows, num_cols = img.shape[:2]
rotation_matrix = cv2.getRotationMatrix2D((num_cols/2, num_rows/2), 30, 1)
img_rotation = cv2.warpAffine(img, rotation_matrix, (num_cols, num_rows))
cv2.imshow('Rotation', img_rotation)
cv2.waitKey()

And yet i still get error. The error is:

AttributeError: 'NoneType' object has no attribute 'shape'

I think both the errors are related. Please help. Thanks in advance.

omee gravatar imageomee ( 2016-06-07 23:25:37 -0600 )edit

^^ this is another, unrelated problem. your image simply was not loaded. add a check after imread() like:

if img==None: raise Exception("image not loaded")

(and maybe, that book is just bad ...)

berak gravatar imageberak ( 2016-06-07 23:30:36 -0600 )edit

I added the line you told me to add. It still doesn't work. I get the following error:

C:\Python27\python.exe C:/OpenCV_Python/Python_Test.py
Traceback (most recent call last):
  File "C:/OpenCV_Python/Python_Test.py", line 4, in <module>
    if img==None: throw("image not loaded")
NameError: name 'throw' is not defined

I am very sorry to ask an unrelated problem.

omee gravatar imageomee ( 2016-06-07 23:36:21 -0600 )edit

I added the line you told me to add. It still doesn't work. I get the following error:

C:\Python27\python.exe C:/OpenCV_Python/Python_Test.py
Traceback (most recent call last):
  File "C:/OpenCV_Python/Python_Test.py", line 4, in <module>
    if img==None: throw("image not loaded")
NameError: name 'throw' is not defined

I am very sorry to ask an unrelated problem.

omee gravatar imageomee ( 2016-06-07 23:36:38 -0600 )edit

i got typo's too ;) (now my job to feel sorry)

(please see edited comment above , it's raise Exception in python, not throw..)

berak gravatar imageberak ( 2016-06-07 23:38:37 -0600 )edit

Please don't be sorry. You helped me so much thanks a lot. But I tried this:

`if img==None: raise Exception("image not loaded")`

And yet I still get the error:

Traceback (most recent call last):
  File "C:/OpenCV_Python/Python_Test.py", line 4, in <module>
    if img==None: raise Exception("image not loaded")
Exception: image not loaded
omee gravatar imageomee ( 2016-06-07 23:44:46 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-06-07 15:41:37 -0600

Seen: 9,301 times

Last updated: Jun 07 '16