Task
Reference
主要是高明豪dalao代码归纳
- Ai Challenger 2018 Competitions 农作物病害检测
- imgaug_example
- 图像库差别学习
Imgaug
构造数据增强器
1 | from imgaug import augmenters as iaa |
使用数据增强器:
1 | file_list = os.listdir('1_aug/') |
Opencv
灰度图 To 颜色3通道图
1
2img1 = cv2.imread(it1, cv2.IMREAD_GRAYSCALE)
img1 = cv2.cvtColor(img1, cv2.COLOR_GRAY2BGR)模糊去噪与梯度滤波处理边缘检测——拉普拉斯算子+中值模糊+反转正则化
1
2
3
4
5
6
7
8
9
10
11
12
13def edge_aug(img):
img_process_1 = cv2.medianBlur(img, 7)
img_process_1_gray = cv2.cvtColor(img_process_1, cv2.COLOR_BGR2GRAY)
cv2.Laplacian(img_process_1_gray, cv2.CV_8U, img_process_1_gray, ksize=5)
normalizedInverse = (1.0 / 255) * (255 - img_process_1_gray)
channels = cv2.split(img)
for channel in channels:
channel[:] = channel * normalizedInverse
dst = cv2.merge(channels)
return dstCanny边缘检测
1
2
3
4
5
6
7
8
9
10
11
12
13pre_path = 'rec_res/'
tar_path = 'edge_enforce/'
for img_path in os.listdir(pre_path):
img = cv2.imread(pre_path + img_path)
# dst_temp = edge_aug(img)
edges = cv2.Canny(img,100,200)
cv2.imwrite(tar_path + img_path, edges)
#plt.imshow(edges, cmap='gray')
#plt.show()角点检测
1
2
3
4
5
6
7img = cv2.imread('621669_7600000020025.jpeg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = np.float32(gray)
dst = cv2.cornerHarris(gray, 2, 23, 0.04)
img[dst>0.01 * dst.max()] = [0, 0, 255]
plt.imshow(img)
plt.show()Resize
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24def resize_im(im, scale, max_scale=None):
f = float(scale) / min(im.shape[0], im.shape[1])
if max_scale != None and f * max(im.shape[0], im.shape[1]) > max_scale:
f = float(max_scale) / max(im.shape[0], im.shape[1])
return cv2.resize(im, None, None, fx=f, fy=f, interpolation=cv2.INTER_LINEAR), f
img = cv2.imread('621669_7600000020025.jpeg')
SCALE=900 # 600
MAX_SCALE=1500 # 1200
TEXT_PROPOSALS_WIDTH=0 # 16
MIN_NUM_PROPOSALS=0 # 2
MIN_RATIO=0.01 # 0.5
LINE_MIN_SCORE=0.6 # 0.9
MAX_HORIZONTAL_GAP=30 # 50
TEXT_PROPOSALS_MIN_SCORE=0.7 # 0.7
TEXT_PROPOSALS_NMS_THRESH=0.3 # 0.2
TEXT_LINE_NMS_THRESH=0.3
MIN_V_OVERLAPS=0.6 # 0.7
MIN_SIZE_SIM=0.6 # 0.7
img, scale = resize_im(img, scale=SCALE,
max_scale=MAX_SCALE)
plt.imshow(img_temp)
plt.show()
Pillow
翻转
1
2
3
4
5from PIL import Image,ImageEnhance,ImageFilter,ImageOps
img = Image.open(x)
img_flip_left_right = img.transpose(Image.FLIP_LEFT_RIGHT)
img_flip_top_bottom = img.transpose(Image.FLIP_TOP_BOTTOM)旋转
1
2
3
4
5
6
7
8from PIL import Image,ImageEnhance,ImageFilter,ImageOps
img = Image.open(x)
img_rotate_90 = img.transpose(Image.ROTATE_90)
img_rotate_180 = img.transpose(Image.ROTATE_180)
img_rotate_270 = img.transpose(Image.ROTATE_270)
img_rotate_90_left = img_flip_left_right.transpose(Image.ROTATE_90)
img_rotate_270_left = img_flip_left_right.transpose(Image.ROTATE_270)3.亮度
1
2
3
4
5
6from PIL import Image,ImageEnhance,ImageFilter,ImageOps
img = Image.open(x)
enh_bri = ImageEnhance.Brightness(img)
brightness = 1.5
image_brightened = enh_bri.enhance(brightness)色彩
1
2
3
4
5
6from PIL import Image,ImageEnhance,ImageFilter,ImageOps
img = Image.open(x)
enh_col = ImageEnhance.Color(img)
color = 1.5
image_colored = enh_col.enhance(color)对比度
1
2
3
4
5
6from PIL import Image,ImageEnhance,ImageFilter,ImageOps
img = Image.open(x)
enh_con = ImageEnhance.Contrast(img)
contrast = 1.5
image_contrasted = enh_con.enhance(contrast)锐度
1
2
3
4
5
6from PIL import Image,ImageEnhance,ImageFilter,ImageOps
img = Image.open(x)
enh_sha = ImageEnhance.Sharpness(img)
sharpness = 3.0
image_sharped = enh_sha.enhance(sharpness)
Skimage
加高斯噪声
1
2
3
4
5
6
7
8
9
10
11
12from skimage.util import random_noise
import cv2
# 加高斯噪声
def addNoise(img):
'''
注意:输出的像素是[0,1]之间,所以乘以5得到[0,255]之间
'''
return random_noise(img, mode='gaussian', seed=13, clip=True)*255
cv_image = cv2.imread(x)
# 高斯噪声
gau_image = addNoise(cv_image)随机改变亮度
1
2
3
4
5
6
7
8
9
10
11
12
13import cv2
import random
from skimage import exposure
def changeLight(img):
rate = random.uniform(0.5, 1.5)
# print(rate)
img = exposure.adjust_gamma(img, rate) #大于1为调暗,小于1为调亮;1.05
return img
cv_image = cv2.imread(x)
# 随机改变
light = changeLight(cv_image)
Other Skill
搜寻图片名字
1
2
3
4
5
6re.search和re.match区别:match只匹配开头
- match
#re.match('row/0/', 'row/0/_001a_0.png').span()
#(0, 6)
- search
#shutil.copyfile(it, tar_pre_path + it[re.search(ori_pre_path, it).span()[1]:])shutil复制图片
1
2
3
4
5ori_pre_path = './0_aug/'
tar_pre_path = './images/'
for it in glob.glob(ori_pre_path + '*.jpg'):
shutil.copyfile(it, tar_pre_path + it[re.search(ori_pre_path, it).span()[1]:])图像拼接&裁剪
1
2
3
4
5
6①
for it in test_list[1:4]:
img_res = np.hstack([img_res, it])
②
img_cut = test_list[4][:, :test_list[4].shape[1] // 4 * 3, :]Keras—data_generater的写法
TODO
- pydicom (医学,dcm)
- SimpleITK (医学,dcm)