1

我正在尝试使用 openFrameworks 旋转图像,但我遇到了问题。我的旋转图像是红色而不是原来的颜色。

void testApp::setup(){
image.loadImage("abe2.jpg");
rotatedImage.allocate(image.width, image.height, OF_IMAGE_COLOR);

imageCenterX = image.getWidth() / 2;
imageCenterY = image.getHeight() / 2;
w = image.getWidth();
h = image.getHeight();
int degrees = 180;
float radians = (degrees*(PI / 180));

for (int y = 0; y < h; y++) {
    for (int x = 0; x < w; x++) {
        int index = image.getPixelsRef().getPixelIndex(x, y);

        int newX = (cos(radians) * (x - imageCenterX) - sin(radians) * (y - imageCenterY) + imageCenterX);
        int newY = (sin(radians) * (x - imageCenterX) + cos(radians) * (y - imageCenterY) + imageCenterY);

        int newIndex = rotatedImage.getPixelsRef().getPixelIndex(newX, newY);

        rotatedImage.getPixelsRef()[newIndex] = image.getPixelsRef()[index];
    }
}
rotatedImage.update();
}

 void testApp::update(){
 }

void testApp::draw(){
image.draw(0,0);
rotatedImage.draw(0,400);
}

有人可以告诉我我做错了什么吗?

4

1 回答 1

2

如果您的图像具有三个颜色分量(红、绿、蓝),则需要转换所有这三个分量。以下应该可以解决问题:

rotatedImage.getPixelsRef()[newIndex] = image.getPixelsRef()[index];
rotatedImage.getPixelsRef()[newIndex+1] = image.getPixelsRef()[index+1];
rotatedImage.getPixelsRef()[newIndex+2] = image.getPixelsRef()[index+2];
于 2012-10-08T19:15:32.460 回答