2012-10-08 142 views
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); 
} 

有人能告訴我我做錯了什麼嗎?

+0

紅色的東西提醒我本能地未能實現飛機的RGB分離並意外地只在其中的一個操作的:下面應該做的伎倆。雖然不熟悉OFW,所以我不會有太大的幫助我下注 –

+1

你的輪換操作可能是意外翻轉指數的結果。檢查以確保索引(x,y等)的每次使用都與您的意思完全吻合 –

+0

剛剛意識到我在計算newY時使用了minus而不是plus。現在修復它並旋轉作品。現在只有顏色問題是一個問題。 – nkobber

回答

2

如果您的圖像有三種顏色成分(紅色,綠色,藍色),則需要轉換全部三種顏色成分。

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

非常感謝您的幫助。 – nkobber