画像処理入門 > 初級編 > 左右反転

左右反転


左右反転
画像の左右を反転するプログラムです。

サンプルプログラム
プログラムはこちら /* in : 横は4バイト */ void FlipH(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[i * widthStep + (inWidth - j -1)]; } } break; case 3: // 24bit画像 for(i = 0; i < inHeight; ++i){ for(j = 0; j < inWidth; ++j){ out[i * widthStep + j * 3] = in[i * widthStep + (inWidth - j -1) * 3]; out[i * widthStep + j * 3 + 1] = in[i * widthStep + (inWidth - j -1) * 3 + 1]; out[i * widthStep + j * 3 + 2] = in[i * widthStep + (inWidth - j -1) * 3 + 2]; } } break; case 4: // 32bit画像 for(i = 0; i < inHeight; ++i){ for(j = 0; j < inWidth; ++j){ out[i * widthStep + j * 4] = in[i * widthStep + (inWidth - j -1) * 4]; out[i * widthStep + j * 4 + 1] = in[i * widthStep + (inWidth - j -1) * 4 + 1]; out[i * widthStep + j * 4 + 2] = in[i * widthStep + (inWidth - j -1) * 4 + 2]; out[i * widthStep + j * 4 + 3] = in[i * widthStep + (inWidth - j -1) * 4 + 3]; } } break; default: break; } }