日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

opencv图像旋转

發布時間:2023/11/27 生活经验 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 opencv图像旋转 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

轉自:http://download.csdn.net/source/2642701

?

/* ?程序名:rotate.c
功能:讀入圖像文件,做圖像旋轉轉,然后顯示圖像在屏幕上
*/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cv.h>
#include <highgui.h>

?

void myRotate(IplImage *img, int x, int y, float degree,int center[2]);


int main(int argc, char *argv[])
{
?? IplImage* img = 0;
?? int height,width,step,channels;
?uchar *data;
?int center[2]={0,0};
?argv[1]="d://a_base_1.jpg";
?//請自己添加圖像文件路徑
?
?img=cvLoadImage(argv[1],1);
?if(!img)
?{
??printf("Could not load image file: %s/n",argv[1]);
??exit(0);
?}
?// 獲取圖像信息
?height??? = img->height;?
?width???? = img->width;?
?step????? = img->widthStep;?
?channels? = img->nChannels;
?data????? = (uchar *)img->imageData;
?printf("Processing a %dx%d image with %d channels/n",height,width,channels);?
?
?// 創建窗口?
?cvNamedWindow("mainWin", CV_WINDOW_AUTOSIZE);
?cvMoveWindow("mainWin", 100, 100);
?
?center[0]=width/2;//這兩句可以設置旋轉中心的坐標
?center[1]=height/2;
?
?// 反轉圖像
?myRotate(img,0,0,-1,center);
?// 顯示圖像
?cvShowImage("mainWin", img );
?cvWaitKey(0);
?cvReleaseImage(&img );
?return 0;
}

?

void myRotate(IplImage *img, int x, int y, float degree,int center[2])
{
?
?double angle = degree? * CV_PI / 180.; // angle in radian
??? double a = sin(angle), b = cos(angle); // sine and cosine of angle
?IplImage* imgSrc=cvCloneImage(img);
?int w_src = imgSrc->width;
?int h_src = imgSrc->height;
?
??? // Make w_dst and h_dst to fit the output image
?//int w_dst = int(h_src * a + w_src * b);
??? //int h_dst = int(w_src * a + h_src * b);
??? //int w_dst = int(h_src * abs(a) + w_src * abs(b));
?// int h_dst = int(w_src * abs(a) + h_src * abs(b));
?
??? // map matrix for WarpAffine, stored in statck array
??? double map[6];
??? CvMat map_matrix = cvMat(2, 3, CV_64FC1, map);
???
??? // Rotation center needed for cv2DRotationMatrix
??? CvPoint2D32f pt = cvPoint2D32f(center[0], center[1]);
??? cv2DRotationMatrix(pt, degree, 1.0, &map_matrix);
?
??? // otherwise you will get only part of the result
??? map[2] +=x;
??? map[5] +=y;
?
??? // We need a destination image?
??? cvWarpAffine(
????????????? imgSrc,
???????????? ?img,
????????????? &map_matrix,
????????????? CV_INTER_LINEAR | CV_WARP_FILL_OUTLIERS,
??????????????cvScalarAll(0)
????????????? );
}

?

轉自:http://www.cnblogs.com/HappyXie/archive/2011/03/02/1969434.html

//OpenCV 4 下的圖像任意角度的旋轉

//需要inter公司的OpenCV的支持.

//OpenCV 4下的圖像任意角度的旋轉
//待旋轉的圖像IplImage* Img_old
//返回的旋轉后圖像 IplImage* Img_tmp.
//旋轉的角度,單位度.
//三種不同的方法.其中方法二沒有完全測試,方法一可以滿足大部分需要
//Vastsky - Nercita?? 2005 6 12?
//vastsky_sun#126.com
IplImage * CCropMeasureView::FitRotate (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);//目的圖像 使用擴展的大小

IplImage* dst = cvCloneImage( Img_old );//目的圖像 與原圖像等大?

????
???? float m[6];???????????
??????? CvMat M = cvMat( 2, 3, CV_32F, m );


?? if(1==method)
?? {
????? //方法一? 提取象素四邊形,使用子象素精度
?????
??? 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
?
???? //方法一? 提取象素四邊形,使用子象素精度
?? }

???????? if(2==method)
?? {
?? //方法二 使用 二維旋轉的仿射變換矩陣 存在問題 要求輸入和輸出圖像一樣大 旋轉中心不對
?
????? 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) );//小圖
?????? //小目標圖像
?

?? //對圖像進行擴展
?? //?? 只能一定角度以內 不同象限的不同對待
?????????? int 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 ; y<Img_old->height; y++) //為了不越界
?? {
?????????? for (int x=0 ; x< Img_old->width; x++)
???? {
??????? old_ptr = &((uchar*)(Img_old->imageData + Img_old->widthStep*y))[(x)*3];
???? temp_ptr = &((uchar*)(Img_tmp->imageData + Img_tmp->widthStep*(y+dy)))[(x+dx)*3];
???? 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 );//生成輸出圖像
?? cvWarpAffine( Img_tmp, temp??? , &M,CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS,cvScalarAll(0) );//大圖
????? Img_tmp=cvCloneImage( temp );
???
?? //問題
?? //cvWarpAffine( Img_tmp, Img_tmp, &M,CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS,cvScalarAll(0) );//大圖
??
?? //方法二 使用 二維旋轉的仿射變換矩陣
?? }

????????? if(3==method)
??? {
???? //方法三 透視變換
????? 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,&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) );
????????
?? //方法三 透視變換
??? }
??
???
?? //? cvNamedWindow( "dst_litter", 1 );
?? //? cvShowImage( "dst_litter", dst );


// cvNamedWindow( "dst_big", 1 );
// cvShowImage( "dst_big", Img_tmp );
??
???? return Img_tmp;

??? }

?

其他參考博客:

1、? http://cau.anzhi.blog.163.com/blog/static/125775520084240205820/

2、 ?http://blog.csdn.net/hunnish/article/details/1370998

?

總結

以上是生活随笔為你收集整理的opencv图像旋转的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

歡迎分享!

轉載請說明來源于"生活随笔",并保留原作者的名字。

本文地址:opencv图像旋转