Skip to content

Commit

Permalink
docs(transform): 随机裁剪
Browse files Browse the repository at this point in the history
  • Loading branch information
zjZSTU committed May 10, 2020
1 parent 76ad53a commit cfb0fb1
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 1 deletion.
Binary file added docs/data/transforms/imgs/random_crop.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 24 additions & 0 deletions docs/data/transforms/随机裁剪.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

# 随机裁剪

* `py/ssd/data/transforms/random_sample_crop.py`

对图像进行随机裁剪操作

## 实现流程

其实现比较复杂,因为需要考虑到相应的标注边界框的计算。实现流程如下:

1. 计算随机裁剪的宽/高。符合以下要求
1. 裁剪宽的取值为[0.3*width, width]
2. 裁剪高的取值为[0.3*height, height]
3. 高/宽比例(h/w)在(0.5, 2)之间
2. 计算随机裁剪的左上角坐标
3. 计算裁剪边界框和标注边界框的IoU。符合以下要求
1. 设置了5个最小IoU([0.1, 0.3, 0.7, 0.9, None])
2. 每次随机选择一个最小IoU,如果为None,则直接返回原图
4. 计算标注边界框是否位于裁剪图像中。如果其标注边界框中心位于裁剪图像,则进一步计算其长宽

## 示例

![](./imgs/random_crop.png)
3 changes: 2 additions & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@ nav:
- 引言: data/transforms/index.md
- 格式转换: data/transforms/格式转换.md
- 光度扭曲: data/transforms/光度扭曲.md
- 图像扩展: data/transforms/图像扩展.md
- 图像扩展: data/transforms/图像扩展.md
- 随机裁剪: data/transforms/随机裁剪.md
30 changes: 30 additions & 0 deletions py/ssd/data/transforms/random_sample_crop.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,33 @@ def __call__(self, image, boxes=None, labels=None):
current_boxes[:, 2:] -= rect[:2]

return current_image, current_boxes, current_labels


if __name__ == '__main__':
import cv2
import numpy as np
import matplotlib.pyplot as plt

img = cv2.imread('../../../datasets/008591.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
boxes = np.array([[33, 44, 123, 210]])
labels = np.array([3])

model = RandomSampleCrop()

f = plt.figure()
rows = 3
cols = 3

plt.subplot(rows, cols, 1)
plt.title('src')
plt.imshow(img), plt.axis('off')
for i in range(rows):
for j in range(cols):
if i == 0 and j == 0:
continue
plt.subplot(rows, cols, i * cols + j + 1)
res, res_boxes, _ = model(img.astype(np.float32), boxes, labels)
plt.imshow(res.astype(np.uint8)), plt.axis('off')
print('{}-{} : {} - {}'.format(i, j, res_boxes, labels))
plt.show()

0 comments on commit cfb0fb1

Please sign in to comment.