👨‍💻个人简介: 深度学习图像领域工作者
🎉总结链接:
             链接中主要是个人工作的总结,每个链接都是一些常用demo,代码直接复制运行即可。包括:
                    📌1.工作中常用深度学习脚本
                    📌2.torch、numpy等常用函数详解
                    📌3.opencv 图片、视频等操作
                    📌4.个人工作中的项目总结(纯干活)
              链接: https://blog.csdn.net/qq_28949847/article/details/128552785
🎉视频讲解: 以上记录,通过B站等平台进行了视频讲解使用,可搜索 ‘Python图像识别’ 进行观看
              B站:Python图像识别
              抖音:Python图像识别
              西瓜视频:Python图像识别


超链接:B站对此代码进行了讲解


1、cv2.pointPolygonTest() 函数

函数定义:
cv2.pointPolygonTest(contour, pt, measureDist)

函数功能:
找到图像里的点和轮廓之间的最短距离. 它返回的距离当点在轮廓外的时候是负值,当点在轮廓内是正值,如果在轮廓上是0。

参数:
contour :轮廓多边形
pt :坐标点
measureDist:若为True,返回带符号的距离;若为False,会找点是否在内,外,或轮廓上(相应返回+1, -1, 0)。

注意:
contour 传入的是np格式的数据

示例代码:

import cv2
import numpy as np


def onmouse_pick_points(event, x, y, flags, param):
    if event == cv2.EVENT_LBUTTONDOWN:
        print('x = %d, y = %d' % (x, y))
        # 判断点是否在区域内
        flag = cv2.pointPolygonTest(pts, (x, y), False)
        if flag >= 0:
            color = (0, 255, 0)
        else:
            color = (0, 0, 255)
        cv2.drawMarker(param, (x, y), color)


if __name__ == '__main__':
    WIN_NAME = 'pick_points'
    #  凸形状区域
    # pts = np.array([[25, 70], [25, 160],
    #                 [110, 200], [200, 160],
    #                 [200, 70], [110, 20]],
    #                np.int32)
    # 凹形状
    pts = np.array([[100, 100], [400, 100],
                    [400, 200], [150, 200],
                    [150, 450], [400, 450],
                    [400, 550], [100, 550]],
                   np.int32)

    pts = pts.reshape((-1, 1, 2))
    image = np.zeros((800, 800, 3), np.uint8)
    cv2.namedWindow(WIN_NAME, 0)
    cv2.setMouseCallback(WIN_NAME, onmouse_pick_points, image)
    cv2.polylines(image, [pts], True, (255, 255, 255), 3)

    while True:
        cv2.imshow(WIN_NAME, image)
        key = cv2.waitKey(1)
        if key == 27:
            break
    cv2.destroyAllWindows()

效果展示:
凹形状区域,红色十字架代表在区域外,绿色代表在区域内
在这里插入图片描述
凸形状区域:
在这里插入图片描述

GitHub 加速计划 / opencv31 / opencv
77.36 K
55.71 K
下载
OpenCV: 开源计算机视觉库
最近提交(Master分支:1 个月前 )
3919f33e G-API: Introduce level optimization flag for ONNXRT backend #26293 ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [ ] There is a reference to the original bug report and related work - [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [x] The feature is well documented and sample code can be built with the project CMake 1 天前
489df18a Use border value in ipp version of warp affine #26313 ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [ ] There is a reference to the original bug report and related work - [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [ ] The feature is well documented and sample code can be built with the project CMake 1 天前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐