画像処理入門 >
初級編 >
中心基準に回転
中心基準に回転
中心基準に回転
サンプルプログラム
プログラムはこちら
void rotation(unsigned char *out, unsigned char *in, int inHeight, int inWidth, int inChannel, double theta){
int i, j;
int index;
int widthStep;
int pointX, pointY;
double centerX = inWidth / 2.0;
double centerY = inHeight / 2.0;
double radian = theta / 180.0 * 3.14159265;
double cosRadian = cos(radian);
double sinRadian = sin(radian);
widthStep = inWidth * inChannel;
if(widthStep % 4 != 0){
widthStep = widthStep + 4 - widthStep % 4;
}
for(i = 0; i < inHeight; ++i){
for(j = 0; j < inWidth; ++j){
pointX = (int)((j - centerX) * cosRadian - (i - centerY) * sinRadian + centerX);
pointY = (int)((j - centerX) * sinRadian + (i - centerY) * cosRadian + centerY);
// poiuntX, pointYが入力画像の有効範囲にあれば出力画像へ代入する
if(pointX >= 0 && pointX < inWidth && pointY >= 0 && pointY < inHeight){
out[i * widthStep + j] = in[pointY * widthStep + pointX];
} else {
out[i * widthStep + j] = 0;
}
}
}
}