通过旋转输入图片,并保存旋转后的图片。这样做是不是可以增加一些训练集数量。。。
#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);
cvSetImageROI(temp, cvRect(tempX, tempY, width, height));
cvCopy(img, temp, NULL);
cvResetImageROI(temp);
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;
}
效果:
转载请注明原文地址: https://ju.6miu.com/read-1296227.html