-
Notifications
You must be signed in to change notification settings - Fork 11
/
crop.py
92 lines (75 loc) · 2.68 KB
/
crop.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
'''
lanhuage: python
Descripttion:
version: beta
Author: xiaoshuyui
Date: 2020-10-23 15:40:56
LastEditors: xiaoshuyui
LastEditTime: 2020-11-10 12:16:57
'''
import cv2
import numpy as np
import skimage.util.noise as snoise
from convertmask.utils.auglib.optional.generatePolygon import (
generatePolygon, generateRectangle)
from skimage import io
def rectangleCrop(img: np.ndarray, startPoint: tuple = None, noise=False):
imgShape = img.shape
mask = generateRectangle(imgShape, startPoint)
mask[mask != 255] = 1
mask[mask == 255] = 0
if noise:
noisedMask = np.ones(imgShape) * 255
noisedMask = snoise.random_noise(noisedMask, 's&p') * 255
noisedMask = np.array(noisedMask * (1 - mask), dtype=np.uint8)
return img * mask + noisedMask
return img * mask
def polygonCrop(img: np.ndarray,
startPoint: tuple = None,
convexHull=False,
noise=False):
imgShape = img.shape
mask = generatePolygon(imgShape, startPoint, convexHull)
mask[mask != 255] = 1
mask[mask == 255] = 0
if noise:
noisedMask = np.ones(imgShape) * 255
if len(imgShape) == 3:
mask = cv2.merge([mask, mask, mask])
noisedMask = snoise.random_noise(noisedMask, 's&p') * 255
noisedMask = np.array(noisedMask * (1 - mask), dtype=np.uint8)
return img * mask + noisedMask
return img * mask
def multiRectanleCrop(img: np.ndarray, number: int = 1, noise=False):
if isinstance(img,str):
img = io.imread(img)
imgShape = img.shape
mask = np.zeros(imgShape, dtype=np.uint8)
for _ in range(number):
mask += generateRectangle(imgShape)
mask[mask != 255] = 1
mask[mask == 255] = 0
if noise:
noisedMask = np.ones(imgShape) * 255
noisedMask = snoise.random_noise(noisedMask, 's&p') * 255
noisedMask = np.array(noisedMask * (1 - mask), dtype=np.uint8)
return img * mask + noisedMask
return img * mask
def multiPolygonCrop(img: np.ndarray,
number: int = 1,
noise=False,
convexHull=False):
imgShape = img.shape
mask = np.zeros((imgShape[0], imgShape[1]), dtype=np.uint8)
for _ in range(number):
mask += generatePolygon(imgShape, convexHull=convexHull)
mask[mask != 255] = 1
mask[mask == 255] = 0
if noise:
noisedMask = np.ones(imgShape) * 255
if len(imgShape) == 3:
mask = cv2.merge([mask, mask, mask])
noisedMask = snoise.random_noise(noisedMask, 's&p') * 255
noisedMask = np.array(noisedMask * (1 - mask), dtype=np.uint8)
return img * mask + noisedMask
return img * mask