旋转图像不起作用

时间:2012-10-08 18:44:37

标签: c++ image-processing openframeworks

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

有人能告诉我我做错了吗?

1 个答案:

答案 0 :(得分:2)

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

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