-
Notifications
You must be signed in to change notification settings - Fork 6
/
tif_process.py
141 lines (88 loc) · 3.31 KB
/
tif_process.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import cv2
import numpy as np
import os
from libtiff import TIFF
from random import shuffle
path = "E:\\code\\gpu\\in" #文件夹目录
out = "E:\\code\\gpu\\out" #文件夹目录
colors = [(255,0,0), (0,255,0),(0,0,255),(0,255,255),(255,255,0),(255,0,255), (255,51,0),(51,255,0),(204,51,51),(255,255,153),(255,153,0),(204,0,0),(255,153,102),(255,102,0),(255,102,102),(255,153,153),
(0,51,255),(0, 255,51),(51,204,51),(153,255,255),(0,255,153),(0,204,0),(102,255,153),(0,255,102),(102,255,102),(153,255,153),
(51,0,255),(255,0,51),(51,51,204),(255,153,255),(153,0,255),(0,0,204),(153,102,255),(102,0,255),(102,102,255),(153,153,255),
(255,255,255)]
def manipu_cv(img, label):
height = img.shape[0]
width = img.shape[1]
img2 = np.zeros((height,width,3), np.uint8)
for i in range(height):
for j in range(width):
#if img[i,j]%2 == 0:
# img[i,j] +=1
if img[i,j] != 0:
img2[i,j]=colors[int(img[i,j]%7)]#37
#if img[i,j] == 18 or img[i,j] == 19:
# img[i,j]=0
#else:
# img[i,j]=255
return img2
def manipu_cv2(img):
height = img.shape[0]
width = img.shape[1]
img2 = np.zeros((height,width,3), np.uint8)
for i in range(height):
for j in range(width):
if img[i,j] != 0:
img[i,j]=255
return img
if __name__ == "__main__":
#files= os.listdir(path)
#files = os.walk(path)
shuffle(colors)
files=[]
fpathes =[]
for fpathe,dirs,fs in os.walk(path):
fpathes.append(fpathe)
for f in fs:
files.append(os.path.join(fpathe,f))
for folder in fpathes[1:]:
os.mkdir(folder.replace("\\in","\\out"))
for file in files: #遍历文
position = file
out_position = file.replace("\\in","\\out")
print (file)
img1 = cv2.imread(position,-1)
img = manipu_cv(img1,0)
retval, dst = cv2.threshold(img1, 0, 65535, 0)
#cv2.imshow('binary', dst)
#cv2.waitKey(0)
cv2.imwrite(out_position,img)
test = 0
#label=[]
#position = []
#out_position = []
#for file in files: #遍历文件夹
# position.append(file)
# out_position.append(file.replace("\\in","\\out"))
# print (file)
#img1 = cv2.imread(position[0],-1)
#img2 = cv2.imread(position[1],-1)
#p_set = np.unique(img1)
#label = np.unique(np.append(label,p_set))
#p_set = np.unique(img2)
#label = np.unique(np.append(label,p_set))
#img = manipu_cv(img1, label)
#cv2.imwrite(out_position[0].replace(".png", ".tif"),img)
#img = manipu_cv(img2, label)
#cv2.imwrite(out_position[1].replace(".png", ".tif"),img)
#test=0
#img = cv2.imread(position,-1)
#cv2.namedWindow("Image")
#cv2.imshow("Image",img)
#cv2.waitKey(0)
##释放窗口
#cv2.destroyAllWindows()
#p_set = np.unique(img)
#label = np.unique(np.append(label,p_set))
#img2 = manipu_cv(img)
#test =1
#cv2.imwrite(out_position.replace(".png", ".tif"),img2)
#test =1