原理自己找书查看;
代码如下:
void Ctry::OnTryTyr1() { //TODO: 在此添加命令处理程序代码 IplImage *img = cvLoadImage("C:\\Users\\Administrator\\Desktop\\3838.jpg"); IplImage *move = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 3); IplImage* r = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 1); IplImage* g = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 1); IplImage* b = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 1); IplImage* r1 = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 1); IplImage* g1 = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 1); IplImage* b1 = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 1); cvSplit(img, r, g, b, NULL); int nHeight = img->height; int nWidth = img->width; int x = 50; //水平右移距离 int y = 50; //垂直下移距离 if (x > img->width || y > img->height) { MessageBox(NULL, "超过图片大小", "错误", MB_OK | MB_ICONERROR); return; } double temp1=0, temp2=0, temp3=0; for (int j = 0; j < r->height; j++) { for (int i = 0; i < r->width; i++) { if (i - x>0 && i - x < nWidth && j - y>0 && j - y <nHeight) { temp1 = cvGetReal2D(r, j - y, i - x); temp2 = cvGet2D(g, j - y, i - x).val[0]; temp3 = cvGet2D(b, j - y, i - x).val[0]; cvSetReal2D(r1, j, i, temp1); cvSetReal2D(g1, j, i, temp2); cvSetReal2D(b1, j, i, temp3); } else { cvSetReal2D(r1, j, i, 255); cvSet2D(g1, j, i, 255); cvSet2D(b1, j, i, 255); } } } cvMerge(r1, g1, b1, 0, move); cvNamedWindow("move", CV_WINDOW_AUTOSIZE); cvShowImage("move", move); cvSaveImage("C:\\Users\\Administrator\\Desktop\\move.jpg", move); cvNamedWindow("img", CV_WINDOW_AUTOSIZE); cvShowImage("img", img); cvWaitKey(0); cvReleaseImage(&img); cvReleaseImage(&r); cvReleaseImage(&g); cvReleaseImage(&b); cvReleaseImage(&r1); cvReleaseImage(&g1); cvReleaseImage(&b1); cvReleaseImage(&move); cvDestroyWindow("move"); cvDestroyWindow("r1"); cvDestroyWindow("img"); }