【OpenCV】通过旋转图片增加训练集
2016-08-13 23:21
239 查看
通过旋转输入图片,并保存旋转后的图片。这样做是不是可以增加一些训练集数量。。。
效果:
#include <cv.h> #include <highgui.h> #include <stdio.h> #include <opencv2/core/core.hpp> #include <iostream> #include <fstream> using namespace std; using namespace cv; //旋转图像内容不变,尺寸相应变大 IplImage* rotateImage2(IplImage* img, int degree) { double angle = degree * CV_PI / 180.; double a = sin(angle), b = cos(angle); int width = img->width, height = img->height; //旋转后的新图尺寸 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 = sqrt((double)width * width + (double)height *height) + 10; int tempX = (tempLength + 1) / 2 - width / 2; 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); return img_rotate; } int main(){ char filename[100] =""; IplImage *result; IplImage *image = cvLoadImage("D:/1.png"); for (int i = 0;i < 360; i+=50) { char dir[100] = "D:/"; sprintf(filename, "%d.jpg", i); strcat(dir, filename); result = rotateImage2(image, i); cvSaveImage(dir, result); } cvWaitKey(0); return 0; }
效果:
相关文章推荐
- OpenCV旋转图片
- opencv 图片旋转90度
- opencv 图片旋转
- php_imagick实现图片剪切、旋转、锐化、减色或增加特效的方法
- 【图像处理】基于OpenCV底层实现的图片旋转
- 利用OpenCV通过HSV直方图来进行图片定位
- [项目实战派]opencv通过dll调用matlab函数,图片作为参数
- 使用php_imagick对图片改变大小、旋转、锐化、减色或是增加特效
- 我的Android进阶之旅------>Android通过使用Matrix旋转图片来模拟碟片加载过程
- QT中给各控件增加背景图片(可缩放可旋转)的几种方法
- opencv 旋转图片
- 图片旋转--修改老外的代码,返回中增加第四个顶点
- OpenCV下的图片旋转
- opencv 图片旋转
- 使用PHP扩展php_imagick对图片改变大小、旋转、锐化、减色或增加特效
- 如何通过手势来控制图片的旋转
- 通过图片旋转做一个水晶球效果
- opencv 旋转图片
- 疯狂ios讲义之实例:通过旋转手势旋转图片
- [项目实战派]csharp通过dll调用opencv函数,图片作为参数