我正在尝试使用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);
}

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

最佳答案

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

rotatedImage.getPixelsRef()[newIndex] = image.getPixelsRef()[index];
rotatedImage.getPixelsRef()[newIndex+1] = image.getPixelsRef()[index+1];
rotatedImage.getPixelsRef()[newIndex+2] = image.getPixelsRef()[index+2];

10-08 05:06