#include <iostream>
static void help(char** argv)
{
cout << "\n这是一个演示程序,展示如何在图像上应用透视变换,\n"
cout << "\n用法:\n" << argv[0] << " [图像名称 -- 默认 right.jpg]\n" << endl;
cout << "\n热键:\n"
"\tESC, q - 退出程序\n"
"\tr - 更改点的顺序以旋转变换\n"
"\tc - 删除选定的点\n"
"\ti - 更改点的顺序以进行逆变换\n"
"\n使用鼠标选择一个点并移动它以查看变换的变化" << endl;
}
static void onMouse(int event, int x, int y, int, void*);
Mat warping(
Mat image,
Size warped_image_size, vector< Point2f> srcPoints, vector< Point2f> dstPoints);
String windowTitle =
"透视变换演示";
String labels[4] = {
"左上",
"右上",
"右下",
"左下" };
vector< Point2f> roi_corners;
vector< Point2f> midpoints(4);
vector< Point2f> dst_corners(4);
int roiIndex = 0;
bool dragging;
int selected_corner_index = 0;
bool validation_needed = true;
int main(
int argc,
char** argv)
{
help(argv);
string filename = samples::findFile(parser.get<string>("@input"));
float original_image_cols = (float)original_image.
cols;
float original_image_rows = (float)original_image.rows;
roi_corners.push_back(
Point2f( (
float)(original_image_cols / 1.70), (
float)(original_image_rows / 4.20) ));
roi_corners.push_back(
Point2f( (
float)(original_image.cols / 1.15), (
float)(original_image.rows / 3.32) ));
roi_corners.push_back(
Point2f( (
float)(original_image.cols / 1.33), (
float)(original_image.rows / 1.10) ));
roi_corners.push_back(
Point2f( (
float)(original_image.cols / 1.93), (
float)(original_image.rows / 1.36) ));
bool endProgram = false;
while (!endProgram)
{
if ( validation_needed & (roi_corners.size() < 4) )
{
validation_needed = false;
image = original_image.clone();
for (size_t i = 0; i < roi_corners.size(); ++i)
{
if( i > 0 )
{
line(image, roi_corners[i-1], roi_corners[(i)],
Scalar(0, 0, 255), 2);
putText(image, labels[i].c_str(), roi_corners[i], FONT_HERSHEY_SIMPLEX, 0.8,
Scalar(255, 0, 0), 2);
}
}
}
if ( validation_needed & ( roi_corners.size() == 4 ))
{
image = original_image.clone();
for ( int i = 0; i < 4; ++i )
{
line(image, roi_corners[i], roi_corners[(i + 1) % 4],
Scalar(0, 0, 255), 2);
putText(image, labels[i].c_str(), roi_corners[i], FONT_HERSHEY_SIMPLEX, 0.8,
Scalar(255, 0, 0), 2);
}
midpoints[0] = (roi_corners[0] + roi_corners[1]) / 2;
midpoints[1] = (roi_corners[1] + roi_corners[2]) / 2;
midpoints[2] = (roi_corners[2] + roi_corners[3]) / 2;
midpoints[3] = (roi_corners[3] + roi_corners[0]) / 2;
dst_corners[0].x = 0;
dst_corners[0].y = 0;
dst_corners[1].x = (float)
norm(midpoints[1] - midpoints[3]);
dst_corners[1].y = 0;
dst_corners[2].x = dst_corners[1].x;
dst_corners[2].y = (float)
norm(midpoints[0] - midpoints[2]);
dst_corners[3].x = 0;
dst_corners[3].y = dst_corners[2].y;
imshow(
"Warped Image", warped_image);
}
if ((c == 'q') | (c == 'Q') | (c == 27))
{
endProgram = true;
}
if ((c == 'c') | (c == 'C'))
{
roi_corners.clear();
}
if ((c == 'r') | (c == 'R'))
{
roi_corners.push_back(roi_corners[0]);
roi_corners.erase(roi_corners.begin());
}
if ((c == 'i') | (c == 'I'))
{
swap(roi_corners[0], roi_corners[1]);
swap(roi_corners[2], roi_corners[3]);
}
}
return 0;
}
static void onMouse(int event, int x, int y, int, void*)
{
if (roi_corners.size() == 4)
{
for (int i = 0; i < 4; ++i)
{
if ((event == EVENT_LBUTTONDOWN) && ((abs(roi_corners[i].x - x) < 10)) && (abs(roi_corners[i].y - y) < 10))
{
selected_corner_index = i;
dragging = true;
}
}
}
else if ( event == EVENT_LBUTTONDOWN )
{
roi_corners.push_back( Point2f( (float) x, (float) y ) );
validation_needed = true;
}
if (event == EVENT_LBUTTONUP)
{
dragging = false;
}
if ((event == EVENT_MOUSEMOVE) && dragging)
{
roi_corners[selected_corner_index].x = (float) x;
roi_corners[selected_corner_index].y = (float) y;
validation_needed = true;
}
}
用于命令行解析。
**定义:** utility.hpp:890
n维密集数组类
**定义:** mat.hpp:829
int cols
**定义:** mat.hpp:2155
用于指定图像或矩形大小的模板类。
**定义:** types.hpp:335
#define CV_VERSION
**定义:** version.hpp:19
double norm(InputArray src1, int normType=NORM_L2, InputArray mask=noArray())
计算数组的绝对范数。
std::string String
**定义:** cvstd.hpp:151
int cvRound(double value)
将浮点数四舍五入到最接近的整数。
**定义:** fast_math.hpp:200
@ circle
**定义:** gr_skig.hpp:62
void imshow(const String &winname, InputArray mat)
在指定的窗口中显示图像。
int waitKey(int delay=0)
等待按键按下。
void namedWindow(const String &winname, int flags=WINDOW_AUTOSIZE)
创建窗口。
void setMouseCallback(const String &winname, MouseCallback onMouse, void *userdata=0)
为指定的窗口设置鼠标处理程序。
void moveWindow(const String &winname, int x, int y)
将窗口移动到指定位置。
CV_EXPORTS_W Mat imread(const String &filename, int flags=IMREAD_COLOR_BGR)
从文件中加载图像。
void putText(InputOutputArray img, const String &text, Point org, int fontFace, double fontScale, Scalar color, int thickness=1, int lineType=LINE_8, bool bottomLeftOrigin=false)
绘制文本字符串。
void line(InputOutputArray img, Point pt1, Point pt2, const Scalar &color, int thickness=1, int lineType=LINE_8, int shift=0)
绘制连接两点的线段。
int main(int argc, char *argv[])
**定义:** highgui_qt.cpp:3