質問編集履歴
2
変数の中身 追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -114,7 +114,9 @@
|
|
114
114
|
|
115
115
|
何か解決策はありますでしょうか
|
116
116
|
|
117
|
+
data_1にはsample
|
117
118
|
|
119
|
+
data_2には.png が入っています
|
118
120
|
|
119
121
|
|
120
122
|
|
1
コード追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -2,15 +2,111 @@
|
|
2
2
|
|
3
3
|
```python
|
4
4
|
|
5
|
+
import numpy as np
|
6
|
+
|
7
|
+
import cv2
|
8
|
+
|
9
|
+
import pickle
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
#関数定義
|
16
|
+
|
17
|
+
def cannyprocess(img,data_1,count,data_2):
|
18
|
+
|
19
|
+
edges = cv2.Canny(img,100,200)
|
20
|
+
|
21
|
+
cv2.imwrite("canny\" + data_1 + '000' + count + data_2,edges)#エッジ画像保存
|
22
|
+
|
23
|
+
can_img = cv2.imread("canny\" + data_1 + '000' + count + data_2)#エッジ画像の読み込み
|
24
|
+
|
25
|
+
return can_img
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
def contourprocess(img):
|
30
|
+
|
31
|
+
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
|
32
|
+
|
33
|
+
image, contours, hierarchy = cv2.findContours(gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
34
|
+
|
5
|
-
|
35
|
+
return contours
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
def maskgenerate(img,data_1,count,data_2):
|
40
|
+
|
41
|
+
back = np.zeros_like(img)
|
42
|
+
|
43
|
+
cont = contourprocess(cannyprocess(img,data_1,count,data_2))
|
44
|
+
|
45
|
+
mask = cv2.drawContours(back, cont, -1, color=(255, 255, 255), thickness=-1)
|
46
|
+
|
47
|
+
cv2.imwrite("mask\" + data_1 + '000' + count + data_2,mask)
|
48
|
+
|
49
|
+
mask = cv2.imread("mask\" + data_1 + '000' + count + data_2,0)
|
50
|
+
|
51
|
+
return mask
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
def mask_and_alpha(base_img, mask_img, data_1, count, data_2):
|
56
|
+
|
57
|
+
bgr_img = cv2.split(base_img)
|
58
|
+
|
59
|
+
clip = cv2.merge(bgr_img + [mask_img])
|
60
|
+
|
61
|
+
cv2.imwrite("clip\" + data_1 + '000' + count + data_2,clip)
|
62
|
+
|
63
|
+
return clip
|
64
|
+
|
65
|
+
#関数定義
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
#切り取り用
|
70
|
+
|
71
|
+
#img = cv2.imread("base\2640163.jpg",1)#元画像読み込み
|
72
|
+
|
73
|
+
#mask_and_alpha(img, maskgenerate(img))
|
74
|
+
|
75
|
+
#切り取り用
|
6
76
|
|
7
77
|
count = 1
|
8
78
|
|
9
|
-
|
79
|
+
clear = [0,0,0]
|
10
80
|
|
11
|
-
|
81
|
+
while(1):
|
12
82
|
|
83
|
+
with open("frag\frag.txt",'rb') as frag:
|
84
|
+
|
85
|
+
data = pickle.load(frag)
|
86
|
+
|
87
|
+
if data[0] == '1':
|
88
|
+
|
89
|
+
count = str(count)
|
90
|
+
|
13
|
-
img = cv2.imread(data
|
91
|
+
img = cv2.imread("base\" + data[1] + '000' + count + data[2], 1)
|
92
|
+
|
93
|
+
mask_and_alpha(img, maskgenerate(img,data[1],count,data[2]),data[1],count,data[2])
|
94
|
+
|
95
|
+
count = int(count)
|
96
|
+
|
97
|
+
count += 1
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
with open("frag\frag.txt",'wb') as file:
|
102
|
+
|
103
|
+
pickle.dump(clear, file)
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
|
14
110
|
|
15
111
|
```
|
16
112
|
|