Python Opencv根据点来填充区域

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_36811967/article/details/88823069
# -*- coding:utf-8 -*-
__author__ = 'ShawDa'

import cv2
import numpy as np


def point2area(points, img, color):
    """
    :param points: 点集合
    :param img: 图片位置
    :param color: BGR三色
    :return:将图片上点包围的区域涂上颜色
    """
    img = cv2.imread(img)
    res = cv2.fillPoly(img, [np.array(points)] ,color)
    cv2.imshow('fillpoly', res)
    cv2.waitKey(0)
    cv2.destroyAllWindows()


if __name__ == '__main__':
    points = [(20,20), (70,70), (120,200)]
    img = 'lena.png'
    color = [255, 255, 255]
    point2area(points, img, color)

根据点围成的闭合区间在图片上将这部分填充某一种颜色,结果如图:

在这里插入图片描述

更换一下points:

points = [(0,20), (70,70), (120,200), (0,300)]

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/sinat_36811967/article/details/88823069