首页 > 代码库 > 仿射变换详解 warpAffine
仿射变换详解 warpAffine
今天遇到一个问题是关于仿射变换的,但是由于没有将仿射变换的具体原理型明白,看别人的代码看的很费解,最后终于在师兄的帮助下将原理弄明白了,我觉得最重要的是理解仿射变换可以看成是几种简单变换的复合实现,
具体实现形式即将几种简单变换的变换矩阵M相乘,这样就很容易理解啦
定义:仿射变换的功能是从二维坐标到二维坐标之间的线性变换,且保持二维图形的“平直性”和“平行性”。仿射变换可以通过一系列的原子变换的复合来实现,包括平移,缩放,翻转,旋转和剪切。
这类变换可以用一个3*3的矩阵M来表示,其最后一行为(0,0,1)。该变换矩阵将原坐标为(x,y)变换为新坐标(x‘,y‘),
即
opencv中相应的函数是:
void warpAffine(InputArray src, OutputArray dst, InputArray M, Size dsize, int flags=INTER_LINEAR, int borderMode=BORDER_CONSTANT, const Scalar& borderValue=Scalar())?
Parameters:
- src – input image.
- dst – output image that has the size dsize and the same type as src .
- M – transformation matrix,最重要的东东了,本文中着重讲M的构造
- dsize – size of the output image.ansformation ( ).
- borderMode – pixel extrapolation method (see borderInterpolate()); when borderMode=BORDER_TRANSPARENT , it means that the pixels in the destination image corresponding to the “outliers” in the source image are not modified by the function.
- borderValue – value used in case of a constant border; by default, it is 0.
下面介绍一些典型的仿射变换:
(1)平移,将每一点移到到(x+t , y+t),变换矩阵为
(2)缩放变换 将每一点的横坐标放大或缩小sx倍,纵坐标放大(缩小)到sy倍,变换矩阵为
(3)旋转变换原点:目标图形围绕原点顺时针旋转Θ 弧度,变换矩阵为
(4) 旋转变换 :目标图形以(x , y )为轴心顺时针旋转θ弧度,变换矩阵为
相当于两次平移与一次原点旋转变换的复合,即先将轴心(x,y)移到到原点,然后做旋转变换,最后将图片的左上角置为图片的原点,即
有的人可能会说为什么这么复杂呢,那是因为在opencv的图像处理中,所有对图像的处理都是从原点进行的,而图像的原点默认为图像的左上角,而我们对图像作旋转处理时一般以图像的中点为轴心,因此就需要做如下处理
如果你觉得这样很麻烦,可以使用opencv中自带的Mat getRotationMatrix2D(Point2f center, double angle, double scale)函数获得变换矩阵M,
center:旋转中心
angle:旋转弧度,一定要将角度转换成弧度
scale:缩放尺度
它得到的矩阵是:
其中α = scale * cos( angle ) , β = scale * sing( angle ) , ( center.x , center.y ) 表示旋转轴心
但是不得不说opencv的文档以及相关书籍中都把这个矩阵写错了,如下:
建议大家自己通过下式验证一下,即首先将轴心(x,y)移到原点,然后做旋转平绽放变换,最后再将图像的左上角转换为原点
没有去研究该函数的源码,不晓得源码中到底怎么写的,但是在别人的博客中看到这个函数貌似需要修正
opencv中还有一个函数:Mat getAffineTransform(InputArray src, InputArray dst)?
它通过三组点对就可以获得它们之间的仿射变换,如果我们在一组图像变换中知道变换后的三组点,那么我们就可以利用该函数求得变换矩阵,然后对整张图片进行仿射变换
还有一种与仿射变换经常混淆的变换为透视变换,透视变换需要四组点对才能确定变换矩阵,由于仿射变换保持“平直性”与“平行性”,因此只需要三组点对,而透视变换没有这种约束,故需要四组点对
warpPerspective函数
主要作用:对图像进行透视变换,就是变形
函数的调用形式:
C++: void warpPerspective(InputArray src, OutputArray dst, InputArray M, Size dsize, int flags=INTER_LINEAR, int borderMode=BORDER_CONSTANT, const Scalar& borderValue=Scalar())
参数详解:
InputArray src:输入的图像
OutputArray dst:输出的图像
InputArray M:透视变换的矩阵
Size dsize:输出图像的大小
int flags=INTER_LINEAR:输出图像的插值方法,
combination of interpolation methods (INTER_LINEAR or INTER_NEAREST) and the optional flagWARP_INVERSE_MAP, that sets M as the inverse transformation ( )
int borderMode=BORDER_CONSTANT:图像边界的处理方式
const Scalar& borderValue=http://www.mamicode.com/Scalar():边界的颜色设置,一般默认是0
函数原理:
透视变换(Perspective Transformation)是将图片投影到一个新的视平面(Viewing Plane),也称作投影映射(Projective Mapping)。通用的变换公式为:
u,v是原始图片左边,对应得到变换后的图片坐标x,y,其中。
变换矩阵可以拆成4部分,表示线性变换,比如scaling,shearing和ratotion。用于平移,产生透视变换。所以可以理解成仿射等是透视变换的特殊形式。经过透视变换之后的图片通常不是平行四边形(除非映射视平面和原来平面平行的情况)。
重写之前的变换公式可以得到:
所以,已知变换对应的几个点就可以求取变换公式。反之,特定的变换公式也能新的变换后的图片。简单的看一个正方形到四边形的变换:
变换的4组对应点可以表示成:
根据变换公式得到:
定义几个辅助变量:
都为0时变换平面与原来是平行的,可以得到:
不为0时,得到:
求解出的变换矩阵就可以将一个正方形变换到四边形。反之,四边形变换到正方形也是一样的。于是,我们通过两次变换:四边形变换到正方形+正方形变换到四边形就可以将任意一个四边形变换到另一个四边形。
- #include<cv.h>
- #include<highgui.h>
- #pragma comment(lib, "cv.lib")
- #pragma comment(lib, "cxcore.lib")
- #pragma comment(lib, "highgui.lib")
- int main()
- {
- CvPoint2D32f srcTri[4], dstTri[4];
- CvMat* warp_mat = cvCreateMat (3, 3, CV_32FC1);
- IplImage* src = NULL;
- IplImage* dst = NULL;
- src = cvLoadImage ("test.png", 1);
- dst = cvCloneImage (src);
- dst->origin = src->origin;
- cvZero (dst);
- srcTri[0].x = 0;
- srcTri[0].y = 0;
- srcTri[1].x = src->width - 1;
- srcTri[1].y = 0;
- srcTri[2].x = 0;
- srcTri[2].y = src->height - 1;
- srcTri[3].x = src->width - 1;
- srcTri[3].y = src->height - 1;
- dstTri[0].x = src->width * 0.05;
- dstTri[0].y = src->height * 0.33;
- dstTri[1].x = src->width * 0.9;
- dstTri[1].y = src->height * 0.25;
- dstTri[2].x = src->width * 0.2;
- dstTri[2].y = src->height * 0.7;
- dstTri[3].x = src->width * 0.8;
- dstTri[3].y = src->height * 0.9;
- cvGetPerspectiveTransform (srcTri, dstTri, warp_mat);
- cvWarpPerspective (src, dst, warp_mat);
- cvNamedWindow("src", 1);
- cvShowImage("src", src);
- cvNamedWindow ("Affine_Transform", 1);
- cvShowImage ("Affine_Transform", dst);
- cvWaitKey (0);
- cvReleaseImage (&src);
- cvReleaseImage (&dst);
- cvReleaseMat (&warp_mat);
- return 0;
- }
今天遇到一个问题是关于仿射变换的,但是由于没有将仿射变换的具体原理型明白,看别人的代码看的很费解,最后终于在师兄的帮助下将原理弄明白了,我觉得最重要的是理解仿射变换可以看成是几种简单变换的复合实现,
具体实现形式即将几种简单变换的变换矩阵M相乘,这样就很容易理解啦
定义:仿射变换的功能是从二维坐标到二维坐标之间的线性变换,且保持二维图形的“平直性”和“平行性”。仿射变换可以通过一系列的原子变换的复合来实现,包括平移,缩放,翻转,旋转和剪切。
这类变换可以用一个3*3的矩阵M来表示,其最后一行为(0,0,1)。该变换矩阵将原坐标为(x,y)变换为新坐标(x‘,y‘),
即
opencv中相应的函数是:
void warpAffine(InputArray src, OutputArray dst, InputArray M, Size dsize, int flags=INTER_LINEAR, int borderMode=BORDER_CONSTANT, const Scalar& borderValue=Scalar())?
Parameters:
- src – input image.
- dst – output image that has the size dsize and the same type as src .
- M – transformation matrix,最重要的东东了,本文中着重讲M的构造
- dsize – size of the output image.
- flags – combination of interpolation methods (see resize() ) and the optional flag WARP_INVERSE_MAP that means that M is the inverse transformation ( ).
- borderMode – pixel extrapolation method (see borderInterpolate()); when borderMode=BORDER_TRANSPARENT , it means that the pixels in the destination image corresponding to the “outliers” in the source image are not modified by the function.
- borderValue – value used in case of a constant border; by default, it is 0.
下面介绍一些典型的仿射变换:
(1)平移,将每一点移到到(x+t , y+t),变换矩阵为
(2)缩放变换 将每一点的横坐标放大或缩小sx倍,纵坐标放大(缩小)到sy倍,变换矩阵为
(3)旋转变换原点:目标图形围绕原点顺时针旋转Θ 弧度,变换矩阵为
(4) 旋转变换 :目标图形以(x , y )为轴心顺时针旋转θ弧度,变换矩阵为
相当于两次平移与一次原点旋转变换的复合,即先将轴心(x,y)移到到原点,然后做旋转变换,最后将图片的左上角置为图片的原点,即
有的人可能会说为什么这么复杂呢,那是因为在opencv的图像处理中,所有对图像的处理都是从原点进行的,而图像的原点默认为图像的左上角,而我们对图像作旋转处理时一般以图像的中点为轴心,因此就需要做如下处理
如果你觉得这样很麻烦,可以使用opencv中自带的Mat getRotationMatrix2D(Point2f center, double angle, double scale)函数获得变换矩阵M,
center:旋转中心
angle:旋转弧度,一定要将角度转换成弧度
scale:缩放尺度
它得到的矩阵是:
opencv中还有一个函数:Mat getAffineTransform(InputArray src, InputArray dst)?
它通过三组点对就可以获得它们之间的仿射变换,如果我们在一组图像变换中知道变换后的三组点,那么我们就可以利用该函数求得变换矩阵,然后对整张图片进行仿射变换
还有一种与仿射变换经常混淆的变换为透视变换,透视变换需要四组点对才能确定变换矩阵,由于仿射变换保持“平直性”与“平行性”,因此只需要三组点对,而透视变换没有这种约束,故需要四组点对
本文将openCV中的RANSAC代码全部挑选出来,进行分析和讲解,以便大家更好的理解RANSAC算法。代码我都试过,可以直接运行。
在计算机视觉和图像处理等很多领域,都需要用到RANSAC算法。openCV中也有封装好的RANSAC算法,以便于人们使用。关于RANSAC算法的一些应用,可以看我的另一篇博客:
利用SIFT和RANSAC算法(openCV框架)实现物体的检测与定位,并求出变换矩阵(findFundamentalMat和findHomography的比较)
但是前几天师弟在使用openCV自带的RANSAC算法时,发现实验的运行时间并不会随着输入数据的增加而增加,感觉和理论上的不太相符。所以我就花了点时间,把openCV中关于RANSAC的源代码全部复制出来研究了一下。以便我们更加清晰的了解RANSAC算法的实际运行过程。
首先看两个类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | //模型估计的基类,提供了估计矩阵的各种虚函数 //置信度设为0。99 循环次数设置为了2000 class CvModelEstimator2 { public : CvModelEstimator2( int _modelPoints, CvSize _modelSize, int _maxBasicSolutions); virtual ~CvModelEstimator2(); virtual int runKernel( const CvMat* m1, const CvMat* m2, CvMat* model )= 0 ; //virtual bool runLMeDS( const CvMat* m1, const CvMat* m2, CvMat* model, // CvMat* mask, double confidence=0.99, int maxIters=2000 ); virtual bool runRANSAC( const CvMat* m1, const CvMat* m2, CvMat* model, CvMat* mask, double threshold, double confidence= 0.99 , int maxIters= 2000 ); virtual bool refine( const CvMat*, const CvMat*, CvMat*, int ) { return true ; } //virtual void setSeed( int64 seed ); protected : virtual void computeReprojError( const CvMat* m1, const CvMat* m2, const CvMat* model, CvMat* error ) = 0 ; virtual int findInliers( const CvMat* m1, const CvMat* m2, const CvMat* model, CvMat* error, CvMat* mask, double threshold ); virtual bool getSubset( const CvMat* m1, const CvMat* m2, CvMat* ms1, CvMat* ms2, int maxAttempts= 1000 ); virtual bool checkSubset( const CvMat* ms1, int count ); CvRNG rng; int modelPoints; CvSize modelSize; int maxBasicSolutions; bool checkPartialSubsets; }; //单应矩阵估计的子类 class CvHomographyEstimator : public CvModelEstimator2 { public : CvHomographyEstimator( int modelPoints ); virtual int runKernel( const CvMat* m1, const CvMat* m2, CvMat* model ); virtual bool refine( const CvMat* m1, const CvMat* m2, CvMat* model, int maxIters ); protected : virtual void computeReprojError( const CvMat* m1, const CvMat* m2, const CvMat* model, CvMat* error ); }; |
CvHomographyEstimator继承自CvModelEstimator2,同样的,从名字也就可以看出,这个类使用来估计单应矩阵的。
接下来是两个类的构造函数和析构函数,这个没啥好说的了,基本都是默认的。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 | 本范例的代码主要都是 学习OpenCV——通过KeyPoints进行目标定位这篇博客提供的,然后在它的基础上稍加修改,检测keypoints点的检测器是SURF,获取描述子也是用到SURF来描述,而用到的匹配器是FlannBased,匹配的方式是Knn方式,最后通过findHomography寻找单映射矩阵,perspectiveTransform获得最终的目标,在这个过程中还通过单映射矩阵来进一步去除伪匹配,这里只是贴出代码和代码解析,至于原理还没弄得特别明白,希望接下来可以继续学习,学懂了算法原理再来补充。 1、代码实现[cpp] view plain copy
|
仿射变换详解 warpAffine