OpenCV 版本:4.10.0
开源计算机视觉库
正在加载...
正在搜索...
没有找到
samples/cpp/image_alignment.cpp

使用图像对齐ECC算法的示例

/*
* 本示例演示了使用 findTransformECC 函数,该函数实现了图像对齐ECC算法
* 演示将从默认的图像 fruits.jpg 加载一张图像,然后基于指定的运动类型创建
*
*
* 一个模板图像。如果提供了两张图像,则第一张是输入图像,第二张是模板图像。
* 在后一种情况下,您还可以解析warp的初始化参数。
* 输入和输出warp文件包含了原始的图像变换元素
* 作者:G. Evangelidis, INRIA, Grenoble, France
*
M. Asbach, Fraunhofer IAIS, St. Augustin, Germany
*
包含头文件:
#include <opencv2/imgcodecs.hpp>
*/
#include <opencv2/highgui.hpp>
#include <opencv2/video.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/core/utility.hpp>
#include <cstdio>
#include <
#include <time.h>
#include <iostream>
#include <fstream>
使用命名空间:
using namespace cv;
using namespace std;
静态函数 help(const char** argv);
静态函数 readWarp(string iFilename, Mat& warp, int motionType);
静态函数 saveWarp(string fileName, const Mat& warp, int motionType);
静态函数 draw_warped_roi(Mat& image, const int width, const int height, Mat& W);
#define HOMO_VECTOR(H, x, y)
H.at(0,0) = (float)(x);
H.at(1,0) = (float)(y);
H.at(2,0) = 1.;
#define GET_HOMO_VALUES(X, x, y)
(x) = static_cast (X.at(0,0)/X.at(2,0));
(y) = static_cast (X.at(1,0)/X.at(2,0));
const std::string keys =
"{@inputImage | fruits.jpg | 输入图像文件名 }"
"{@templateImage | | 模板图像文件名(可选)}"
"{@inputWarp | | 输入 warp(矩阵)文件名(可选)}"
"{n numOfIter | 50 | ECC 迭代次数 }"
"{e epsilon | 0.0001 | ECC 收敛容差 }"
"{o outputWarp | outWarp.ecc | 输出 warp(矩阵)文件名 }"
"{m motionType | affine | 运动类型(平移、欧几里得、仿射、单应性) }"
"{v verbose | 1 | 显示初始和最终图像 }"
"{w warpedImfile | warpedECC.png | 变换后的输入图像 }"
"{h help | | 显示帮助信息 }"
;
static void help(const char** argv)
{
cout << "\n本文件演示了 ECC 图像对齐算法的使用。当提供一个图像时,
模板图像会通过随机变换产生。当提供两个图像时,第一个图像是输入图像,
"存在时,可以通过命令行解析对光场进行初始化。 "
"如果输入的光场(inputWarp)缺失,则使用单位变换初始化算法。 \n" << endl;
cout << "\n使用示例(单个图像): \n"
<< argv[0]
<< " fruits.jpg -o=outWarp.ecc "
"-m=euclidean -e=1e-6 -N=70 -v=1 \n" << endl;
cout << "\n使用示例(两个图像,包含初始化): \n"
<< argv[0]
<< " yourInput.png yourTemplate.png "
"yourInitialWarp.ecc -o=outWarp.ecc -m=homography -e=1e-6 -N=70 -v=1 -w=yourFinalImage.png \n" << endl;
}
static int readWarp(string iFilename, Mat& warp, int motionType){
// 从文件中读取指定数量的原始值
// 坐标变换时9个值,否则为6个
int numOfElements;
if (motionType==MOTION_HOMOGRAPHY)
numOfElements=9;
else
numOfElements=6;
int i;
int ret_value;
ifstream myfile(iFilename.c_str());
if (myfile.is_open()){
float* matPtr = warp.ptr<float>(0);
for(i=0; i<numOfElements; i++){
myfile >> matPtr[i];
}
ret_value = 1;
}
else {
cout << "无法打开文件 " << iFilename.c_str() << endl;
ret_value = 0;
}
return ret_value;
}
static int saveWarp(string fileName, const Mat& warp, int motionType)
{
// 将原始矩阵元素保存到文件中
const float* matPtr = warp.ptr<float>(0);
int ret_value;
ofstream outfile(fileName.c_str());
if( !outfile ) {
cerr << "错误在保存 "
<< "无法打开文件 '" << fileName.c_str() << "'!" << endl;
ret_value = 0;
}
else {//保存光场的元素
outfile << matPtr[0] << " " << matPtr[1] << " " << matPtr[2] << endl;
outfile << matPtr[3] << " " << matPtr[4] << " " << matPtr[5] << endl;
if (motionType==MOTION_HOMOGRAPHY){
outfile << matPtr[6] << " " << matPtr[7] << " " << matPtr[8] << endl;
}
ret_value = 1;
}
return ret_value;
}
static void draw_warped_roi(Mat& image, const int width, const int height, Mat& W)
{
Point2f top_left, top_right, bottom_left, bottom_right;
Mat H = Mat (3, 1, CV_32F);
Mat U = Mat (3, 1, CV_32F);
Mat warp_mat = Mat::eye (3, 3, CV_32F);
for (int y = 0; y < W.rows; y++)
for (int x = 0; x < W.cols; x++)
变焦麻数.at<float>(y,x) = W.at<float>(y,x);
// 变换矩形的角落
// 顶部左角
HOMO_VECTOR(H, 1, 1);
gemm(变焦麻数, H, 1, 0, 0, U);
GET_HOMO_VALUES(U, top_left.x, top_left.y);
// 顶部右角
HOMO_VECTOR(H, 宽度, 1);
gemm(变焦麻数, H, 1, 0, 0, U);
GET_HOMO_VALUES(U, top_right.y);
// 底部左角
HOMO_VECTOR(H, 1, 高度);
gemm(变焦麻数, H, 1, 0, 0, U);
GET_HOMO_VALUES(U, bottom_left.y);
// 底部右角
HOMO_VECTOR(H, 宽度, 高度);
gemm(变焦麻数, H, 1, 0, 0, U);
GET_HOMO_VALUES(U, bottom_right.y);
// 绘制变换周界
line(image, top_left, top_right, Scalar(255));
line(image, top_right, bottom_right, Scalar(255));
line(image, bottom_right, bottom_left, Scalar(255));
line(image, bottom_left, top_left, Scalar(255));
}
int main (const int argc, const char * argv[])
{
CommandLineParser parser(argc, argv, keys);
parser.about("ECC demo");
parser.printMessage();
帮助(argv);
string imgFile = parser.get<string>(0);
string tempImgFile = parser.get<string>(1);
string inWarpFile = parser.get<string>(2);
int number_of_iterations = parser.get<int>("n");
double termination_eps = parser.get<double>("e");
string warpType = parser.get<string>("m");
int verbose = parser.get<int>("v");
string finalWarp = parser.get<string>("o");
string warpedImFile = parser.get<string>("w");
if (!parser.check())
{
parser.printErrors();
return -1;
}
if (!(warpType == "translation" || warpType == "euclidean"
|| warpType == "affine" || warpType == "homography"))
{
cerr << "无效的运动变换" << endl;
return -1;
}
int mode_temp;
if (warpType == "translation")
mode_temp = MOTION_TRANSLATION;
else if (warpType == "euclidean")
mode_temp = MOTION_EUCLIDEAN;
else if (warpType == "affine")
mode_temp = MOTION_AFFINE;
else
mode_temp = MOTION_HOMOGRAPHY;
Mat inputImage = imread(samples::findFile(imgFile), IMREAD_GRAYSCALE);
if (inputImage.empty())
{
cerr << "无法加载 inputImage" << endl;
return -1;
}
Mat target_image;
Mat template_image;
if (tempImgFile!="") {
inputImage.copyTo(target_image);
template_image = imread(samples::findFile(tempImgFile), IMREAD_GRAYSCALE);
if (template_image.empty()){
cerr << "无法加载模板图像" << endl;
return -1;
}
}
else{ //对输入图像应用随机变换
resize(inputImage, target_image, Size(216, 216), 0, 0, INTER_LINEAR_EXACT);
Mat warpGround;
RNG rng(getTickCount());
double angle;
switch (mode_temp) {
warpGround = (Mat_<float>(2,3) << 1, 0, (rng.uniform(10.f, 20.f)),
0, 1, (rng.uniform(10.f, 20.f)));
warpAffine(target_image, template_image, warpGround,
Size(200,200), INTER_LINEAR + WARP_INVERSE_MAP);
break;
angle = CV_PI/30 + CV_PI*rng.uniform((double)-2.f, (double)2.f)/180;
warpGround = (Mat_<float>(2,3) << cos(angle), -sin(angle), (rng.uniform(10.f, 20.f)),
sin(angle), cos(angle), (rng.uniform(10.f, 20.f)));
warpAffine(target_image, template_image, warpGround,
Size(200,200), INTER_LINEAR + WARP_INVERSE_MAP);
break;
warpGround = (Mat_<float>(2,3) << (1-rng.uniform(-0.05f, 0.05f)),
(rng.uniform(-0.03f, 0.03f)), (rng.uniform(10.f, 20.f)),
(rng.uniform(-0.03f, 0.03f)), (1-rng.uniform(-0.05f, 0.05f)),
(rng.uniform(10.f, 20.f)));
warpAffine(target_image, template_image, warpGround,
Size(200,200), INTER_LINEAR + WARP_INVERSE_MAP);
break;
warpGround = (Mat_<float>(3,3) << (1-rng.uniform(-0.05f, 0.05f)),
(rng.uniform(-0.03f, 0.03f)), (rng.uniform(10.f, 20.f)),
(rng.uniform(-0.03f, 0.03f)), (1-rng.uniform(-0.05f, 0.05f)),(rng.uniform(10.f, 20.f)),
(rng.uniform(0.0001f, 0.0003f)), (rng.uniform(0.0001f, 0.0003f)), 1.f);
warpPerspective(target_image, template_image, warpGround,
Size(200,200), INTER_LINEAR + WARP_INVERSE_MAP);
break;
}
}
const int warp_mode = mode_temp;
// initialize or load the warp matrix
Mat warp_matrix;
if (warpType == "homography")
warp_matrix = Mat::eye(3, 3, CV_32F);
else
warp_matrix = Mat::eye(2, 3, CV_32F);
if (inWarpFile!=""){
int readflag = readWarp(inWarpFile, warp_matrix, warp_mode);
if ((!readflag) || warp_matrix.empty())
{
cerr << "-> Check warp initialization file" << endl << flush;
return -1;
}
}
else {
printf("\n ->Performance Warning: Identity warp ideally assumes images of "
"similar size. If the deformation is strong, the identity warp may not "
"be a good initialization. \n");
}
if (number_of_iterations > 200)
cout << "-> Warning: too many iterations " << endl;
if (warp_mode != MOTION_HOMOGRAPHY)
warp_matrix.rows = 2;
// start timing
const double tic_init = (double) getTickCount ();
double cc = findTransformECC (template_image, target_image, warp_matrix, warp_mode,
TermCriteria (TermCriteria::COUNT+TermCriteria::EPS,
number_of_iterations, termination_eps));
if (cc == -1)
{
cerr << "The execution was interrupted. The correlation value is going to be minimized." << endl;
cerr << "Check the warp initialization and/or the size of images." << endl << flush;
}
// end timing
const double toc_final = (double) getTickCount ();
const double total_time = (toc_final-tic_init)/(getTickFrequency());
if (verbose){
cout << "Alignment time (" << warpType << " transformation): "
<< total_time << " sec" << endl << flush;
输出最终相关性: "Final correlation: " << cc << endl << flush;
}
保存最终的仿射变换矩阵
saveWarp(finalWarp, warp_matrix, warp_mode);
if (verbose){
输出: "\nThe final warp has been saved in the file: " << finalWarp << endl << flush;
}
保存最终的图像变形结果
Mat warped_image = Mat(template_image.rows, template_image.cols, CV_32FC1);
if (warp_mode != MOTION_HOMOGRAPHY)
warpAffine(target_image, warped_image, warp_matrix, warped_image.size(),
INTER_LINEAR + WARP_INVERSE_MAP);
else
warpPerspective(target_image, warped_image, warp_matrix, warped_image.size(),
INTER_LINEAR + WARP_INVERSE_MAP);
保存变形后的图像
imwrite(warpedImFile, warped_image);
显示结果图像
if (verbose)
{
输出: "The warped image has been saved in the file: " << warpedImFile << endl << flush;
namedWindow("image", WINDOW_AUTOSIZE);
namedWindow("template", WINDOW_AUTOSIZE);
namedWindow("warped image", WINDOW_AUTOSIZE);
namedWindow("error (black: no error)", WINDOW_AUTOSIZE);
moveWindow("image", 20, 300);
moveWindow("template", 300, 300);
moveWindow("warped image", 600, 300);
moveWindow("error (black: no error)", 900, 300);
绘制对应的区域边界
Mat identity_matrix = Mat::eye(3,3,CV_32F);
draw_warped_roi(target_image, template_image.cols-2, template_image.rows-2, warp_matrix);
draw_warped_roi(template_image, template_image.cols-2, template_image.rows-2, identity_matrix);
Mat errorImage;
subtract(template_image, warped_image, errorImage);
double max_of_error;
minMaxLoc(errorImage, NULL, &max_of_error);
展示图像
输出: "Press any key to exit the demo (you might need to click on the images before)." << endl << flush;
imshow("image", target_image);
waitKey (200);
imshow("template", template_image);
waitKey (200);
imshow("warped image", warped_image);
waitKey(200);
imshow ("error (black: no error)", abs(errorImage)*255/max_of_error);
waitKey(0);
}
// 完成
return 0;
}
用于命令行解析。
定义 utility.hpp:820
从Mat派生出的模板矩阵类。
定义 mat.hpp:2230
n维稠密数组类
定义 mat.hpp:812
MatSize 大小
定义 mat.hpp:2160
void copyTo(OutputArray m) const
将矩阵复制到另一个矩阵。
uchar * ptr(int i0=0)
返回指向指定矩阵行的指针。
_Tp & at(int i0=0)
返回对指定数组元素的引用。
int cols
定义 mat.hpp:2138
bool empty() const
如果数组没有元素则返回true。
int rows
行数和列数;当矩阵维度超过2时为(-1, -1)
定义 mat.hpp:2138
int type() const
返回矩阵元素的类型。
_Tp y
点的y坐标
定义 types.hpp:202
_Tp x
点的x坐标
定义 types.hpp:201
随机数生成器
定义 core.hpp:2889
用于指定图像或矩形大小的模板类。
定义 types.hpp:335
定义迭代算法的终止准则的类。
定义 types.hpp:886
void subtract(InputArray src1, InputArray src2, OutputArray dst, InputArray mask=noArray(), int dtype=-1)
计算两个数组或数组与标量的每元素差。
void minMaxLoc(InputArray src, double *minVal, double *maxVal=0, Point *minLoc=0, Point *maxLoc=0, InputArray mask=noArray())
在数组中查找全局最小值和最大值。
void gemm(InputArray src1, InputArray src2, double alpha, InputArray src3, double beta, OutputArray dst, int flags=0)
执行广义矩阵乘法。
#define CV_32FC1
定义 interface.h:118
#define CV_32F
定义 interface.h:78
softfloat abs(softfloat a)
绝对值。
定义 softfloat.hpp:444
#define CV_PI
定义 cvdef.h:380
double getTickFrequency()
返回每秒钟的滴答次数。
int64 getTickCount()
返回滴答次数。
#define CV_Assert(expr)
在运行时检查条件,如果失败则抛出异常。
定义 base.hpp:342
Quat< T > cos(const Quat< T > &q)
Quat< T > sin(const Quat< T > &q)
void imshow(const String &winname, InputArray mat)
在指定的窗口中显示一个图像。
int waitKey(int delay=0)
等待按键。
void namedWindow(const String &winname, int flags=WINDOW_AUTOSIZE)
创建一个窗口。
void moveWindow(const String &winname, int x, int y)
将窗口移动到指定的位置。
CV_EXPORTS_W bool imwrite(const String &filename, InputArray img, const std::vector< int > &params=std::vector< int >())
将图像保存到指定的文件。
CV_EXPORTS_W Mat imread(const String &filename, int flags=IMREAD_COLOR)
从文件中加载图像。
void line(InputOutputArray img, Point pt1, Point pt2, const Scalar &color, int thickness=1, int lineType=LINE_8, int shift=0)
绘制连接两个点的线段。
void warpAffine(InputArray src, OutputArray dst, InputArray M, Size dsize, int flags=INTER_LINEAR, int borderMode=BORDER_CONSTANT, const Scalar &borderValue=Scalar())
对图像应用仿射变换。
void resize(InputArray src, OutputArray dst, Size dsize, double fx=0, double fy=0, int interpolation=INTER_LINEAR)
调整图像大小。
void warpPerspective(InputArray src, OutputArray dst, InputArray M, Size dsize, int flags=INTER_LINEAR, int borderMode=BORDER_CONSTANT, const Scalar &borderValue=Scalar())
对图像应用透视变换。
double findTransformECC(InputArray templateImage, InputArray inputImage, InputOutputArray warpMatrix, int motionType, TermCriteria criteria, InputArray inputMask, int gaussFiltSize)
根据ECC标准找出两个图像之间的几何变换(扭曲)。
@ MOTION_TRANSLATION
定义 tracking.hpp:262
@ MOTION_EUCLIDEAN
定义 tracking.hpp:263
@ MOTION_HOMOGRAPHY
定义 tracking.hpp:265
@ MOTION_AFFINE
定义 tracking.hpp:264
int main(int argc, char *argv[])
定义 highgui_qt.cpp:3
磁盘上与文件关联的文件存储的“黑盒”表示。
定义 core.hpp:102
STL命名空间。