1、尺寸變換
1.1 圖像差值原理
?文章來源地址http://www.zghlxwxcb.cn/news/detail-524483.html
1.2 圖像縮放、翻轉(zhuǎn)、拼接
1.2.1 圖像縮放
?1.2.2 圖像翻轉(zhuǎn)
1.2.3 圖像拼接
#include <iostream>
#include <fstream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat gray = imread(" lena.png",IMREAD_GRAYSCALE);
Mat sma1lImg,bigImg0,biglmg1,bigImg2;
resize(gray, sma1lImg,Size(15, 15), 0, 0, INTER_AREA);//先將圖像縮小resize(smalllmg,bigImg0,Size(30,30),0,0,INTER_NEAREST);//最近鄰差值resize(smallImg,bigImgl,Size(30,30),0,0,INTER_LINEAR);//雙線性差值resize(smalllmg,bigImg2,Size(30,30),0,0,INTER_CUBIC);//雙三次差值
Mat img_x,img_y,img_xy;
flip(gray,img_x,0);// 沿x軸對稱
flip(gray,img_y,1);//沿y軸對稱
flip(gray,img_xy, -1); //先x軸對稱,再y軸對稱
Mat img00 = imread("lena00.png");
Mat img01 = imread("lena01.png");
Mat img10 = imread("lena10.png") ;
Mat imgl1 = imread("lenal1.png" );
//圖像連接
Mat img,img0,imgl;//圖像橫向連接
hconcat(img00,img01,img0);
hconcat(img10, imgl1, imgl);//橫向連接結(jié)果再進(jìn)行豎向連接
vconcat(img0, imgl, img);
waitKey(0);
system("pause");
return 0;
}
2、仿射變換
2.1 仿射變換原理
?2.2 仿射變換函數(shù)
?2.3 邊界填充方法和對應(yīng)標(biāo)志
?2.4 圖像旋轉(zhuǎn)
?2.5 計(jì)算仿射變換矩陣
?
//仿射變換
int affineConversion(void)
{
Mat img = imread("F:/testMap/lena.png");
if (img.empty())
{
cout << "請確認(rèn)圖像文件名稱是否正確" << endl;
return -1;
}
Mat rotation0, img_warp0;
double angle = 45;//設(shè)置圖像旋轉(zhuǎn)的角度
Size dst_size(img.rows, img.cols);//設(shè)置輸出圖像的尺寸
Point2f center(img.rows/2.0,img.cols/2.0);//設(shè)置圖像的旋轉(zhuǎn)中心
rotation0 = getRotationMatrix2D(center,angle,1);//計(jì)算仿射變換矩陣
warpAffine(img,img_warp0,rotation0,dst_size);//進(jìn)行仿射變換
imshow("img_warp0", img_warp0);
//根據(jù)定義的三個(gè)點(diǎn)進(jìn)行仿射變換
Point2f src_points[3];
Point2f dst_points[3];
src_points[0] = Point2f(0,0);//原始圖像中的三個(gè)點(diǎn)
src_points[1] = Point2f(0,(float)(img.cols - 1));
src_points[2] = Point2f((float)(img.rows - 1),(float)(img.cols - 1));
dst_points[0] = Point2f((float)(img.rows)*0.11,(float)(img.cols)*0.20);//放射變換后圖像中的三個(gè)點(diǎn)
dst_points[1] = Point2f((float)(img.rows)*0.15,(float)(img.cols)*0.70);
dst_points[2] = Point2f((float)(img.rows)*0.81,(float)(img.cols)*0.85);
Mat rotation1,img_warpl;
rotation1 = getAffineTransform(src_points, dst_points);//根據(jù)對應(yīng)點(diǎn)求取仿射變換矩陣
warpAffine(img,img_warpl,rotation1,dst_size);//進(jìn)行仿射變換
imshow("img_warpl", img_warpl);
waitKey(0);
}
3、透視變換
3.1 原理
?3.2 計(jì)算透視變化矩陣函數(shù)
?4個(gè)像素坐標(biāo),圖有誤
3.3 計(jì)算方法標(biāo)志
?3.4 透視變換函數(shù)
文章來源:http://www.zghlxwxcb.cn/news/detail-524483.html
?
//透視變換
int perspectiveConversion(void)
{
Mat img = imread("F:/testMap/二維碼.png");
if (img.empty())
{
cout << "請確認(rèn)圖像文件名稱是否正確" << endl;
return -1;
}
Point2f src_points[4];
Point2f dst_points[4];
//通過Image Watch查看的二維碼四個(gè)角點(diǎn)坐標(biāo)
src_points[0] = Point2f(100.0,401.0);
src_points[1] = Point2f(551.0,403.0);
src_points[2] = Point2f(1.0,676.0);
src_points[3] = Point2f(684.0,678.0);
//期望透視變換后二維碼四個(gè)角點(diǎn)的坐標(biāo)
dst_points[0] = Point2f(0.0,0.0);
dst_points[1] = Point2f(627.0,0.0);
dst_points[2] = Point2f(0.0, 627.0);
dst_points[3] = Point2f(627.0,627.0);
Mat rotation,img_warp;
rotation = getPerspectiveTransform(src_points,dst_points);//計(jì)算透視變換矩陣
warpPerspective(img,img_warp,rotation,img.size());//透視變換投影
imshow("img",img);
imshow("img_warp",img_warp);
waitKey(0);
}
到了這里,關(guān)于圖像尺寸、仿射、透視變換的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!