1 Star 0 Fork 0

lewlovehow/Learning-OpenCV-3_examples

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
example_19-04.cpp 2.26 KB
一键复制 编辑 原始数据 按行查看 历史
Gary Bradski 提交于 8年前 . example 19-04
// Example 19-4. Two-dimensional line fitting
#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h>
using namespace std;
void help(char **argv) {
cout << "\nExample 19-04, two dimensional line fitting"
<< "\nCall"
<< "\n" << argv[0] << "\n"
<< "\n 'q', 'Q' or ESC to quit"
<< "\n" << endl;
}
int main(int argc, char **argv) {
cv::Mat img(500, 500, CV_8UC3);
cv::RNG rng(-1);
help(argv);
for (;;) {
char key;
int i, count = rng.uniform(0, 100) + 3, outliers = count / 5;
float a = (float)rng.uniform(0., 200.);
float b = (float)rng.uniform(0., 40.);
float angle = (float)rng.uniform(0., CV_PI);
float cos_a = cos(angle), sin_a = sin(angle);
cv::Point pt1, pt2;
vector<cv::Point> points(count);
cv::Vec4f line;
float d, t;
b = MIN(a * 0.3f, b);
// generate some points that are close to the line
for (i = 0; i < count - outliers; i++) {
float x = (float)rng.uniform(-1., 1.) * a;
float y = (float)rng.uniform(-1., 1.) * b;
points[i].x = cvRound(x * cos_a - y * sin_a + img.cols / 2);
points[i].y = cvRound(x * sin_a + y * cos_a + img.rows / 2);
}
// generate outlier points
for (; i < count; i++) {
points[i].x = rng.uniform(0, img.cols);
points[i].y = rng.uniform(0, img.rows);
}
// find the optimal line
cv::fitLine(points, line, cv::DIST_L1, 1, 0.001, 0.001);
// draw the points
img = cv::Scalar::all(0);
for (i = 0; i < count; i++)
cv::circle(img, points[i], 2,
i < count - outliers ? cv::Scalar(0, 0, 255)
: cv::Scalar(0, 255, 255),
cv::FILLED, CV_AA, 0);
// ... and the long enough line to cross the whole image
d = sqrt((double)line[0] * line[0] + (double)line[1] * line[1]);
line[0] /= d;
line[1] /= d;
t = (float)(img.cols + img.rows);
pt1.x = cvRound(line[2] - line[0] * t);
pt1.y = cvRound(line[3] - line[1] * t);
pt2.x = cvRound(line[2] + line[0] * t);
pt2.y = cvRound(line[3] + line[1] * t);
cv::line(img, pt1, pt2, cv::Scalar(0, 255, 0), 3, CV_AA, 0);
cv::imshow("Fit Line", img);
key = (char)cv::waitKey(0);
if (key == 27 || key == 'q' || key == 'Q') // 'ESC'
break;
}
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/lewlovehow/Learning-OpenCV-3_examples.git
git@gitee.com:lewlovehow/Learning-OpenCV-3_examples.git
lewlovehow
Learning-OpenCV-3_examples
Learning-OpenCV-3_examples
master

搜索帮助