#include"rotate.h"
IplImage* Rotate::rotateImage(IplImage* img,double degree)
{
double angle = degree * CV_PI / 180.; // 弧度
double a = sin(angle), b = cos(angle);
int width = img->width;
int height = img->height;
int width_rotate= int(height * fabs(a) + width * fabs(b));
int height_rotate=int(width * fabs(a) + height * fabs(b));
//旋转数组map
// [ m0 m1 m2 ] ===> [ A11 A12 b1 ]
// [ m3 m4 m5 ] ===> [ A21 A22 b2 ]
float map[6];
CvMat map_matrix = cvMat(2, 3, CV_32F, map);
// 旋转中心
CvPoint2D32f center = cvPoint2D32f(width / 2, height / 2);
cv2DRotationMatrix(center, degree, 1.0, &map_matrix);
map[2] += (width_rotate - width) / 2;
map[5] += (height_rotate - height) / 2;
IplImage* img_rotate = cvCreateImage(cvSize(width_rotate, height_rotate), 8, 3);
//对图像做仿射变换
//CV_WARP_FILL_OUTLIERS - 填充所有输出图像的象素。
//如果部分象素落在输入图像的边界外,那么它们的值设定为 fillval.
//CV_WARP_INVERSE_MAP - 指定 map_matrix 是输出图像到输入图像的反变换,
cvWarpAffine( img,img_rotate, &map_matrix, CV_INTER_LINEAR | CV_WARP_FILL_OUTLIERS, cvScalarAll(0));
return img_rotate;
}
void Rotate::compensate_affine_coor1(int *x0,int *y0,int w1,int h1,float theta)
{
if(theta>180) theta=theta-360;
if(theta<-180) theta=theta+360;
float x_ori=0,y_ori=0;
float x_tmp,y_tmp;
float x1=*x0;
float y1=*y0;
theta=theta*CV_PI/180;
if(theta>=0&&theta<=CV_PI/2)
{
x_ori=0;
y_ori=w1*sin(theta);
}
if(theta>CV_PI/2)
{
x_ori=-w1*cos(theta);
y_ori=(w1*sin(theta)-h1*cos(theta));
}
if(theta<0&&theta>=-CV_PI/2)
{
x_ori=-h1*sin(theta);
y_ori=0;
}
if(theta<-CV_PI/2)
{
x_ori=-w1*cos(theta)-h1*sin(theta);
y_ori=-h1*cos(theta);
}
if(theta>0)
{
x_tmp=cos(theta)*x1+sin(theta)*y1;
y_tmp=-sin(theta)*x1+cos(theta)*y1;
}
else
{
x_tmp=cos(-theta)*x1-sin(-theta)*y1;
y_tmp=sin(-theta)*x1+cos(-theta)*y1;
}
x1=x_tmp+1;
y1=y_tmp+1;
x1=x1+x_ori;
y1=y1+y_ori;
*x0=x1;
*y0=y1;
}
void Rotate::cutimage(string filepath,string imgpathname)
{
int pos=imgpathname.find_last_of('.');
string txtpathname(imgpathname.substr(0,pos));
imgpathname=filepath+"\\"+txtpathname+".jpg";
txtpathname=filepath+"\\"+txtpathname+".txt";
if(_access(txtpathname.c_str(),0)==-1)
{
return;
}
IplImage *img=cvLoadImage(imgpathname.c_str());
ifstream file(txtpathname.c_str());
string temp;
char* temp1;
float para[9];
while(getline (file, temp))
{
temp1=new char[temp.length()+1];
strcpy(temp1,temp.c_str());
char* para1 = strtok(temp1,"\t");
for(int i=0;i<9;i++)
{
para[i]=atof(para1);
para1 = strtok(NULL,"\t");
}
IplImage *rotate=rotateImage(img,90-para[8]);
int lefttopx=para[0],lefttopy=para[1],rightbottomx=para[4],rightbottomy=para[5];
compensate_affine_coor1(&lefttopx,&lefttopy,img->width,img->height,90-para[8]);
compensate_affine_coor1(&rightbottomx,&rightbottomy,img->width,img->height,90-para[8]);
int centerx=(lefttopx+rightbottomx+1)/2;
int centery=(lefttopy+rightbottomy+1)/2;
lefttopx=min(max(0,centerx-(centerx-lefttopx)*5),rotate->width);
rightbottomx=min(max(0,centerx+(rightbottomx-centerx)*5),rotate->width);
lefttopy=min(max(0,centery-(centery-lefttopy)*2),rotate->height);
rightbottomy=min(max(0,centery+(rightbottomy-centery)*2),rotate->height);
IplImage *cut=cvCreateImage(cvSize(fabs(1.0*lefttopx-rightbottomx),fabs(1.0*lefttopy-rightbottomy)),8,3);
cvSetImageROI(rotate,cvRect(min(lefttopx,rightbottomx),min(lefttopy,rightbottomy),fabs(1.0*lefttopx-rightbottomx),fabs(1.0*lefttopy-rightbottomy)));
//cvRectangle(rotate,cvPoint(lefttopx,lefttopy),cvPoint(rightbottomx,rightbottomy),cvScalar(0,0,255),1);
cvCopy(rotate,cut,0);
cvShowImage("旋转图",cut);
cvWaitKey(0);
cvReleaseImage(&cut);
cvReleaseImage(&rotate);
}
file.close();
file.clear();
}