博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
基于OpenCV的图像几何变换算法
阅读量:5876 次
发布时间:2019-06-19

本文共 3684 字,大约阅读时间需要 12 分钟。

#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
(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
(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
(i); p = OutImage.ptr
(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;}

转载于:https://www.cnblogs.com/Toring/p/6628297.html

你可能感兴趣的文章
Golang协程与通道整理
查看>>
解决win7远程桌面连接时发生身份验证错误的方法
查看>>
C/C++ 多线程机制
查看>>
js - object.assign 以及浅、深拷贝
查看>>
python mysql Connect Pool mysql连接池 (201
查看>>
Boost在vs2010下的配置
查看>>
android camera(四):camera 驱动 GT2005
查看>>
一起谈.NET技术,ASP.NET伪静态的实现及伪静态的意义
查看>>
20款绝佳的HTML5应用程序示例
查看>>
string::c_str()、string::c_data()及string与char *的正确转换
查看>>
11G数据的hive初测试
查看>>
如何使用Core Text计算一段文本绘制在屏幕上之后的高度
查看>>
==和equals区别
查看>>
2010技术应用计划
查看>>
XML 节点类型
查看>>
驯服 Tiger: 并发集合 超越 Map、Collection、List 和 Set
查看>>
Winform开发框架之权限管理系统改进的经验总结(3)-系统登录黑白名单的实现...
查看>>
Template Method Design Pattern in Java
查看>>
MVC输出字符串常用四个方式
查看>>
LeetCode – LRU Cache (Java)
查看>>