python中用OpenCV填充不规则点集合(小杜总结)

大家好,不多说,直接上代码。

#导入包和文件
import cv2
import numpy as np
import matplotlib.pyplot as plt
#为了画出轮廓
from imutils import contours
# 绘图展示函数,画出图像
def cv_show(name,img):
    cv2.imshow(name, img)
    cv2.waitKey(1)
    cv2.destroyAllWindows()
    plt.imshow(img,cmap='gray')
# 读取一个模板图像
img = cv2.imread('13.png')
print(img.shape)
cv_show('img',img)
(240, 320, 3)

python中用OpenCV填充不规则点集合(小杜总结)_第1张图片

# 灰度图
ref = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv_show('ref',ref)

python中用OpenCV填充不规则点集合(小杜总结)_第2张图片

# 二值图像
ref = cv2.threshold(ref, 10, 255, cv2.THRESH_BINARY_INV)[1]
print(ref.shape)
cv_show('ref',ref)
(240, 320)

python中用OpenCV填充不规则点集合(小杜总结)_第3张图片

"""计算轮廓
cv2.findContours()函数接受的参数为二值图,即黑白的(不是灰度图),cv2.RETR_EXTERNAL只检测外轮廓,cv2.CHAIN_APPROX_SIMPLE只保留终点坐标
返回的list中每个元素都是图像中的一个轮廓"""
#refCnts, hierarchy = cv2.findContours(ref.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
refCnts, hierarchy = cv2.findContours(ref.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
"""此处为了变换图形,画出所有的点,-1是划所有的点,6是为了把轮廓上不全的点补全"""
cv2.drawContours(img,refCnts,-1,(0,0,0),8)
cv_show('img',img)
#此处为了下边遍历点集合做准备
print(np.array(refCnts).shape)
(28,)

python中用OpenCV填充不规则点集合(小杜总结)_第4张图片

"""保存图片为了下一次填充做准备"""
cv2.imwrite('text1.png',img)
True
#读取图片
img1 = cv2.imread('text.png')
cv_show('text',img1)

python中用OpenCV填充不规则点集合(小杜总结)_第5张图片

# 灰度图
text = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
cv_show('text',text)

python中用OpenCV填充不规则点集合(小杜总结)_第6张图片

# 二值图像
text = cv2.threshold(text, 10, 255, cv2.THRESH_BINARY_INV)[1]
print(text.shape)
cv_show('text',text)
(240, 320)

python中用OpenCV填充不规则点集合(小杜总结)_第7张图片

print(text.shape)
(240, 320)
"""计算轮廓
cv2.findContours()函数接受的参数为二值图,即黑白的(不是灰度图),cv2.RETR_EXTERNAL只检测外轮廓,cv2.CHAIN_APPROX_SIMPLE只保留终点坐标
返回的list中每个元素都是图像中的一个轮廓"""
#refCnts, hierarchy = cv2.findContours(text.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
refCnts1, hierarchy1 = cv2.findContours(text.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
"""此处为了变换图形,画出所有的点,-1是划所有的点,6是为了把轮廓上不全的点补全"""
cv2.drawContours(img,refCnts1,-1,(0,0,0),6)
cv_show('img',img)
#此处为了下边遍历点集合做准备
print(np.array(refCnts).shape)
(28,)

python中用OpenCV填充不规则点集合(小杜总结)_第8张图片

#遍历所有点集合
a = []
for g in range(np.array(refCnts1).shape[0]):
    print(g)
    for i in refCnts1[g]:
        #print(i)
        for j in i:
            #print(j)
            #print(tuple(j))
            a.append(tuple(j))
print(a)
print(len(a))
0
[(154, 137), (153, 138), (152, 138), (151, 138), (150, 138), (149, 138), (148, 139), (147, 139), (146, 139), (145, 140), (144, 140), (143, 140), (142, 141), (141, 141), (140, 142), (139, 143), (138, 143), (137, 144), (136, 145), (135, 146), (134, 147), (133, 148), (132, 148), (131, 149), (131, 150), (130, 151), (130, 152), (130, 153), (129, 154), (129, 155), (129, 156), (129, 157), (129, 158), (129, 159), (129, 160), (129, 161), (129, 162), (130, 163), (130, 164), (130, 165), (131, 166), (131, 167), (131, 168), (131, 169), (131, 170), (131, 171), (131, 172), (131, 173), (130, 174), (129, 175), (128, 176), (127, 177), (126, 178), (125, 179), (124, 180), (124, 181), (123, 182), (123, 183), (123, 184), (122, 185), (122, 186), (122, 187), (121, 188), (121, 189), (121, 190), (120, 191), (120, 192), (120, 193), (120, 194), (120, 195), (120, 196), (120, 197), (120, 198), (120, 199), (120, 200), (120, 201), (120, 202), (120, 203), (120, 204), (119, 205), (119, 206), (119, 207), (119, 208), (118, 209), (118, 210), (118, 211), (118, 212), (118, 213), (118, 214), (117, 215), (117, 216), (117, 217), (117, 218), (117, 219), (116, 220), (116, 221), (116, 222), (116, 223), (116, 224), (116, 225), (116, 226), (116, 227), (116, 228), (116, 229), (116, 230), (116, 231), (116, 232), (115, 233), (115, 234), (115, 235), (115, 236), (115, 237), (115, 238), (116, 239), (117, 239), (118, 239), (119, 239), (120, 239), (121, 239), (122, 239), (123, 239), (124, 239), (125, 239), (126, 239), (127, 239), (128, 239), (129, 239), (130, 239), (131, 239), (132, 239), (133, 239), (134, 239), (135, 239), (136, 239), (137, 239), (138, 239), (139, 239), (140, 239), (141, 239), (142, 239), (143, 239), (144, 239), (145, 239), (146, 239), (147, 239), (148, 239), (149, 239), (150, 239), (151, 239), (152, 239), (153, 239), (154, 239), (155, 239), (156, 239), (157, 239), (158, 239), (159, 239), (160, 239), (161, 239), (162, 239), (163, 239), (164, 239), (165, 239), (166, 239), (167, 239), (168, 239), (169, 239), (170, 239), (171, 239), (172, 239), (173, 239), (174, 239), (175, 239), (176, 239), (177, 239), (178, 239), (179, 239), (180, 239), (181, 239), (182, 239), (183, 239), (184, 239), (185, 239), (186, 239), (187, 239), (188, 239), (189, 239), (190, 239), (191, 239), (192, 239), (193, 239), (194, 239), (195, 239), (196, 239), (197, 239), (198, 239), (199, 239), (200, 239), (201, 239), (202, 239), (203, 239), (204, 239), (205, 239), (206, 239), (207, 239), (208, 239), (209, 239), (210, 239), (211, 239), (212, 239), (213, 239), (214, 239), (215, 239), (216, 239), (217, 239), (218, 239), (219, 239), (220, 239), (221, 238), (222, 238), (223, 237), (224, 236), (225, 236), (226, 236), (227, 235), (228, 235), (229, 234), (230, 234), (231, 234), (232, 233), (233, 233), (234, 233), (235, 232), (236, 231), (237, 231), (238, 230), (239, 229), (240, 228), (241, 228), (242, 228), (243, 227), (243, 226), (244, 225), (245, 224), (246, 223), (247, 222), (248, 221), (248, 220), (249, 219), (249, 218), (249, 217), (250, 216), (250, 215), (250, 214), (250, 213), (250, 212), (250, 211), (249, 210), (249, 209), (249, 208), (248, 207), (248, 206), (247, 205), (246, 204), (246, 203), (245, 202), (244, 201), (243, 200), (243, 199), (242, 198), (242, 197), (241, 196), (240, 195), (240, 194), (239, 193), (238, 192), (238, 191), (237, 190), (236, 190), (235, 189), (234, 188), (233, 187), (232, 187), (231, 186), (230, 185), (229, 185), (228, 184), (227, 184), (226, 183), (225, 182), (224, 181), (223, 180), (222, 179), (221, 178), (221, 177), (220, 176), (219, 175), (218, 174), (217, 173), (216, 172), (215, 171), (214, 170), (213, 169), (212, 169), (211, 168), (210, 167), (209, 166), (208, 165), (207, 164), (206, 163), (205, 162), (204, 161), (204, 160), (203, 159), (202, 159), (201, 158), (201, 157), (200, 156), (199, 155), (198, 154), (197, 154), (196, 154), (195, 153), (195, 152), (194, 151), (193, 151), (192, 151), (191, 150), (190, 150), (189, 150), (188, 149), (187, 148), (186, 147), (185, 147), (184, 146), (183, 146), (182, 145), (181, 145), (180, 144), (179, 144), (178, 144), (177, 143), (176, 142), (175, 142), (174, 141), (173, 141), (172, 141), (171, 140), (170, 140), (169, 139), (168, 138), (167, 138), (166, 138), (165, 137), (164, 137), (163, 137), (162, 137), (161, 137), (160, 137), (159, 137), (158, 137), (157, 137), (156, 137), (155, 137)]
364
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 = a
    img = 'text1.png'
    color = [0, 0, 255]
    point2area(points, img, color)

结果图
python中用OpenCV填充不规则点集合(小杜总结)_第9张图片



你可能感兴趣的:(python中用OpenCV填充不规则点集合(小杜总结))