<span style="font-family:Microsoft YaHei;font-size:18px;">#include "stdafx.h" #include "GeometricTrans.h" #include "InputScale.h" #include "Translate.h" GeometricTrans::GeometricTrans() { } GeometricTrans::~GeometricTrans() { } //图像缩放 BOOL GeometricTrans::ReSize(Mat& InImage,Mat& OutImage,float Scale) { CInputScale Cale; Cale.DoModal(); Scale = Cale.val; /// 比例缩放 Mat dst; //目标图像指针 Size dst_size; //目标图像尺寸 dst_size.width = InImage.cols * Scale; //目标图像的宽为源图像宽的scale倍 dst_size.height = InImage.rows * Scale; //目标图像的高为源图像高的scale倍 resize(InImage, dst, dst_size); //缩放源图像到目标图像 /// 显示缩放后的图像 namedWindow("NewImage", CV_WINDOW_AUTOSIZE); imshow("NewImage", dst); return TRUE; } BOOL GeometricTrans::Translate(Mat& InImage, Mat& OutImage, int dx , int dy) { CTranslate Tra; Tra.DoModal(); dx = Tra.m_fx; dy = Tra.m_fy; CV_Assert(InImage.depth() == CV_8U); int rows = InImage.rows; int cols = InImage.cols; OutImage.create(rows, cols, InImage.type()); //3通道指针 Vec3b *p; for (int i = 0; i < rows; i++) { p = OutImage.ptr<Vec3b>(i); for (int j = 0; j < cols; j++) { //平移后坐标映射到原图像 int x = j - dx; int y = i - dy; //保证映射后的坐标在原图像范围内 if (x >= 0 && y >= 0 && x < cols && y < rows) { p[j] = InImage.ptr<Vec3b>(y)[x]; } } } // namedWindow("NewImage", CV_WINDOW_AUTOSIZE); // imshow("NewImage", OutImage); return TRUE; } //水平镜像 BOOL GeometricTrans::HrizonMirrorTrans(Mat &InImage, Mat &OutImage) { CV_Assert(InImage.depth() == CV_8U); OutImage.create(InImage.rows, InImage.cols, InImage.type()); int rows = InImage.rows; int cols = InImage.cols; Vec3b *origal; Vec3b *p; for (int i = 0; i < rows; i++) { origal = InImage.ptr<Vec3b>(i); p = OutImage.ptr<Vec3b>(i); for (int j = 0; j < cols; j++) { p[j] = origal[cols - 1 - j]; } } // namedWindow("NewImage", CV_WINDOW_AUTOSIZE); // imshow("NewImage", OutImage); return TRUE; } //垂直镜像 BOOL GeometricTrans::VerticalMirrorTrans(Mat &InImage, Mat &OutImage) { CV_Assert(InImage.depth() == CV_8U); OutImage.create(InImage.rows, InImage.cols, InImage.type()); int rows = InImage.rows; for (int i = 0; i < rows; i++) { //将倒数第i行转化为目标图像第i行 InImage.row(rows - i - 1).copyTo(OutImage.row(i)); } // namedWindow("NewImage", CV_WINDOW_AUTOSIZE); // imshow("NewImage", OutImage); return TRUE; } IplImage* GeometricTrans::RotateImage(IplImage* Inimage, int Angle, BOOL bOk) { CInputScale Cale; Cale.DoModal(); Angle = Cale.val; Angle = abs(Angle) % 180; if (Angle > 90) { Angle = 90 - (Angle % 90); } IplImage* dst = NULL; int width = (double)(Inimage->height * sin(Angle * CV_PI / 180.0)) + (double)(Inimage->width * cos(Angle * CV_PI / 180.0)) + 1; int height = (double)(Inimage->height * cos(Angle * CV_PI / 180.0)) + (double)(Inimage->width * sin(Angle * CV_PI / 180.0)) + 1; int tempLength = sqrt((double)Inimage->width * Inimage->width + Inimage->height * Inimage->height) + 10; int tempX = (tempLength + 1) / 2 - Inimage->width / 2; int tempY = (tempLength + 1) / 2 - Inimage->height / 2; int flag = -1; dst = cvCreateImage(cvSize(width, height), Inimage->depth, Inimage->nChannels); cvZero(dst); IplImage* temp = cvCreateImage(cvSize(tempLength, tempLength), Inimage->depth, Inimage->nChannels); cvZero(temp); cvSetImageROI(temp, cvRect(tempX, tempY, Inimage->width, Inimage->height)); cvCopy(Inimage, temp, NULL); cvResetImageROI(temp); if (bOk) flag = 1; float m[6]; int w = temp->width; int h = temp->height; m[0] = (float)cos(flag * Angle * CV_PI / 180.); m[1] = (float)sin(flag * Angle * CV_PI / 180.); m[3] = -m[1]; m[4] = m[0]; // 将旋转中心移至图像中间 m[2] = w * 0.5f; m[5] = h * 0.5f; // CvMat M = cvMat(2, 3, CV_32F, m); cvGetQuadrangleSubPix(temp, dst, &M); cvReleaseImage(&temp); return dst; }</span>