#include #include #include #pragma comment(lib,"cv.lib") #pragma comment(lib,"cxcore.lib") #pragma comment(lib,"highgui.lib") void FlipV(unsigned char *out, unsigned char *in, int inHeight, int inWidth, int inChannel); int main(){ IplImage *in, *out; in = cvLoadImage("./image640.bmp", 1); out = cvCreateImage(cvSize((int)(in->width), (int)(in->height)), in->depth, in->nChannels); FlipV((unsigned char*)out->imageData, (unsigned char*)in->imageData, in->height, in->width, in->nChannels); cvNamedWindow("in", 1); cvShowImage("in", in); cvNamedWindow("out", 1); cvShowImage("out", out); cvWaitKey(-1); cvDestroyWindow("in"); cvDestroyWindow("out"); cvReleaseImage(&in); cvReleaseImage(&out); return 0; } /* 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; } }