Rotation of the image at any angle under the OpenCV

Source: Internet
Author: User
Tags cos sin
OpenCV the image under any angle of rotation//requires support from Intel Company's OpenCV.
OpenCV 4 images at any angle of rotation//image to be rotated iplimage* img_old//Returns the rotated image iplimage* img_tmp.
The angle of rotation, the unit degree. Three different methods. Where method Two is not fully tested, method one can satisfy most needs//vastsky-nercita 2005 6//vastsky_sun#126.com Iplimage * CCROPMEASUREVIEW::FI 
	Trotate (iplimage* img_old, double Angle,int method) {iplimage* img_tmp = NULL;
	Double Anglerad = (cv_pi* (angle/180));
	int Newheight =int (fabs ((Sin (anglerad) *img_old->width) + fabs ((cos (anglerad) *img_old->height));
	int Newwidth =int (fabs ((Sin (anglerad) *img_old->height) + fabs ((cos (anglerad) *img_old->width));
	Img_tmp = Cvcreateimage (Cvsize (newwidth,newheight), ipl_depth_8u, 3);            
	Cvfillimage (img_tmp,0);//The destination image uses an extended size iplimage* DST = cvcloneimage (img_old);//destination image and original image such as large float m[6];
	Cvmat m = Cvmat (2, 3, cv_32f, M);
		if (1==method) {//method one extracts the pixel quadrilateral, using sub-pixel precision int w = img_old->width;
		int h = img_old->height;
		M[0] = (float) (cos (angle*cv_pi/180.)); M[1] = (float) (Sin (angle*cv_pi/180.));
		M[2] = w*0.5f;
		M[3] =-m[1];
		M[4] = m[0];
		M[5] = h*0.5f;
		Cvgetquadranglesubpix (Img_old, DST, &m, 1, cvscalarall (0));  Cvgetquadranglesubpix (Img_old, Img_tmp, &m, Cv_inter_linear, Cvscalarall (0));//+cv_warp_fill_outliers//Method one
		Extracting pixel quadrilateral, using subpixel precision} if (2==method) {//Method two affine transformation matrices using two-dimensional rotation require input and output images as large as the center of rotation is not cvpoint2d32f center;
		Center.x=float (img_old->width/2.0+0.5);//float (img_tmp->width/2.0+0.5);   
		Center.y=float (img_old->height/2.0+0.5);//float (img_tmp->height/2.0+0.5);
		Cv2drotationmatrix (center, angle,1, &m); Cvwarpaffine (Img_old, DST, &m,cv_inter_linear,cvscalarall (0));//small picture//small target image//Image expansion//can only be treated differently in different quadrants within a certain angle
		T Dx=int ((newwidth-img_old->width)/2+0.5); 
		int Dy=int ((newheight-img_old->height)/2+0.5);
		uchar* old_ptr,*temp_ptr;  for (int y=0; yheight; y++)//In order not to cross out {for (int x=0; x< img_old->width; x + +) {Old_ptr = & ((uchar*) (Img_old->imAgedata + img_old->widthstep*y)) [(x)];
				Temp_ptr = & ((uchar*) (Img_tmp->imagedata + img_tmp->widthstep* (y+dy))) [(X+DX)]; TEMP_PTR[0]=OLD_PTR[0]; Green temp_ptr[1]=old_ptr[1]; Blue temp_ptr[2]=old_ptr[2];
		Red}} center.x=float (img_tmp->width/2.0+0.5);   
		Center.y=float (img_tmp->height/2.0+0.5);
		Cv2drotationmatrix (center, angle,1, &m); iplimage* temp = cvcloneimage (img_tmp);//Generate Output Image cvwarpaffine (img_tmp, temp, &m,cv_inter_linear+cv_warp_fill_
		Outliers,cvscalarall (0));//Large Figure img_tmp=cvcloneimage (temp); Problem//cvwarpaffine (img_tmp, img_tmp, &m,cv_inter_linear+cv_warp_fill_outliers,cvscalarall (0));//Large Map//method Two use two-dimensional spin
		Turn affine transformation matrix} if (3==method) {//Method three-perspective transformation cvpoint2d32f src_point[4];
		cvpoint2d32f Dst_point[4];                    
		src_point[0].x=0.0;
		src_point[0].y=0.0;                    
		src_point[1].x=0.0; 
		src_point[1].y= (float) img_old->height; src_point[2].x= (float) img_old->Width
		src_point[2].y= (float) img_old->height; 
		src_point[3].x= (float) img_old->width;


		src_point[3].y=0.0;               
		dst_point[0].x=0;

		dst_point[0].y= (float) fabs ((Sin (anglerad) *img_old->width);              
		dst_point[1].x= (float) fabs ((Sin (anglerad) *img_old->height);

		dst_point[1].y= (float) fabs ((Sin (anglerad) *img_old->width) + (float) fabs ((cos (anglerad) *img_old->height));
		dst_point[2].x= (float) fabs ((Sin (anglerad) *img_old->height) + (float) fabs ((cos (anglerad) *img_old->width));

		dst_point[2].y= (float) fabs ((cos (anglerad) *img_old->height));
		dst_point[3].x= (float) fabs ((cos (anglerad) *img_old->width));


		dst_point[3].y=0;            
		float newm[9];
		Cvmat NEWM = Cvmat (3, 3, cv_32f, NEWM);

		Cvwarpperspectiveqmatrix (SRC_POINT,DST_POINT,&AMP;NEWM);
		Cvwarpperspective (img_old,dst,&newm,cv_inter_linear+cv_warp_fill_outliers, cvScalarAll (0)); Cvwarpperspective (Img_old,img_tmp,&newm,cv_inter_linear+cv_wArp_fill_outliers, Cvscalarall (0));
	Method three-perspective transformation}//Cvnamedwindow ("Dst_litter", 1);
	Cvshowimage ("Dst_litter", DST);
	Cvnamedwindow ("Dst_big", 1);
	Cvshowimage ("Dst_big", img_tmp);
return img_tmp; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.