根據角度進行旋轉

<!-- lang: cpp -->
//根據角度進行旋轉,角度爲水平正方向,angle爲弧度

void ImageRotate(const IplImage* src,IplImage* dst, double angle,CvScalar fillVal,int hor) { double scale = 1.0; double a = 0.0; CvMat* rot_mat = cvCreateMat(2,3,CV_32FC1);code

CvPoint2D32f center = cvPoint2D32f(
	0,
	0
	);

if(hor)
{
	a= -(angle)*180/CV_PI;
	cv2DRotationMatrix(center,a,scale,rot_mat);
}
else
{
	cvSet2D(rot_mat,0,0,cvScalar(1));
	cvSet2D(rot_mat,0,1,cvScalar(0));
	cvSet2D(rot_mat,0,2,cvScalar(0));
	cvSet2D(rot_mat,1,0,cvScalar(-1/tan(angle)));
	cvSet2D(rot_mat,1,1,cvScalar(1));
	cvSet2D(rot_mat,1,2,cvScalar(0));
}
/*cvSet2D(rot_mat,0,0,cvScalar(1/cos(angle)));
cvSet2D(rot_mat,0,1,cvScalar(-tan(angle)));
cvSet2D(rot_mat,0,2,cvScalar(0));
cvSet2D(rot_mat,0,2,cvScalar(1));
cvSet2D(rot_mat,0,2,cvScalar(0));*/

cvWarpAffine(src,dst,rot_mat,CV_WARP_FILL_OUTLIERS,fillVal);


//對超出的邊界的部分用原圖中的像素點進行填充
/*for(int i=0;i<src->height;i++)
{
	uchar* ptr = (uchar*)(dst->imageData+i*dst->widthStep);
	uchar* ptr1 = (uchar*)(src->imageData+i*src->widthStep);
	for(int j=0;j<src->width;j++)
	{
		if(ptr[j*3] == 0 && ptr[j*3+1] == 0 && ptr[j*3+2] == 0)
		{
			ptr[j*3] = ptr1[j*3];
			ptr[j*3+1] = ptr1[j*3+1];
			ptr[j*3+2] = ptr1[j*3+2];
		}
	}
}*/

cvReleaseMat(&rot_mat);

}io

相關文章
相關標籤/搜索