Home>

I'm new to Python.
I wanted to rotate the image from the center and tried it while watching the internet, but I don't know what the error message was giving an error.

In the code below, the error spit out was the eleventh line, undefined name 'identity'.
import cv2
import matplotlib.pyplot as plt

def rotate_center (image, angle):
h, w = image.shape [: 2]
affine = cv2.getRotationMatrix2D ((w/2.0, h/2.0), angle, 1.0)
return cv2.warpAffine (image, affine, (w, h))

ifname== "main":
image = cv2.imread ("mizu.jpg") [:,:, ::-1]
converted = identity (image)
plt.imshow (converted)
plt.title ("Identity")
plt.show ()

When I looked into it, it came out that there was a variable error, not being defined, etc., but if I don't check how to define a variable in Python, is it defined without permission? I do not understand because it is written.
I tried to rewrite the characters for strange spaces, but the error did not change.
And even if you look at the site described at the end, it is not described because it is not described where the rotation angle of the image is decided, so I am very sorry, but I would appreciate it if someone could tell me.

  • Answer # 1

    identity (image)is a function that performs "1. Identity transformation" in the reference site.
    The error is that "identityis not defined", but when you look at the code in the question, the function is not defined (copy and paste).

    Even if you say

    , you don't need to actually define (copy and paste), and what you want to do is "12. Rotate from the center of the image" in the reference site code>must be called from__ main __.

    import cv2
    import matplotlib.pyplot as plt
    import numpy as np
    def rotate_center (image, angle):
        h, w = image.shape [: 2]
        affine = cv2.getRotationMatrix2D ((w/2.0, h/2.0), angle, 1.0)
        return cv2.warpAffine (image, affine, (w, h))
    if __name__ == "__main__":
        image = cv2.imread ("mizu.jpg") [:,:, ::-1]
        # 30 is the rotation angle
        converted = rotate_center (image, 30)
        plt.imshow (converted)
        plt.title ("Rotate Center")
        plt.show ()

    I haven't checked the actual operation, but I think it will work.