画像処理入門 > 初級編 > 90度回転

90度回転


上下反転
画像を90度回転させるプログラムです。

サンプルプログラム
プログラムはこちら /* in : 横は4バイト */ void FlipR(unsigned char *out, unsigned char *in, int inHeight, int inWidth, int inChannel, int direction){ int inStep, outStep; // OpenCVで使用する画像データの横幅は4バイト区切りのため調整 inStep = inWidth * inChannel; if( (inStep % 4) != 0){ inStep = inStep + (4 - inStep %4); } outStep = inHeight * inChannel; if( (outStep % 4) != 0){ outStep = outStep + (4 - outStep %4); } switch(direction){ case 0: FlipRight(out, outStep, in, inHeight, inWidth, inChannel, inStep); break; case 1: FlipLeft(out, outStep, in, inHeight, inWidth, inChannel, inStep); break; } } void FlipRight(unsigned char *out, int outStep, unsigned char *in, int inHeight, int inWidth, int inChannel, int inStep){ int i, j; switch(inChannel){ case 1: // 8bit画像 for(i = 0; i < inHeight; ++i){ for(j = 0; j < inWidth; ++j){ out[j * outStep + (inHeight - i - 1)] = in[i * inStep + j]; } } break; case 3: // 24bit画像 for(i = 0; i < inHeight; ++i){ for(j = 0; j < inWidth; ++j){ out[j * outStep + (inHeight - i - 1) * 3] = in[i * inStep + j * 3]; out[j * outStep + (inHeight - i - 1) * 3 + 1] = in[i * inStep + j * 3 + 1]; out[j * outStep + (inHeight - i - 1) * 3 + 2] = in[i * inStep + j * 3 + 2]; } } break; case 4: // 32bit画像 for(i = 0; i < inHeight; ++i){ for(j = 0; j < inWidth; ++j){ out[j * outStep + (inHeight - i - 1) * 4] = in[i * inStep + j * 4]; out[j * outStep + (inHeight - i - 1) * 4 + 1] = in[i * inStep + j * 4 + 1]; out[j * outStep + (inHeight - i - 1) * 4 + 2] = in[i * inStep + j * 4 + 2]; out[j * outStep + (inHeight - i - 1) * 4 + 3] = in[i * inStep + j * 4 + 3]; } } break; default: break; } } void FlipLeft(unsigned char *out, int outStep, unsigned char *in, int inHeight, int inWidth, int inChannel, int inStep){ int i, j; switch(inChannel){ case 1: // 8bit画像 for(i = 0; i < inHeight; ++i){ for(j = 0; j < inWidth; ++j){ out[(inWidth - j - 1) * outStep + i] = in[i * inStep + j]; } } break; case 3: // 24bit画像 for(i = 0; i < inHeight; ++i){ for(j = 0; j < inWidth; ++j){ out[(inWidth - j - 1) * outStep + i * 3] = in[i * inStep + j * 3]; out[(inWidth - j - 1) * outStep + i * 3 + 1] = in[i * inStep + j * 3 + 1]; out[(inWidth - j - 1) * outStep + i * 3 + 2] = in[i * inStep + j * 3 + 2]; } } break; case 4: // 32bit画像 for(i = 0; i < inHeight; ++i){ for(j = 0; j < inWidth; ++j){ out[(inWidth - j - 1) * outStep + i * 4] = in[i * inStep + j * 4]; out[(inWidth - j - 1) * outStep + i * 4 + 1] = in[i * inStep + j * 4 + 1]; out[(inWidth - j - 1) * outStep + i * 4 + 2] = in[i * inStep + j * 4 + 2]; out[(inWidth - j - 1) * outStep + i * 4 + 3] = in[i * inStep + j * 4 + 3]; } } break; default: break; } }