图片旋转 Mat 版本
来源:互联网 发布:程序员 白发老头图片 编辑:程序博客网 时间:2024/06/07 20:19
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#include<io.h>
#include <opencv2/ml.hpp>
#include "cv.h"
using namespace cv;
using namespace std;
Mat rotateImage2(const Mat src, int degree)
{
IplImage imgTmp = src;
IplImage *img = cvCloneImage(&imgTmp);
double angle = degree* CV_PI / 180.;
float a = (float)sin(angle), b = (float)cos(angle);
int width = img->width, height = img->height;
//After rotated, new size 旋转后的新图尺寸
int width_rotate = int(height * fabs(a) + width * fabs(b));
int height_rotate = int(width * fabs(a) + height * fabs(b));
IplImage* img_rotate = cvCreateImage(cvSize(width_rotate, height_rotate), img->depth, img->nChannels);
cvZero(img_rotate);
//保证原图可以任意角度旋转的最小尺寸
int tempLength = (int)(sqrt((double)width * width + (double)height *height) + 10);
int tempX = (tempLength + 1) / 2 - width / 2;//rotated center
int tempY = (tempLength + 1) / 2 - height / 2;
IplImage* temp = cvCreateImage(cvSize(tempLength, tempLength), img->depth, img->nChannels);
cvZero(temp);
//将原图复制到临时图像tmp中心
cvSetImageROI(temp, cvRect(tempX, tempY, width, height));
cvCopy(img, temp, NULL);
cvResetImageROI(temp);
//旋转数组map
// [ m0 m1 m2 ] ===> [ A11 A12 b1 ]
// [ m3 m4 m5 ] ===> [ A21 A22 b2 ]
float m[6];
int w = temp->width;
int h = temp->height;
m[0] = b;
m[1] = a;
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, img_rotate, &M);
cvReleaseImage(&temp);
Mat mm(img_rotate, true);
return mm;
}
int main()
{
Mat src = imread("0_1.png",1);
Mat dst = rotateImage2(src,-45);
imshow("rotation", dst);
cvWaitKey(0);
return 0;
}
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#include<io.h>
#include <opencv2/ml.hpp>
#include "cv.h"
using namespace cv;
using namespace std;
Mat rotateImage2(const Mat src, int degree)
{
IplImage imgTmp = src;
IplImage *img = cvCloneImage(&imgTmp);
double angle = degree* CV_PI / 180.;
float a = (float)sin(angle), b = (float)cos(angle);
int width = img->width, height = img->height;
//After rotated, new size 旋转后的新图尺寸
int width_rotate = int(height * fabs(a) + width * fabs(b));
int height_rotate = int(width * fabs(a) + height * fabs(b));
IplImage* img_rotate = cvCreateImage(cvSize(width_rotate, height_rotate), img->depth, img->nChannels);
cvZero(img_rotate);
//保证原图可以任意角度旋转的最小尺寸
int tempLength = (int)(sqrt((double)width * width + (double)height *height) + 10);
int tempX = (tempLength + 1) / 2 - width / 2;//rotated center
int tempY = (tempLength + 1) / 2 - height / 2;
IplImage* temp = cvCreateImage(cvSize(tempLength, tempLength), img->depth, img->nChannels);
cvZero(temp);
//将原图复制到临时图像tmp中心
cvSetImageROI(temp, cvRect(tempX, tempY, width, height));
cvCopy(img, temp, NULL);
cvResetImageROI(temp);
//旋转数组map
// [ m0 m1 m2 ] ===> [ A11 A12 b1 ]
// [ m3 m4 m5 ] ===> [ A21 A22 b2 ]
float m[6];
int w = temp->width;
int h = temp->height;
m[0] = b;
m[1] = a;
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, img_rotate, &M);
cvReleaseImage(&temp);
Mat mm(img_rotate, true);
return mm;
}
int main()
{
Mat src = imread("0_1.png",1);
Mat dst = rotateImage2(src,-45);
imshow("rotation", dst);
cvWaitKey(0);
return 0;
}
0 0
- 图片旋转 Mat 版本
- Rotate Image------旋转图片(in-place版本)
- OpenCV Mat结构的图片 旋转顺时针90度 180度 270度 逆时针90度
- 图片旋转
- 图片旋转
- 旋转图片
- 图片旋转
- 图片旋转
- 旋转图片
- 旋转图片
- 图片旋转
- 图片旋转
- 图片旋转
- 图片旋转
- 图片旋转
- 图片旋转
- 图片旋转
- 旋转图片
- 括号匹配算法
- java设计模式,享元模式
- Android Studio 使用小技巧和快捷键
- 实用文章收集
- 去除字符串中的空格、回车、换行符、制表符
- 图片旋转 Mat 版本
- Ubuntu系统下Android开发环境搭建相关问题
- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- socket编程发送GET请求
- 使用Openjdk7编译android源码出现"Could not initialize NSS"错误的解决办法
- nginx安装配置记录
- 用shader在love2d里实现精灵描边效果
- C与指针
- java--图形界面(2)