c++ - 使用类旋转图像

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

我在将图像旋转 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风格的。这很酷,我喜欢这种编码,但您可以通过引用让您的生活更轻松。

您的代码的解决方案: 您永远不会将点设置为 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)

关于c++ - 使用类旋转图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27045281/

相关文章:

c++ - POSIX UDP 套接字未绑定(bind)到正确的 IP

ios - 是否有像 iOS 8 photo.app 这样的图像裁剪和旋转 View Controller ?

图像像素化库,非正方形 "pixel"形状

c++ - CMarkup XML 处理

c++ - 重载新的

c++ - 很难理解带有 async_read 和 async_write 的 Boost ASIO TCP 的一些概念

c++ - 在opencv中找到手作为最大轮廓

java - 使用 NetBeans 设置图像库

visual-studio-2010 - 停止 VS2010 询问解决方案加载时重新映射 url

dll - 无法从程序集 "...\Microsoft.Search.Interop.dll"中嵌入互操作类型,因为它缺少GuidAttribute属性