When I use this code to rotate the image, the destination image size remains same and hence the image gets clipped. Please provide me a way/code snippet to resize accordingly (like Matlab does in imrotate) so that image does not get clipped and outlier pixels gets filled with all white instead of black. I don’t want image to be scaled down to fit in original size. I just want rotation, no scaling.
void imrotate(std::string imgPath,std::string angleStr,std::string outPath) {
size_t found1,found2;
found1=imgPath.find_last_of('/');
found2=imgPath.size()-4;
IplImage* src=cvLoadImage(imgPath.c_str(), -1);;
IplImage* dst;
dst = cvCloneImage( src );
int angle = atoi(angleStr.c_str());
CvMat* rot_mat = cvCreateMat(2,3,CV_32FC1);
CvPoint2D32f center = cvPoint2D32f(
src->width/2,
src->height/2
);
double scale = 1;
cv2DRotationMatrix( center, angle, scale, rot_mat );
cvWarpAffine( src, dst, rot_mat);
char angStr[4];
sprintf(angStr,"%d",angle);
cvSaveImage(string(outPath+imgPath.substr(found1+1,found2-found1-1)+"_"+angStr+".jpg").c_str(),dst);
cvReleaseImage(&src);
cvReleaseImage(&dst);
cvReleaseMat( &rot_mat );
}
Original Image:
alt text http://freeimagehosting.in/images/185_ElectricalG.jpg
Rotated Image:
alt text http://freeimagehosting.in/images/638_ElectricalG_60.jpg
Instead of cloning the source image as the dest you are going to have to create an image big enough to take the final rotated image, which will be a square with sides 1.5 times the biggest of the source width or height.
Edit:
The amount you need to enlarge the destination by is 1 + sin(angle of rotation), which has a maximum value at 45 degrees of 1.414… This must be rounded up to a suitable value