OpenCV结构分析与形状描述符(17)判断轮廓是否为凸多边形的函数isContourConvex()的使用

  • 操作系统:ubuntu22.04
  • OpenCV版本:OpenCV4.9
  • IDE:Visual Studio Code
  • 编程语言:C++11

算法描述

测试轮廓的凸性。

该函数测试输入的轮廓是否为凸的。轮廓必须是简单的,即没有自相交。否则,函数的输出是不确定的。

cv::isContourConvex 函数是 OpenCV 提供的一个用于判断轮廓是否为凸多边形的函数。这个函数可以用来验证一个给定的轮廓是否是凸的,这对于后续处理(如使用 intersectConvexConvex 来寻找两个凸多边形的交集)是非常有用的。

函数原型

cpp 复制代码
bool cv::isContourConvex	
(
	InputArray 	contour
)	

参数

  • 参数contour 输入的二维点向量,存储在 std::vector<> 或 Mat 中

代码示例

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

int main()
{
    // 定义两个矩形的顶点
    cv::Mat p1 = ( cv::Mat_< float >( 4, 2 ) << 0, 0,  // 左上角
                   2, 0,                               // 右上角
                   2, 2,                               // 右下角
                   0, 2 );                             // 左下角

    cv::Mat p2 = ( cv::Mat_< float >( 4, 2 ) << 1, 1,  // 左上角
                   3, 1,                               // 右上角
                   3, 3,                               // 右下角
                   1, 3 );                             // 左下角

    // 验证轮廓是否为凸多边形
    bool isP1Convex = cv::isContourConvex( p1 );
    bool isP2Convex = cv::isContourConvex( p2 );

    std::cout << "Polygon p1 is convex: " << std::boolalpha << isP1Convex << std::endl;
    std::cout << "Polygon p2 is convex: " << std::boolalpha << isP2Convex << std::endl;

    // 如果两个多边形都是凸的,才继续进行交集计算
    if ( isP1Convex && isP2Convex )
    {
        cv::Mat p12;
        bool intersect = cv::intersectConvexConvex( p1, p2, p12, true );

        if ( intersect )
        {
            std::cout << "Polygons intersect." << std::endl;
            std::cout << "Intersection vertices:" << std::endl;
            for ( int i = 0; i < p12.rows; ++i )
            {
                cv::Point2f pt = p12.at< cv::Point2f >( i );
                std::cout << "Vertex " << i << ": (" << pt.x << ", " << pt.y << ")" << std::endl;
            }
        }
        else
        {
            std::cout << "Polygons do not intersect." << std::endl;
        }
    }
    else
    {
        std::cout << "One or both polygons are not convex." << std::endl;
    }

    return 0;
}

运行结果

bash 复制代码
Polygon p1 is convex: true
Polygon p2 is convex: true
Polygons intersect.
Intersection vertices:
Vertex 0: (2, 1)
Vertex 1: (2, 2)
Vertex 2: (1, 2)
Vertex 3: (1, 1)
相关推荐
小趴菜不能喝3 分钟前
Spring AI 基础实践
数据库·人工智能·spring
zhangfeng11337 分钟前
KTransformers / 简称 Kt 让超大模型(如 DeepSeek-V3)能够在消费级硬件(单卡 24GB 显存 + 大内存)跑
人工智能·语言模型·自然语言处理
AI智能观察8 分钟前
构建品牌AI认知资产:企业GEO实战四步框架,提升品牌AI推荐率
人工智能·geo·智能营销·geo优化·geo工具·geo平台·流量运营、
sali-tec8 分钟前
C# 基于OpenCv的视觉工作流-章24-SURF特征点
图像处理·人工智能·opencv·算法·计算机视觉
hillstream315 分钟前
从这次xAI重组说开去--用类比的思维来理解
人工智能·算法·xai·elon.mask
ccLianLian22 分钟前
计算机基础·cs336·推理和训练
人工智能·深度学习
鹅是开哥22 分钟前
Spring AI Alibaba + DashScope 调用超时彻底解决(SocketTimeoutException / read timeout)
java·人工智能·spring
网易伏羲24 分钟前
以数据驱动工程机械智能化,网易灵动入选杭州国家语料库首批高质量数据集榜单
人工智能·具身智能·网易伏羲·网易灵动
够快云库24 分钟前
2026信创架构实战:制造业非结构化数据的深度治理之道
人工智能·架构·企业文件管理
lisw0525 分钟前
机器人系统:化学研究的超空间引擎——从自动化到智能化的范式革命
大数据·人工智能·科技·机器学习·机器人