opencv 直线检测 HoughLines HoughLinesP

cpp 复制代码
#include "iostream"
#include "opencv2/opencv.hpp"

using namespace std;
using namespace cv;

void drawline(Mat img, vector<Vec2f> lines, double rows, double cols,Scalar scalar)
{
	Point p1, p2;
	for (int i = 0; i < lines.size(); i++)
	{
		float jl = lines[i][0];
		float jd = lines[i][1];
		double a = cos(jd);
		double b = sin(jd);

		double x0 = jl * a;
		double y0 = jl * b;

		double len = max(rows, cols);
		p1.x = cvRound(x0 + len * (-b));
		p1.y = cvRound(y0 + len * a);

		p2.x = cvRound(x0 - len * (-b));
		p2.y = cvRound(y0 - len * a);

		line(img, p1, p2, scalar);
	}
}

int main()
{
	Mat  img,smallimg,gray, edge,gau, smallimg1, smallimg2, smallimg3, smallimg4;

	img = imread("shubiaodian.jpg");

	resize(img, smallimg, Size(0, 0), 0.1, 0.1);

	smallimg.copyTo(smallimg1);
	smallimg.copyTo(smallimg2);
	smallimg.copyTo(smallimg3);
	smallimg.copyTo(smallimg4);
	cvtColor(smallimg, gray, COLOR_BGR2GRAY);
	GaussianBlur(gray, gau, Size(3, 3), 5, 5);

	Canny(gau, edge, 150, 255);

	vector<Vec2f> lines1;
	vector<Vec2f> lines2;

	HoughLines(edge, lines1, 1, CV_PI * 2 / 180, 30);
	HoughLines(edge, lines2, 1, CV_PI * 2 / 180, 80);

	drawline(smallimg1, lines1, edge.rows, edge.cols, Scalar(0, 0, 255));
	drawline(smallimg2, lines2, edge.rows, edge.cols, Scalar(0, 0, 255));

	vector<Vec4i> lines3;
	vector<Vec4i> lines4;

	HoughLinesP(edge, lines3, 1, CV_PI / 180, 30, 0, 10);
	HoughLinesP(edge, lines4, 2, CV_PI / 180, 60, 150, 80);

	cout << "lines1:" << lines1.size() << endl;
	cout << "lines2:" << lines2.size() << endl;
	cout << "lines3:" << lines3.size() << endl;
	cout << "lines4:" << lines4.size() << endl;
	for (int i = 0; i < lines3.size(); i++)
	{
		line(smallimg3, Point(lines3[i][0], lines3[i][1]), Point(lines3[i][2], lines3[i][3]), Scalar(0,0,255));
	}
	for (int i = 0; i < lines4.size(); i++)
	{
		line(smallimg4, Point(lines4[i][0], lines4[i][1]), Point(lines4[i][2], lines4[i][3]), Scalar(0,0,255));
	}

	imshow("smallimg", smallimg);
	imshow("canny", edge);
	imshow("smallimg1", smallimg1);
	imshow("smallimg2", smallimg2);
	imshow("smallimg3", smallimg3);
	imshow("smallimg4", smallimg4);

	waitKey(0);

	return 1;
}
相关推荐
禁默6 分钟前
第六届机器人、智能控制与人工智能国际学术会议(RICAI 2024)
人工智能·机器人·智能控制
Robot25114 分钟前
浅谈,华为切入具身智能赛道
人工智能
只怕自己不够好19 分钟前
OpenCV 图像运算全解析:加法、位运算(与、异或)在图像处理中的奇妙应用
图像处理·人工智能·opencv
果冻人工智能1 小时前
2025 年将颠覆商业的 8 大 AI 应用场景
人工智能·ai员工
代码不行的搬运工1 小时前
神经网络12-Time-Series Transformer (TST)模型
人工智能·神经网络·transformer
石小石Orz1 小时前
Three.js + AI:AI 算法生成 3D 萤火虫飞舞效果~
javascript·人工智能·算法
孤独且没人爱的纸鹤2 小时前
【深度学习】:从人工神经网络的基础原理到循环神经网络的先进技术,跨越智能算法的关键发展阶段及其未来趋势,探索技术进步与应用挑战
人工智能·python·深度学习·机器学习·ai
阿_旭2 小时前
TensorFlow构建CNN卷积神经网络模型的基本步骤:数据处理、模型构建、模型训练
人工智能·深度学习·cnn·tensorflow
羊小猪~~2 小时前
tensorflow案例7--数据增强与测试集, 训练集, 验证集的构建
人工智能·python·深度学习·机器学习·cnn·tensorflow·neo4j
极客代码2 小时前
【Python TensorFlow】进阶指南(续篇三)
开发语言·人工智能·python·深度学习·tensorflow