forked from chuliuT/Tianchi_Fabric_defects_detection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tianchi_vertical_line_det.py
108 lines (96 loc) · 3.19 KB
/
tianchi_vertical_line_det.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
from skimage import io,data,color
from matplotlib import pyplot as plt
import numpy as np
import cv2
import os
import random
import skimage
from skimage.exposure import histogram
from skimage import feature
import json
detect_img_root='./detect_images/'
normal_img_root='./normal2_images/'
aug_dir='./normal_images_aug/'
detect_name=os.listdir(detect_img_root)
normal_name=os.listdir(normal_img_root)
# # for test
# img=io.imread(img_root+file_name[0])
# # img=np.array(img)
# print(img.shape)
# cv2.imshow('image',img)
# cv2.waitKey(0)
# random.shuffle(normal_name)
# print(img.shape)
# print(img.shape) #显示尺寸
# print(img.shape[0]) #图片高度
# print(img.shape[1]) #图片宽度
# print(img.shape[2]) #图片通道数
# print(img.size) #显示总像素个数
# print(img.max()) #最大像素值
# print(img.min()) #最小像素值
# print(img.mean())
# random.seed(2019)#设定随机数种子
# # 正常样本扩增
# result=[]
# for i in range(len(normal_name)):
# # print(normal_name[i])
# img=cv2.imread(normal_img_root+normal_name[i])
# # print(img.shape)
# height,width=img.shape[0],img.shape[1]
# # print(height,width)
# defect_img=cv2.imread(detect_img_root+detect_name[i])
# # print(detect_name[i].split('.')[0].split('_')[1])
# label=detect_name[i].split('.')[0].split('_')[1]
# defect_h,defect_w=defect_img.shape[0],defect_img.shape[1]
# # print(defect_h)
# # print((height-defect_h))
# # h_range=abs(height-defect_h)
# # print((width-defect_w))
# # w_range=abs(width-defect_w)
# for j in range(5):
# xmin=random.randint(1,2446)
# ymin=random.randint(1,1000)
# xmax=xmin+defect_w
# ymax=ymin+defect_h
# print(xmin,ymin,xmax,ymax)
# print(img[xmin:xmax,ymin:ymax].shape,defect_img.shape)
# img=np.array(img)
# defect_img=np.array(defect_img)
# try:
# img[ymin:ymax,xmin:xmax]=defect_img
# result.append({'name': normal_name[i], 'category': label, 'bbox': [xmin,ymin,xmax,ymax]})
# # cv2.rectangle(img, (xmin,ymin), (xmax,ymax), (0,255,0), 2)
# # cv2.imshow('image',img)
# # cv2.waitKey(0)
# except:
# continue
# cv2.imwrite(aug_dir+normal_name[i],img)
# json_name='annotation_normal.json'
# with open(json_name,'w') as fp:
# json.dump(result, fp, indent = 4, separators=(',', ': '))
# # if (defect_w*defect_h)<(32*32):
#带桌面数据处理,检测垂直边
vertical_line_x=2446/2
kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (5, 5)) # 十字形结构
for i in range(len(normal_name)):
img=cv2.imread(normal_img_root+normal_name[i])
img_gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
img=skimage.filters.sobel_v(img_gray)
img=cv2.dilate(img, kernel)
print(img.shape)
row_index=[]
for k in range(img.shape[1]):
count_list=list(img[:,k]*255>50)
row_index.append(count_list.count(True))
print(np.argmax(row_index))
cmp_point_x=np.argmax(row_index)
img=cv2.imread(normal_img_root+normal_name[i])
cmp_img=img.copy()
if cmp_point_x < vertical_line_x:
cmp_img=cmp_img[:,cmp_point_x:]
else:
cmp_img=cmp_img[:,:cmp_point_x]
# plot_for_show=np.concatenate((img,cmp_img),axis=1)
plt.figure()
plt.imshow(cmp_img)
plt.show()