#include #include #include #pragma comment(lib,"cv.lib") #pragma comment(lib,"cxcore.lib") #pragma comment(lib,"highgui.lib") void FlipR(unsigned char *out, unsigned char *in, int inHeight, int inWidth, int inChannel, int direction); void FlipRight(unsigned char *out, int outStep, unsigned char *in, int inHeight, int inWidth, int inChannel, int inStep); void FlipLeft(unsigned char *out, int outStep, unsigned char *in, int inHeight, int inWidth, int inChannel, int inStep); int main(){ IplImage *in, *out; in = cvLoadImage("./image640.bmp", 1); out = cvCreateImage(cvSize((int)(in->height), (int)(in->width)), in->depth, in->nChannels); FlipR((unsigned char*)out->imageData, (unsigned char*)in->imageData, in->height, in->width, in->nChannels, 1); 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 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; } }