OPENCV 检测直线[opencv--3]

opencv中集成了很多好用的函数,比如霍夫变换检测直线的函数,当然,考虑到看我文章的人水平,我这里只讲讲如何使用这个函数,和怎么调节其中的参数

先把运行效果PO出来吧

cpp 复制代码
#include "CV_ERROR.h"
#include "MCV_funs.hpp"

#include <opencv2/opencv.hpp>
#include <iostream>

int main() {
    cv::utils::logging::setLogLevel(cv::utils::logging::LOG_LEVEL_ERROR);
    // 读取PNG图像
    //cv::Mat image = cv::imread("lines.png", cv::IMREAD_COLOR);
    cv::Mat image = cv::imread("PIC.jpg", cv::IMREAD_COLOR);
    if (image.empty()) {
        std::cerr << "无法读取图像文件" << std::endl;
        return -1;
    }
    showimageWin(image);

    // 转换为灰度图像
    cv::Mat gray;
    cv::cvtColor(image, gray, cv::COLOR_BGR2GRAY);

    // 应用边缘检测(Canny)
    cv::Mat edges;
    cv::Canny(gray, edges, 150, 200, 3);

    showimageWin(edges);

    // 使用霍夫变换检测直线
    std::vector<cv::Vec2f> lines;
    cv::HoughLines(edges, lines, 0.5, CV_PI / 360, 200);


    // 在原图上绘制检测到的直线
    cv::Mat result = image.clone();
    for (size_t i = 0; i < lines.size(); i++) {
        float rho = lines[i][0], theta = lines[i][1];
        cv::Point pt1, pt2;
        double a = cos(theta), b = sin(theta);
        double x0 = a * rho, y0 = b * rho;
        pt1.x = cvRound(x0 + 1000 * (-b));
        pt1.y = cvRound(y0 + 1000 * (a));
        pt2.x = cvRound(x0 - 1000 * (-b));
        pt2.y = cvRound(y0 - 1000 * (a));
        cv::line(result, pt1, pt2, cv::Scalar(0, 255, 0), 15, cv::LINE_AA);
    }

    // 显示结果图像
    showimageWin(result);
    cv::waitKey();

    return 0;
}

下面是原理类的讲解,本鼠懒得重复做笔记了,直接PO在下面吧ψ(`∇´)ψ

相关推荐
端平入洛6 小时前
auto有时不auto
c++
哇哈哈20211 天前
信号量和信号
linux·c++
多恩Stone1 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
蜡笔小马1 天前
21.Boost.Geometry disjoint、distance、envelope、equals、expand和for_each算法接口详解
c++·算法·boost
超级大福宝1 天前
N皇后问题:经典回溯算法的一些分析
数据结构·c++·算法·leetcode
weiabc1 天前
printf(“%lf“, ys) 和 cout << ys 输出的浮点数格式存在细微差异
数据结构·c++·算法
问好眼1 天前
《算法竞赛进阶指南》0x01 位运算-3.64位整数乘法
c++·算法·位运算·信息学奥赛
yyjtx1 天前
DHU上机打卡D31
开发语言·c++·算法
czxyvX1 天前
020-C++之unordered容器
数据结构·c++
会编程的土豆1 天前
2.25 做题
数据结构·c++·算法