Easiest way to rotate by 90 degrees an image using OpenCV?

后端 未结 7 1096
轮回少年
轮回少年 2020-11-29 05:52

What is the best way (in c/c++) to rotate an IplImage/cv::Mat by 90 degrees? I would assume that there must be something better than transforming it using a matrix, but I ca

7条回答
  •  旧巷少年郎
    2020-11-29 06:20

    Rotation is a composition of a transpose and a flip.

    R_{+90} = F_x \circ T

    R_{-90} = F_y \circ T

    Which in OpenCV can be written like this (Python example below):

    img = cv.LoadImage("path_to_image.jpg")
    timg = cv.CreateImage((img.height,img.width), img.depth, img.channels) # transposed image
    
    # rotate counter-clockwise
    cv.Transpose(img,timg)
    cv.Flip(timg,timg,flipMode=0)
    cv.SaveImage("rotated_counter_clockwise.jpg", timg)
    
    # rotate clockwise
    cv.Transpose(img,timg)
    cv.Flip(timg,timg,flipMode=1)
    cv.SaveImage("rotated_clockwise.jpg", timg)
    

提交回复
热议问题