#include
using namespace std;
using namespace cv;
int main()
{
Mat src;
src = imread("test.jpg");
//滤波
Mat dst, dst2;
GaussianBlur(src, dst, Size(9, 9), 0, 0);
medianBlur(src, dst2, 9);
//显示结果
namedWindow("Ori",WINDOW_AUTOSIZE);
imshow("Ori", src);
namedWindow("Gaussian",WINDOW_AUTOSIZE);
imshow("Gaussian", dst);
namedWindow("Median",WINDOW_AUTOSIZE);
imshow("Median", dst2);
waitKey();
return 0;
}
#include
using namespace std;
using namespace cv;
int main()
{
Mat src;
src = imread("test.jpg");
//滤波
Mat dst, dst2;
Sobel(src, dst, -1,1,1);
Laplacian(src, dst2, -1);
//显示结果
namedWindow("Ori",WINDOW_AUTOSIZE);
imshow("Ori", src);
namedWindow("Sobel",WINDOW_AUTOSIZE);
imshow("Sobel", dst);
namedWindow("Laplacian",WINDOW_AUTOSIZE);
imshow("Laplacian", dst2);
waitKey();
return 0;
}
#include
using namespace std;
using namespace cv;
int main()
{
Mat src;
src = imread("test.jpg");
//pyrDown两次
Mat dst, dst2;
pyrDown(src, dst);
pyrDown(dst, dst2);
//显示结果
namedWindow("Ori", WINDOW_AUTOSIZE);
imshow("Ori", src);
namedWindow("1st Pyr", WINDOW_AUTOSIZE);
imshow("1st Pyr", dst);
namedWindow("2nd Pyr", WINDOW_AUTOSIZE);
imshow("2nd Pyr", dst2);
//pyrUp两次
pyrUp(dst2, dst);
pyrUp(dst, src);
//显示结果
namedWindow("New Ori", WINDOW_AUTOSIZE);
imshow("New Ori", dst2);
namedWindow("1st PyrUp", WINDOW_AUTOSIZE);
imshow("1st PyrUp", dst);
namedWindow("2nd PyrUp", WINDOW_AUTOSIZE);
imshow("2nd PyrUp", src);
waitKey();
return 0;
}
#include
using namespace cv;
int main()
{
Mat src,dst;
//读入图片并缩放
src = imread("test.jpg");
resize(src, dst, Size(0, 0), 0.5, 0.5);
//显示
namedWindow("Ori", WINDOW_AUTOSIZE);
imshow("Ori", src);
namedWindow("resize", WINDOW_AUTOSIZE);
imshow("resize", dst);
waitKey();
return 0;
}
#include
using namespace cv;
int main()
{
Mat src,dst;
//读入图片
src = imread("test.jpg");
//平移矩阵
Mat M =Mat::zeros(2, 3, CV_32FC1);
M.at(0, 0) = 1;
M.at(0, 2) = 50; //水平平移量
M.at(1, 1) = 1;
M.at(1, 2) = 150; //竖直平移量
//平移
warpAffine(src,dst,M,src.size());
//显示
namedWindow("Ori", WINDOW_AUTOSIZE);
imshow("Ori", src);
namedWindow("move", WINDOW_AUTOSIZE);
imshow("move", dst);
waitKey();
return 0;
}
#include
using namespace cv;
int main()
{
Mat src,dst;
//读入图片
src = imread("test.jpg");
//旋转矩阵
Mat M = getRotationMatrix2D(Point2f(src.cols / 2, src.rows / 2), 45, 1);
//旋转
warpAffine(src,dst,M,src.size());
//显示
namedWindow("Ori", WINDOW_AUTOSIZE);
imshow("Ori", src);
namedWindow("rotate", WINDOW_AUTOSIZE);
imshow("rotate", dst);
waitKey();
return 0;
}
#include
#include
#define M_PI 3.14159265358979323846
using namespace cv;
int main()
{
Mat src,dst;
//读入图片
src = imread("test.jpg");
//倾斜矩阵
double m = 1 / tan(M_PI/ 3);
Mat M = (Mat_(2,3)<<1,m,0,0,1,0);
//倾斜
warpAffine(src,dst,M,src.size());
//显示
namedWindow("Ori", WINDOW_AUTOSIZE);
imshow("Ori", src);
namedWindow("skewed", WINDOW_AUTOSIZE);
imshow("skewed", dst);
waitKey();
return 0;
}
#include
#include
#define M_PI 3.14159265358979323846
using namespace cv;
int main()
{
Mat src,dst,dsth,dstv;
//读入图片
src = imread("test.jpg");
//水平、垂直、组合反射矩阵
Mat Mh = (Mat_(2, 3) << -1, 0, src.cols, 0, 1, 0);
Mat Mv = (Mat_(2, 3) << 1, 0, 0, 0, -1, src.rows);
Mat M = (Mat_(2, 3) << -1, 0, src.cols, 0, -1, src.rows);
//反射
warpAffine(src,dsth,Mh,src.size());
warpAffine(src,dstv,Mv,src.size());
warpAffine(src,dst,M,src.size());
//显示
namedWindow("Ori", WINDOW_AUTOSIZE);
imshow("Ori", src);
namedWindow("H-reflect", WINDOW_AUTOSIZE);
imshow("H-reflect", dsth);
namedWindow("V-reflect", WINDOW_AUTOSIZE);
imshow("V-reflect", dstv);
namedWindow("reflect", WINDOW_AUTOSIZE);
imshow("reflect", dst);
waitKey();
return 0;
}
getPerspectiveTransform:Mat getPerspectiveTransform(InputArray src, InputArray dst); 或Mat getPerspectiveTransform(const Point2f src[], const Point2f dst[]); 根据输入和输出点获得图像透视变换的矩阵
warpPerspective:void warpPerspective(InputArray src, OutputArray dst, InputArray M, Size dsize, int flags=INTER_LINEAR, int borderMode=BORDER_CONSTANT, const Scalar& borderValue=Scalar())
#include
#include
#define M_PI 3.14159265358979323846
using namespace cv;
int main()
{
Mat src,dst;
//读入图片
src = imread("test.jpg");
//找到透视变换矩阵
Point2f src_verts[4];
src_verts[2] = Point(195, 140);
src_verts[3] = Point(410, 120);
src_verts[1] = Point(220, 750);
src_verts[0] = Point(400, 750);
Point2f dst_verts[4];
dst_verts[2] = Point(160, 100);
dst_verts[3] = Point(530, 120);
dst_verts[1] = Point(220, 750);
dst_verts[0] = Point(400, 750);
Mat M = getPerspectiveTransform(src_verts, dst_verts);
warpPerspective(src, dst, M, src.size());
//显示
namedWindow("Ori", WINDOW_AUTOSIZE);
imshow("Ori", src);
namedWindow("perspective", WINDOW_AUTOSIZE);
imshow("perspective", dst);
waitKey();
return 0;
}
void denoise_TVL1(const std::vector
#include
#include
using namespace cv;
using namespace std;
int main()
{
Mat src,dst;
//读入图片
src = imread("test.jpg");
//人为添加一些噪声
Mat noisy = src.clone();
Mat noise(src.size(), src.type());
randn(noise, 0, 50);
noisy += noise;
cout << "here1" << endl;
//去噪
fastNlMeansDenoisingColored(noisy, dst, 10, 10, 7, 21);
cout << "here2" << endl;
//显示
namedWindow("Ori", WINDOW_AUTOSIZE);
imshow("Ori", src);
namedWindow("Ori_noise", WINDOW_AUTOSIZE);
imshow("Ori_noise", noisy);
namedWindow("denoise", WINDOW_AUTOSIZE);
imshow("denoise", dst);
waitKey();
return 0;
}
《OpenCV 图像处理》Gloria Bueno Garcia、Oscar Deniz Suarez、Jose Luis Espinosa Aranda著,刘冰 翻译,机械工业出版社出版,2016年11月