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

后端 未结 7 1079
轮回少年
轮回少年 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:06

    Here's my EmguCV (a C# port of OpenCV) solution:

    public static Image Rotate90(this Image img)
        where TColor : struct, IColor
        where TDepth : new()
    {
        var rot = new Image(img.Height, img.Width);
        CvInvoke.cvTranspose(img.Ptr, rot.Ptr);
        rot._Flip(FLIP.HORIZONTAL);
        return rot;
    }
    
    public static Image Rotate180(this Image img)
        where TColor : struct, IColor
        where TDepth : new()
    {
        var rot = img.CopyBlank();
        rot = img.Flip(FLIP.VERTICAL);
        rot._Flip(FLIP.HORIZONTAL);
        return rot;
    }
    
    public static void _Rotate180(this Image img)
        where TColor : struct, IColor
        where TDepth : new()
    {
        img._Flip(FLIP.VERTICAL);
        img._Flip(FLIP.HORIZONTAL);
    }
    
    public static Image Rotate270(this Image img)
        where TColor : struct, IColor
        where TDepth : new()
    {
        var rot = new Image(img.Height, img.Width);
        CvInvoke.cvTranspose(img.Ptr, rot.Ptr);
        rot._Flip(FLIP.VERTICAL);
        return rot;
    }
    

    Shouldn't be too hard to translate it back into C++.

提交回复
热议问题