OpenCV實現(xiàn)繞圖片中任意角度旋轉(zhuǎn)任意角度_第1頁
OpenCV實現(xiàn)繞圖片中任意角度旋轉(zhuǎn)任意角度_第2頁
OpenCV實現(xiàn)繞圖片中任意角度旋轉(zhuǎn)任意角度_第3頁
OpenCV實現(xiàn)繞圖片中任意角度旋轉(zhuǎn)任意角度_第4頁
OpenCV實現(xiàn)繞圖片中任意角度旋轉(zhuǎn)任意角度_第5頁
全文預(yù)覽已結(jié)束

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)

文檔簡介

第OpenCV實現(xiàn)繞圖片中任意角度旋轉(zhuǎn)任意角度最近在做項目需要把把圖片繞圖片中任意點旋轉(zhuǎn)任意角度,考慮到自己旋轉(zhuǎn)需要編寫插值算法,所以想到了用opencv,但是網(wǎng)上都是圍繞圖片中點旋轉(zhuǎn)任意角度的,都是向下面這樣寫的:

繞圖片中心旋轉(zhuǎn)圖片不裁剪

#include"opencv.hpp"

#includeiostream

usingnamespacestd;

usingnamespacecv;

intmain(){

Matsrc=imread("timg.jpg");

Matdes,m;

Point2fcenter=Point(src.cols/2,src.rows/2);

doubleangle=50,scale=0.5;

intw=src.cols,h=src.rows;

intbound_w=(h*fabs(sin(angle*CV_PI/180))+w*fabs(cos(angle*CV_PI/180)))*scale;

intbound_h=(h*fabs(cos(angle*CV_PI/180))+w*fabs(sin(angle*CV_PI/180)))*scale;

m=getRotationMatrix2D(center,angle,scale);

m.atdouble(0,2)+=(bound_w-src.cols)/2;

m.atdouble(1,2)+=(bound_h-src.rows)/2;

warpAffine(src,des,m,Size2i(bound_h,bound_w));

imshow("image",des);

waitKey();

return0;

旋轉(zhuǎn)之后的效果:

但是遇到繞任意點旋轉(zhuǎn)時,會產(chǎn)生問題,用這種方式還是會存在裁剪,如果要理解繞任意點旋轉(zhuǎn),需要先理解函數(shù)getRotationMatrix2D,這個函數(shù)處理過程如下面矩陣表示所示:

具體實現(xiàn)代碼如下:

Matsrc=imread("/home/sss/1111.jpg",IMREAD_GRAYSCALE);

Matdes,m;

//旋轉(zhuǎn)的任意角度

doubleangle=45;

intw=src.cols,h=src.rows;

Point2frorate_center;

//旋轉(zhuǎn)的任意中心

rorate_center.x=w;

rorate_center.y=h;

//重新計算旋轉(zhuǎn)后的寬和高

intbound_w=ceil(h*fabs(sin(angle*CV_PI/180.0))+w*fabs(cos(angle*CV_PI/180.0)));

intbound_h=ceil(h*fabs(cos(angle*CV_PI/180.0))+w*fabs(sin(angle*CV_PI/180.0)));

m=getRotationMatrix2D(rorate_center,angle,1.0);

//通過eigen計算旋轉(zhuǎn)矩陣

Eigen::Matrix3dT1;

T11,0,-rorate_center.x,

0,1,-rorate_center.y,

0,0,1;

Eigen::Matrix3dT2;

T21,0,rorate_center.x,

0,1,rorate_center.y,

0,0,1;

Eigen::Matrix3drorate;

roratecos(angle*CV_PI/180.0),sin(angle*CV_PI/180.0),0,

-sin(angle*CV_PI/180.0),cos(angle*CV_PI/180.0),0,

0,0,1;

Eigen::Matrix3dT=T2*rorate*T1;

//計算原來矩陣的四個頂點經(jīng)過變換后的頂點

Eigen::Matrixdouble,3,1left_top_p,right_top_p,right_bottom_p,left_botoom_p;

left_top_p0,0,1;

right_top_pw,0,1;

right_bottom_pw,h,1;

left_botoom_p0,h,1;

left_top_p=T*left_top_p;

right_top_p=T*right_top_p;

right_bottom_p=T*right_bottom_p;

left_botoom_p=T*left_botoom_p;

//找到經(jīng)過變換過定位的最大最小值

doublemin_x=10000,min_y=10000;

//min_x

if(left_top_p[0]min_x){

min_x=left_top_p[0];

if(right_top_p[0]min_x){

min_x=right_top_p[0];

if(right_bottom_p[0]min_x)

min_x=right_bottom_p[0];

if(left_botoom_p[0]min_x){

min_x=left_botoom_p[0];

//min_y

if(left_top_p[1]min_y){

min_y=left_top_p[1];

if(right_top_p[1]min_y){

min_y=right_top_p[1];

if(right_bottom_p[1]min_y)

min_y=right_bottom_p[1];

if(left_botoom_p[1]min_y){

min_y=left_botoom_p[1];

doublemax_x=-1000,max_y=-1000;

//max_x

if(left_top_p[0]max_x){

max_x=left_top_p[0];

if(right_top_p[0]max_x){

max_x=right_top_p[0];

if(right_bottom_p[0]max_x)

max_x=right_bottom_p[0];

if(left_botoom_p[0]max_x){

max_x=left_botoom_p[0];

//max_y

if(left_top_p[1]max_y){

max_y=left_top_p[1];

if(right_top_p[1]max_y){

max_y=right_top_p[1];

if(right_bottom_p[1]max_y)

max_y=right_bottom_p[1];

if(left_botoom_p[1]max_y){

max_y=left_botoom_p[1];

//將偏置添加到矩陣中

m.atdouble(0,2)+=-min_x;

m.atdouble(1,2)+=-min_y;

//變換

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論