几何变换通过网格发生形变使图像发生形变。将经过选取的输入像素坐标,通过相应的映射函数处理得到输出像素坐标
O(x,y)=I(fx(x,y),fy(x,y))
内插法
fx(x,y),,fy(x,y)通常为浮点数
外插法
仿射变换:
缩放、平移、旋转、倾斜、反射等
仿射变换 | 变换矩阵 | 关键函数 | 示例 |
缩放 |
@param src input image.
@param dst output image; it has the size dsize (when it is non-zero) or the size computed from
src.size(), fx, and fy; the type of dst is the same as of src.
@param dsize output image size; if it equals zero, it is computed as:
\f[\texttt{dsize = Size(round(fx*src.cols), round(fy*src.rows))}\f]
Either dsize or both fx and fy must be non-zero.
@param fx scale factor along the horizontal axis; when it equals 0, it is computed as
\f[\texttt{(double)dsize.width/src.cols}\f]
@param fy scale factor along the vertical axis; when it equals 0, it is computed as
\f[\texttt{(double)dsize.height/src.rows}\f]
@param interpolation interpolation method, see cv::InterpolationFlags
@sa warpAffine, warpPerspective, remap
*/
void
resize(
InputArray
src
,
OutputArray
dst
,
Size
dsize
,
double
fx
= 0,
double
fy
= 0,
int
interpolation
=
INTER_LINEAR
);
| ||
平移 |
@param src input image.
@param dst output image that has the size dsize and the same type as src .
@param M \f$2\times 3\f$ transformation matrix.
@param dsize size of the output image.
@param flags combination of interpolation methods (see cv::InterpolationFlags) and the optional
flag WARP_INVERSE_MAP that means that M is the inverse transformation (
\f$\texttt{dst}\rightarrow\texttt{src}\f$ ).
@param borderMode pixel extrapolation method (see cv::BorderTypes); 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.
@param borderValue value used in case of a constant border; by default, it is 0.
@sa warpPerspective, resize, remap, getRectSubPix, transform
*/
void
warpAffine(
InputArray
src
,
OutputArray
dst
,
InputArray
M
,
Size
dsize
,
int
flags
=
INTER_LINEAR
,
int
borderMode
=
BORDER_CONSTANT
,
const
Scalar
&
borderValue
=
Scalar
());
| ||
旋转 |
|
The transformation maps the rotation center to itself. If this is not the target, adjust the shift.
@param center Center of the rotation in the source image.
@param angle Rotation angle in degrees. Positive values mean counter-clockwise rotation (the
coordinate origin is assumed to be the top-left corner).
@param scale Isotropic scale factor.
@sa getAffineTransform, warpAffine, transform
*/
Mat
getRotationMatrix2D(
Point2f
center
,
double
angle
,
double
scale
);
| |
倾斜 |
水平倾斜
垂直倾斜
| void warpAffine | |
反射 |
关于X轴旋转
关于Y轴旋转
关于X轴&Y轴旋转
| void warpAffine | |
透视 |
#include "opencv2/opencv.hpp"
#include <iostream>
#include <math.h>
using namespace std;
using namespace cv;
#define PI 3.14159
int main()
{
//read the source image
Mat src;
src = imread("Scale.png" );
//resize
Mat dst;
resize(src, dst, Size(0, 0), 0.5, 0.5);
//translation
Mat dst1;
Mat m = (Mat_ <double>(2, 3) << 1, 0, 200, 0, 1, 150); //translation matrix
warpAffine(src, dst1, m, src.size ());
//rotation
Mat dst2;
Mat m1 = getRotationMatrix2D(Point2f (src.cols / 2, src.rows / 2), 45, 1);
warpAffine(src,dst2, m1, src.size ());
//skew
Mat dst3;
double k=1/tan(PI / 3);
Mat T = (Mat_ <double>(2, 3) << 1, k, 0, 0, 1, 0);
warpAffine(src, dst3, T, Size(src.cols + 0.5*src.cols, src.rows));
//reflection
Mat dsth, dstv, dsthv;
Mat Mh = (Mat_ <double>(2, 3) << -1, 0, src.cols , 0, 1, 0);
Mat Mv = (Mat_ <double>(2, 3) << 1, 0, 0, 0, -1, src.rows);
Mat Mhv = (Mat_ <double>(2, 3) << -1, 0, src.cols , 0, -1, src.rows);
warpAffine(src, dsth, Mh, src.size ());
warpAffine(src, dstv, Mv, src.size ());
warpAffine(src, dsthv, Mhv, src.size ());
//Perspective
Point2f src_verts[4];
src_verts[2] = Point (200, 250);
src_verts[3] = Point (400, 250);
src_verts[1] = Point (300, 350);
src_verts[0] = Point (400, 350);
Point2f dst_verts[4];
dst_verts[2] = Point (195, 255);
dst_verts[3] = Point (410, 267);
dst_verts[1] = Point (330, 320);
dst_verts[0] = Point (450, 300);
Mat dstp;
Mat x = getPerspectiveTransform(src_verts, dst_verts);
warpPerspective(src, dstp, x, src.size ());
//show
namedWindow( "Original", WINDOW_AUTOSIZE );
imshow( "Original", src);
namedWindow( "Scale", WINDOW_AUTOSIZE );
imshow( "Scale", dst);
namedWindow( "Translated", WINDOW_AUTOSIZE );
imshow( "Translated", dst1);
namedWindow( "Rotated", WINDOW_AUTOSIZE );
imshow( "Rotated", dst2);
namedWindow( "Skewed", WINDOW_AUTOSIZE );
imshow( "Skewed", dst3);
namedWindow( "H-Refliction", WINDOW_AUTOSIZE );
imshow( "H-Refliction", dsth);
namedWindow( "V-Refliction", WINDOW_AUTOSIZE );
imshow( "V-Refliction", dstv);
namedWindow( "Refliction", WINDOW_AUTOSIZE );
imshow( "Refliction", dsthv);
namedWindow( "Perspective", WINDOW_AUTOSIZE );
imshow( "Perspective", dstp);
//save image as png
vector<int > params;
params.push_back( IMWRITE_PNG_COMPRESSION);
params.push_back(9);
imwrite( "Scale.png", dst, params);
imwrite( "Translated.png", dst1, params);
imwrite( "Rotated.png", dst2, params);
imwrite( "Skewed.png", dst3, params);
imwrite( "H-Refliction.png", dsth, params);
imwrite( "V-Refliction.png", dstv, params);
imwrite( "Refliction.png", dsthv, params);
imwrite( "Perspective.png", dstp, params);
//wait
waitKey();
return 0;
}
图像修复
inpaint()
对于视频的修复可以将视频看作一个图像序列,并对序列中的所有图像进行修复处理
略
Mat
src;
src
=
imread(
"bird.png"
);
Mat
mask;
//Converts an image from one color space to another.
cvtColor(src, mask,
COLOR_RGB2GRAY
);
threshold(mask, mask, 235, 235,
THRESH_BINARY
);
//inpainting
Mat
dst, dst2;
inpaint(src, mask, dst, 10,
INPAINT_TELEA
);
inpaint(src, mask, dst2, 10,
INPAINT_NS
);
//show
namedWindow(
"Original"
,
WINDOW_AUTOSIZE
);
imshow(
"Original"
, src);
namedWindow(
"Mask"
,
WINDOW_AUTOSIZE
);
imshow(
"Mask"
, mask);
namedWindow(
"Inpaint_telea"
,
WINDOW_AUTOSIZE
);
imshow(
"Inpaint_telea"
, dst);
namedWindow(
"Inpaint_ns"
,
WINDOW_AUTOSIZE
);
imshow(
"Inpaint_ns"
, dst2);
去噪处理:对从模拟设备或者数字设备所获取的信号进行噪声去除的过程
void fastN1MeansDenoising()
void fastN1MeansDenoisingColored()
void fastN1MeansDenoisingMulti()
void fastN1MeansDenoisingColoredMulti()
void denoise_TVL1()
// 去噪.cpp : 定义 DLL 应用程序的导出函数。
//
#include "stdafx.h"
#include <iostream>
#include "opencv2/opencv.hpp"
#include "opencv.hpp"
using namespace std;
using namespace cv;
int main()
{
//read the source image
Mat src;
src = imread("killer.png" );
//add some noisy
Mat noisy = src.clone();
Mat noise(src.size() , src.type());
randn(noise, 0, 50);
noisy = noise + noisy;
//denoising
Mat dst;
fastNlMeansDenoisingColored(noisy, dst, 30, 30, 7, 21);
//show
namedWindow( "Src", WINDOW_AUTOSIZE );
imshow( "Src", src);
namedWindow( "Noise", WINDOW_AUTOSIZE );
imshow( "Noise", noise);
namedWindow( "Original with noisy", WINDOW_AUTOSIZE );
imshow( "Original with noisy", noisy);
namedWindow( "Denoised", WINDOW_AUTOSIZE );
imshow( "Denoised", dst);
//save image as png
vector<int > params;
params.push_back( IMWRITE_PNG_COMPRESSION);
params.push_back(9);
//save
imwrite( "Denoised", dst, params);
imwrite( "Noise", noise, params);
imwrite( "Original with noisy", noisy, params);
//wait
waitKey();
return 0;
}