image.md 4.06 KB
Newer Older
Kai Chen's avatar
Kai Chen committed
1
2
## Image

youkaichao's avatar
youkaichao committed
3
This module provides some image processing methods, which requires `opencv` to be installed.
Kai Chen's avatar
Kai Chen committed
4
5

### Read/Write/Show
Kai Chen's avatar
Kai Chen committed
6

Kai Chen's avatar
Kai Chen committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
To read or write images files, use `imread` or `imwrite`.

```python
import mmcv

img = mmcv.imread('test.jpg')
img = mmcv.imread('test.jpg', flag='grayscale')
img_ = mmcv.imread(img) # nothing will happen, img_ = img
mmcv.imwrite(img, 'out.jpg')
```

To read images from bytes

```python
with open('test.jpg', 'rb') as f:
    data = f.read()
img = mmcv.imfrombytes(data)
```

To show an image file or a loaded image

```python
mmcv.imshow('tests/data/color.jpg')
lizz's avatar
lizz committed
30
# this is equivalent to
Kai Chen's avatar
Kai Chen committed
31
32
33
34
35
36

for i in range(10):
    img = np.random.randint(256, size=(100, 100, 3), dtype=np.uint8)
    mmcv.imshow(img, win_name='test image', wait_time=200)
```

Kai Chen's avatar
Kai Chen committed
37
### Color space conversion
Kai Chen's avatar
Kai Chen committed
38

Kai Chen's avatar
Kai Chen committed
39
Supported conversion methods:
Kai Chen's avatar
Kai Chen committed
40

Kai Chen's avatar
Kai Chen committed
41
42
43
44
45
46
47
48
49
50
51
52
53
54
- bgr2gray
- gray2bgr
- bgr2rgb
- rgb2bgr
- bgr2hsv
- hsv2bgr

```python
img = mmcv.imread('tests/data/color.jpg')
img1 = mmcv.bgr2rgb(img)
img2 = mmcv.rgb2gray(img1)
img3 = mmcv.bgr2hsv(img)
```

Kai Chen's avatar
Kai Chen committed
55
### Resize
Kai Chen's avatar
Kai Chen committed
56

Kai Chen's avatar
Kai Chen committed
57
58
59
There are three resize methods. All `imresize_*` methods have an argument `return_scale`,
if this argument is `False`, then the return value is merely the resized image, otherwise
is a tuple `(resized_img, scale)`.
Kai Chen's avatar
Kai Chen committed
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75

```python
# resize to a given size
mmcv.imresize(img, (1000, 600), return_scale=True)

# resize to the same size of another image
mmcv.imresize_like(img, dst_img, return_scale=False)

# resize by a ratio
mmcv.imrescale(img, 0.5)

# resize so that the max edge no longer than 1000, short edge no longer than 800
# without changing the aspect ratio
mmcv.imrescale(img, (1000, 800))
```

Kai Chen's avatar
Kai Chen committed
76
### Rotate
Kai Chen's avatar
Kai Chen committed
77

Kai Chen's avatar
Kai Chen committed
78
79
80
81
82
To rotate an image by some angle, use `imrotate`. The center can be specified,
which is the center of original image by default. There are two modes of rotating,
one is to keep the image size unchanged so that some parts of the image will be
cropped after rotating, the other is to extend the image size to fit the rotated
image.
Kai Chen's avatar
Kai Chen committed
83
84
85

```python
img = mmcv.imread('tests/data/color.jpg')
Kai Chen's avatar
Kai Chen committed
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103

# rotate the image clockwise by 30 degrees.
img_ = mmcv.imrotate(img, 30)

# rotate the image counterclockwise by 90 degrees.
img_ = mmcv.imrotate(img, -90)

# rotate the image clockwise by 30 degrees, and rescale it by 1.5x at the same time.
img_ = mmcv.imrotate(img, 30, scale=1.5)

# rotate the image clockwise by 30 degrees, with (100, 100) as the center.
img_ = mmcv.imrotate(img, 30, center=(100, 100))

# rotate the image clockwise by 30 degrees, and extend the image size.
img_ = mmcv.imrotate(img, 30, auto_bound=True)
```

### Flip
Kai Chen's avatar
Kai Chen committed
104

Kai Chen's avatar
Kai Chen committed
105
106
107
108
109
110
111
112
113
114
To flip an image, use `imflip`.

```python
img = mmcv.imread('tests/data/color.jpg')

# flip the image horizontally
mmcv.imflip(img)

# flip the image vertically
mmcv.imflip(img, direction='vertical')
Kai Chen's avatar
Kai Chen committed
115
116
117
```

### Crop
Kai Chen's avatar
Kai Chen committed
118

Kai Chen's avatar
Kai Chen committed
119
`imcrop` can crop the image with one or some regions, represented as (x1, y1, x2, y2).
Kai Chen's avatar
Kai Chen committed
120
121
122
123
124

```python
import mmcv
import numpy as np

125
img = mmcv.imread('tests/data/color.jpg')
Kai Chen's avatar
Kai Chen committed
126
127
128

# crop the region (10, 10, 100, 120)
bboxes = np.array([10, 10, 100, 120])
129
patch = mmcv.imcrop(img, bboxes)
Kai Chen's avatar
Kai Chen committed
130
131

# crop two regions (10, 10, 100, 120) and (0, 0, 50, 50)
Kai Chen's avatar
Kai Chen committed
132
bboxes = np.array([[10, 10, 100, 120], [0, 0, 50, 50]])
133
patches = mmcv.imcrop(img, bboxes)
Kai Chen's avatar
Kai Chen committed
134

Kai Chen's avatar
Kai Chen committed
135
# crop two regions, and rescale the patches by 1.2x
136
patches = mmcv.imcrop(img, bboxes, scale_ratio=1.2)
Kai Chen's avatar
Kai Chen committed
137
138
139
```

### Padding
Kai Chen's avatar
Kai Chen committed
140

Kai Chen's avatar
Kai Chen committed
141
142
There are two methods `impad` and `impad_to_multiple` to pad an image to the
specific size with given values.
Kai Chen's avatar
Kai Chen committed
143
144

```python
145
img = mmcv.imread('tests/data/color.jpg')
Kai Chen's avatar
Kai Chen committed
146
147

# pad the image to (1000, 1200) with all zeros
148
img_ = mmcv.impad(img, shape=(1000, 1200), pad_val=0)
Kai Chen's avatar
Kai Chen committed
149
150

# pad the image to (1000, 1200) with different values for three channels.
151
152
153
154
155
img_ = mmcv.impad(img, shape=(1000, 1200), pad_val=[100, 50, 200])

# pad the image on left, right, top, bottom borders with all zeros
img_ = mmcv.impad(img, padding=(10, 20, 30, 40), pad_val=0)

Kai Chen's avatar
Kai Chen committed
156
# pad the image on left, right, top, bottom borders with different values
157
158
# for three channels.
img_ = mmcv.impad(img, padding=(10, 20, 30, 40), pad_val=[100, 50, 200])
Kai Chen's avatar
Kai Chen committed
159
160
161

# pad an image so that each edge is a multiple of some value.
img_ = mmcv.impad_to_multiple(img, 32)
162
```