# OPENCV用户手册之图像处理部分（之二）：采样、差值与几何变换（中文翻译）

10 篇文章 0 订阅

### 采样、差值和几何变换

#### InitLineIterator

int cvInitLineIterator( const CvArr* image, CvPoint pt1, CvPoint pt2,
CvLineIterator* line_iterator, int connectivity=8 );


image

pt1

pt2

line_iterator

connectivity

##### 例子：使用线段迭代器计算彩色线上象素值的和
    CvScalar sum_line_pixels( IplImage* image, CvPoint pt1, CvPoint pt2 )
{
CvLineIterator iterator;
int blue_sum = 0, green_sum = 0, red_sum = 0;
int count = cvInitLineIterator( image, pt1, pt2, &iterator, 8 );

for( int i = 0; i < count; i++ ){
blue_sum += iterator.ptr[0];
green_sum += iterator.ptr[1];
red_sum += iterator.ptr[2];
CV_NEXT_LINE_POINT(iterator);

/* print the pixel coordinates: demonstrates how to calculate the coordinates */
{
int offset, x, y;
/* assume that ROI is not set, otherwise need to take it into account. */
offset = iterator.ptr - (uchar*)(image->imageData);
y = offset/image->widthStep;
x = (offset - y*image->widthStep)/(3*sizeof(uchar) /* size of pixel */);
printf("(%d,%d)/n", x, y );
}
}
return cvScalar( blue_sum, green_sum, red_sum );
}


#### SampleLine

int cvSampleLine( const CvArr* image, CvPoint pt1, CvPoint pt2,
void* buffer, int connectivity=8 );


image

pt1

pt2

buffer

connectivity
The line connectivity, 4 or 8.

#### GetRectSubPix

void cvGetRectSubPix( const CvArr* src, CvArr* dst, CvPoint2D32f center );


src

dst

center

dst(x, y) = src(x + center.x - (width(dst)-1)*0.5, y + center.y - (height(dst)-1)*0.5)


void cvGetQuadrangleSubPix( const CvArr* src, CvArr* dst, const CvMat* map_matrix,
int fill_outliers=0, CvScalar fill_value=cvScalarAll(0) );


src

dst

map_matrix
3 × 2 变换矩阵 [ A | b ] （见讨论）.
fill_outliers

fill_value

dst(x+width(dst)/2, y+height(dst)/2)= src( A11x+A12y+b1, A21x+A22y+b2),

where A and b are taken from map_matrix
| A11 A12  b1 |
map_matrix = |            |
| A21 A22  b2 |


#include "cv.h"
#include "highgui.h"
#include "math.h"

int main( int argc, char** argv )
{
IplImage* src;
/* the first command line parameter must be image file name */
if( argc==2 && (src = cvLoadImage(argv[1], -1))!=0)
{
IplImage* dst = cvCloneImage( src );
int delta = 1;
int angle = 0;

cvNamedWindow( "src", 1 );
cvShowImage( "src", src );

for(;;)
{
float m[6];
double factor = (cos(angle*CV_PI/180.) + 1.1)*3;
CvMat M = cvMat( 2, 3, CV_32F, m );
int w = src->width;
int h = src->height;

m[0] = (float)(factor*cos(-angle*2*CV_PI/180.));
m[1] = (float)(factor*sin(-angle*2*CV_PI/180.));
m[2] = w*0.5f;
m[3] = -m[1];
m[4] = m[0];
m[5] = h*0.5f;

cvGetQuadrangleSubPix( src, dst, &M, 1, cvScalarAll(0));

cvNamedWindow( "dst", 1 );
cvShowImage( "dst", dst );

if( cvWaitKey(5) == 27 )
break;

angle = (angle + delta) % 360;
}
}
return 0;
}


#### Resize

void cvResize( const CvArr* src, CvArr* dst, int interpolation=CV_INTER_LINEAR );


src

dst

interpolation

• CV_INTER_NN - 最近邻差值,
• CV_INTER_LINEAR - 双线性差值 (缺省使用)
• CV_INTER_AREA - 使用象素关系重采样。当图像缩小时候，该方法可以避免波纹出现。当图像放大是，类似于 CV_INTER_NN 方法..
• CV_INTER_CUBIC - 立方差值.

#### WarpAffine

void cvWarpAffine( const CvArr* src, CvArr* dst, const CvMat* map_matrix,
int flags=CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS,
CvScalar fillval=cvScalarAll(0) );


src

dst

map_matrix
2×3 变换矩阵
flags

• CV_WARP_FILL_OUTLIERS - 填充所有缩小图像的象素。如果部分象素落在输入图像的边界外，那么它们的值设定为 fillval.
• CV_WARP_INVERSE_MAP - 指定 matrix 是输出图像到输入图像的反变换，因此可以直接用来做象素差值。否则, 函数从 map_matrix 得到反变换。
fillval

dst(x',y')<-src(x,y)



#### 2DRotationMatrix

CvMat* cv2DRotationMatrix( CvPoint2D32f center, double angle,
double scale, CvMat* map_matrix );


center

angle

scale

map_matrix

[  α  β  |  (1-α)*center.x - β*center.y ]
[ -β  α  |  β*center.x + (1-α)*center.y ]

where α=scale*cos(angle), β=scale*sin(angle)


#### WarpPerspective

void cvWarpPerspective( const CvArr* src, CvArr* dst, const CvMat* map_matrix,
int flags=CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS,
CvScalar fillval=cvScalarAll(0) );


src

dst

map_matrix
3×3 变换矩阵
flags

• CV_WARP_FILL_OUTLIERS - 填充所有缩小图像的象素。如果部分象素落在输入图像的边界外，那么它们的值设定为 fillval.
• CV_WARP_INVERSE_MAP - 指定 matrix 是输出图像到输入图像的反变换，因此可以直接用来做象素差值。否则, 函数从 map_matrix 得到反变换。
fillval

dst(x',y')<-src(x,y)



#### WarpPerspectiveQMatrix

4个对应点计算透视变换矩阵

CvMat* cvWarpPerspectiveQMatrix( const CvPoint2D32f* src,
const CvPoint2D32f* dst,
CvMat* map_matrix );


src

dst

map_matrix

(tix'i,tiy'i,ti)T=matrix•(xi,yi,1)T


where dst(i)=(x'i,y'i), src(i)=(xi,yi), i=0..3.

• 0
点赞
• 1
收藏
觉得还不错? 一键收藏
• 2
评论
01-09 2624
08-26
02-27 3567
03-08 1万+
07-22 5574
03-02 3万+
08-15 4819
05-05 182
10-09 4018
01-04 2万+

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、付费专栏及课程。