画像処理入門 > 初級編 > 上下反転

上下反転


上下反転
画像の上下を反転するプログラムです。

サンプルプログラム
プログラムはこちら /* in : 横は4バイト */ void FlipV(unsigned char *out, unsigned char *in, int inHeight, int inWidth, int inChannel){ int widthStep; int i, j; // OpenCVで使用する画像データの横幅は4バイト区切りのため調整 widthStep = inWidth * inChannel; if( (widthStep % 4) != 0){ widthStep = widthStep + (4 - widthStep %4); } // ループ中にif文を書くと遅いためループ前にビット数の条件分岐 switch(inChannel){ case 1: // 8bit画像 for(i = 0; i < inHeight; ++i){ for(j = 0; j < inWidth; ++j){ out[i * widthStep + j] = in[(inHeight - i - 1) * widthStep + j]; } } break; case 3: // 24bit画像 for(i = 0; i < inHeight; ++i){ for(j = 0; j < inWidth; ++j){ out[i * widthStep + j * 3] = in[(inHeight - i - 1) * widthStep + j * 3]; out[i * widthStep + j * 3 + 1] = in[(inHeight - i - 1) * widthStep + j * 3 + 1]; out[i * widthStep + j * 3 + 2] = in[(inHeight - i - 1) * widthStep + j * 3 + 2]; } } break; case 4: // 32bit画像 for(i = 0; i < inHeight; ++i){ for(j = 0; j < inWidth; ++j){ out[i * widthStep + j * 4] = in[(inHeight - i - 1) * widthStep + j * 4]; out[i * widthStep + j * 4 + 1] = in[(inHeight - i - 1) * widthStep + j * 4 + 1]; out[i * widthStep + j * 4 + 2] = in[(inHeight - i - 1) * widthStep + j * 4 + 2]; out[i * widthStep + j * 4 + 3] = in[(inHeight - i - 1) * widthStep + j * 4 + 3]; } } break; default: break; } }