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 分钟前
开源呼叫中心中,如何将ASR与IVR菜单结合,实现动态的IVR交互
人工智能·机器人·自动化·音视频·信息与通信
CountingStars61931 分钟前
目标检测常用评估指标(metrics)
人工智能·目标检测·目标跟踪
tangjunjun-owen39 分钟前
第四节:GLM-4v-9b模型的tokenizer源码解读
人工智能·glm-4v-9b·多模态大模型教程
冰蓝蓝44 分钟前
深度学习中的注意力机制:解锁智能模型的新视角
人工智能·深度学习
橙子小哥的代码世界1 小时前
【计算机视觉基础CV-图像分类】01- 从历史源头到深度时代:一文读懂计算机视觉的进化脉络、核心任务与产业蓝图
人工智能·计算机视觉
新加坡内哥谈技术1 小时前
苏黎世联邦理工学院与加州大学伯克利分校推出MaxInfoRL:平衡内在与外在探索的全新强化学习框架
大数据·人工智能·语言模型
小陈phd2 小时前
OpenCV学习——图像融合
opencv·计算机视觉·cv
fanstuck2 小时前
Prompt提示工程上手指南(七)Prompt编写实战-基于智能客服问答系统下的Prompt编写
人工智能·数据挖掘·openai
lovelin+v175030409662 小时前
安全性升级:API接口在零信任架构下的安全防护策略
大数据·数据库·人工智能·爬虫·数据分析
唐小旭2 小时前
python3.6搭建pytorch环境
人工智能·pytorch·python