-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_mask.py
98 lines (84 loc) · 4.89 KB
/
add_mask.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
'''
# coding=utf-8
给图像加上黑色mask
分区贡献率
一共19个遮罩贡献率区域
'''
import numpy as np
import cv2
from PIL import Image
# mask坐标
mask_1 = [[135, 89], [113, 117], [96, 153], [127, 154], [133, 134], [137, 109]]
mask_2 = [(94, 153), (127, 155), (112, 201), (111, 262), (62, 264), (75, 200)]
mask_3 = [(62, 263), (108, 264), (117, 328), (104, 385), (53, 427), (55, 331)]
mask_4 = [(137, 92), (126, 154), (177, 153), (181, 120), (189, 55), (158, 66)]
mask_5 = [(129, 154), (177, 153), (165, 211), (152, 263), (111, 264), (110, 201)]
mask_6 = [(110, 264), (152, 264), (154, 364), (98, 391), (111, 348), (116, 297)]
mask_7 = [(193, 59), (181, 119), (179, 151), (225, 151), (222, 101), (213, 67)]
mask_8 = [(176, 154), (225, 151), (228, 263), (152, 265.), (163., 228.), (170., 190.)]
mask_9 = [(152., 264.), (227., 264.), (225., 338.), (195., 368.), (156., 381.), (149., 309.)]
mask_10 = [(300.82110818987735, 90.84570820837348), (337.3485685220172, 122.6381273863471),
(352.9065608857064, 150.3719398607496), (281.8809435732122, 150.3719398607496),
(281.20450912261697, 126.02029963932301), (289.3217225297592, 107.75656947325308)]
mask_11 = [(279.8516402214266, 151.0483743113448), (352.2301264351112, 150.3719398607496),
(369.817422150586, 212.60390931550643), (381.31680781070406, 273.48300986907293),
(303.526845992258, 274.83587877026326), (289.3217225297592, 210.5746059637209)]
mask_12 = [(304.20328044285327, 272.80657541847773), (315.7026661029713, 386.44756311846845),
(380.6403733601089, 399.299817679777), (387.40471786606076, 370.88957075477924),
(388.0811523166559, 314.0690769047839), (382.66967671189445, 276.18874767145365)]
mask_13 = [(300.82110818987735, 90.84570820837348), (337.3485685220172, 122.6381273863471),
(353.58299533630156, 149.6955054101544), (392.8161934708222, 149.6955054101544),
(372.5231599529667, 102.34509386849163), (325.84918286189907, 68.52337133873243)]
mask_14 = [(352.9065608857064, 150.3719398607496), (369.1409876999908, 215.30964711788715),
(380.6403733601089, 274.1594443196681), (425.96148154998616, 274.1594443196681),
(411.75635808748734, 215.30964711788715), (395.5219312732029, 150.3719398607496)]
mask_15 = [(380.6403733601089, 274.83587877026326), (388.0811523166559, 349.9201027863286),
(378.6110700083234, 397.9469487785866), (431.3729571547476, 427.71006460477463),
(425.96148154998616, 337.06784822502016), (427.31435045117655, 274.83587877026326)]
mask_16 = [(323.81987951011354, 68.52337133873243), (363.7295120952293, 93.5514460107542),
(392.8161934708222, 149.01907095955926), (436.1079983089139, 150.3719398607496),
(392.8161934708222, 75.28771584468427), (361.02377429284854, 65.81763353635176)]
mask_17 = [(394.1690623720126, 151.0483743113448), (410.40348918629695, 211.92747486491123),
(423.93217819820063, 275.51231322085846), (467.9004174868875, 274.1594443196681),
(457.0774662773646, 201.78095810598353), (435.4315638583188, 148.34263650896406)]
mask_18 = [(425.96148154998616, 275.51231322085846), (425.285047099391, 335.71497932382977),
(431.3729571547476, 422.2985890000132), (460.4596385303405, 473.0311727946519),
(468.57685193748273, 355.33157839109003), (468.57685193748273, 274.1594443196681)]
mask_19 = [(189.2094238416721, 76.64058474587466), (310.967624948805, 76.64058474587466),
(298.11537038749657, 121.28525848515676), (382.66967671189445, 270.7772720666922),
(367.11168434820524, 393.2119076244203), (212.2081951619083, 366.154529600613),
(191.9151616440529, 293.0996089363332), (213.56106406309868, 146.98976760777367)]
mask_list = [mask_1, mask_2, mask_3, mask_4, mask_5, mask_6, mask_7, mask_8, mask_9, mask_10, mask_11, mask_12, mask_13,
mask_14, mask_15, mask_16, mask_17, mask_18, mask_19]
def add_mask(image_path, mask_point):
'''
Args:
image_path: image_path
mask_point: mask_info
Returns: 返回经过masked的图片 pil格式
'''
#
point = np.array([mask_point], dtype=np.int32)
# 创建全1
# mask = np.ones(image.shape[:2], dtype = "uint8")
# 创建全255
mask = 255 * np.ones((512, 512), dtype="uint8")
# 黑色轮廓
cv2.polylines(mask, point, 1, 0)
# 黑色mask
cv2.fillPoly(mask, point, 0)
# cv2.imshow("Mask", mask)
image = cv2.imread(image_path)
image = cv2.resize(image, (512, 512))
masked = cv2.bitwise_and(image, image, mask=mask)
# cv2.imshow("Mask to Image", masked)
masked_image = Image.fromarray(cv2.cvtColor(masked, cv2.COLOR_BGR2RGB))
# print(image.size)
# image.show()
# cv2.waitKey(1000)
return masked_image
# if __name__ == "__main__":
# # image_path = 'datasets/test/NORMAL/NORMAL_test017.png'
# image_path = r''
# for mask in mask_list:
# add_mask(image_path, mask)