/* Pixel manipulation is necessary when applying a homography to an image. This demo illustrates how this is done. The top half of the image is exchanged with the bottom half. J.K. Johnstone */ #include // these two OpenCV libraries should be included #include // in any OpenCV program #include using namespace std; IplImage *srcImg; // original image IplImage *createdImg; // image created by pixel manipulation int main (int argc, char **argv) { if (argc != 2) { cout << "Need an image parameter (jpg or ppm/pgm)" << endl; exit(-1); } srcImg = cvLoadImage (argv[1]); createdImg = cvCreateImage (cvSize(srcImg->width, srcImg->height), srcImg->depth, srcImg->nChannels); if (srcImg->depth != 8) { cout << "Sorry, I assumed 8-bit channels: you'd better generalize " << "the code before you work with this image." << endl; exit(-1); } // for example, 32-bit channels would simply cast the type to float*, not uchar* int midrow = srcImg->height / 2; int x,y,rgb; if (srcImg->nChannels == 1) // grey-scale image for (x=0; xwidth; x++) { for (y=0; yimageData + createdImg->widthStep*y))[x] = ((uchar *)(srcImg->imageData + srcImg->widthStep*(y+midrow)))[x]; for (y=midrow; yheight; y++) // and bottom is top ((uchar *)(createdImg->imageData + createdImg->widthStep*y))[x] = ((uchar *)(srcImg->imageData + srcImg->widthStep*(y-midrow)))[x]; } else if (srcImg->nChannels == 3) // RGB image for (x=0; xwidth; x++) { for (y=0; yimageData + createdImg->widthStep*y))[3*x + rgb] = ((uchar *)(srcImg->imageData + srcImg->widthStep*(y+midrow)))[3*x + rgb]; for (y=midrow; yheight; y++) for (rgb=0; rgb<3; rgb++) ((uchar *)(createdImg->imageData + createdImg->widthStep*y))[3*x + rgb] = ((uchar *)(srcImg->imageData + srcImg->widthStep*(y-midrow)))[3*x + rgb]; } cvNamedWindow ("pixel manipulation", 1); cvShowImage ("pixel manipulation", createdImg); cvWaitKey(0); cvDestroyWindow("foo"); cvReleaseImage (&srcImg); cvReleaseImage (&createdImg); return 0; }