【问题标题】:Rotating image using classes使用类旋转图像
【发布时间】:2014-11-20 16:59:24
【问题描述】:

我无法将图像旋转 90 度,图像为 768 x 768 像素。我在这里显示的代码能够创建一个新图像,但我编写的函数根本没有操作它。我在驱动程序中旋转它的图像类和函数在下面。我必须将所有图片顺时针和逆时针旋转 90 度;我认为我的问题是试图让指针正确切换像素。

class image {
    public:
        image();            //the image constructor (initializes everything)
        image(string filename);  //a image constructor that directly loads an image from disk
        image(image &other); //copy constructor
        ~image();           //the image destructor  (deletes the dynamically created pixel array)
        pixel** getPixels();                    //return the 2-dimensional pixels array
        int getWidth();                     //return the width of the image
        int getHeight();                    //return the height of the image
        void createNewImage(int width, int height);

    private:
        pixel** pixels;             // pixel data array for image 
        int width, height;      // stores the image dimensions 

        void pixelsToCImage(CImage* myImage);
};

void RotateClockWise(image *imageIn)
{
     image rotateImg;
     image *ptr = (image*) &rotateImg;
     *ptr = *imageIn;
     int height = rotateImg.getHeight();
     int width = rotateImg.getWidth();
     pixel** rotatePix = rotateImg.getPixels();

     for (int i = 0; i < height; i++)
     {
         for (int j = 0; j < width; j++)
         {
             rotatePix[i][j] = rotatePix[j][i];
             *(ptr + j * height + (height - i - 1)) = *(ptr + i * width + j);
         }
     }
}

【问题讨论】:

    标签: c++ image visual-studio-2010 class rotation


    【解决方案1】:

    首先,您的代码非常c 风格。这很酷,我喜欢这种编码方式,但您可以通过引用让您的生活更轻松。

    您的代码的解决方案: 您永远不会将点设置为 imageIn,只需将值从 image in 复制到 rotateImg:

     image rotateImg;
     image *ptr = (image*) &rotateImg;
     *ptr = *imageIn;
    

    这意味着你只修改了局部变量 rotateImg 而不是指针给出的对象。

    这里只是一个简单的 NO: ptr 点在图像上。每个 +j 表示“转到下一个图像”或更准确地说: ptr = ptr + sizeof(image);这应该是大约 12 个字节 + vtable。不要这样做。您可以在循环一维像素数组时执行此操作。

    *(ptr + j * height + (height - i - 1)) = *(ptr + i * width + j); //BAD
    

    这里有一些 C 风格的代码可以解决这个问题。我不知道你可以通过双指针**ptr(间接指针)给出一个二维数组。

    void RotateClockWise(image* imageIn)
    {
         image rotateImg;
         rotateImg = *imageIn;
         image *ptr = imageIn;
         int height = rotateImg.getHeight();
         int width = imageIn->getWidth();
    
         pixel** normalPix = rotateImg.getPixels();
         pixel** rotatePix = imageIn->getPixels();
    
         for (int i = 0; i < height; i++)
         {
             for (int j = 0; j < width; j++)
             {
                 rotatePix[i][j] = normalPix[(height-1)-j][(width-1)-i];
             }
         }
    }
    

    我懒得用 C++ 风格编写代码,但请看一下参考

    void RotateClockWise(image& imageIn)
    

    【讨论】:

    • 我试过了,应该是正确的代码,。这更有意义,但我的程序正在打破我老师写的我们不应该编辑的函数。我现在对此无能为力。不过,感谢您让这一点更清楚。
    • 没问题,帮助很有趣,其他人也帮助了我^^
    【解决方案2】:

    你有imageIn 参数可能指向你想要旋转的图像。但是,您创建 rotateImg 对象,获取指向此对象 (ptr) 的指针并将 imageIn 复制到此 ptr。因此,现在您操作的是图像副本而不是图像本身,这就是imageIn 指向的对象永远不会改变其值的原因。

    【讨论】:

    • 所以你是说我应该摆脱 rotateImg 并使用 imageIn 指针?当我尝试使用 getHeight() 和 getWidth() 函数时,这些赋值语句不再起作用。
    • 好吧,这段代码需要多次更改才能强制它工作;我只是说你为什么imageIn 从不改变它的像素。
    猜你喜欢
    • 2013-12-15
    • 2016-04-08
    • 2011-04-26
    • 2010-12-04
    • 2016-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多