|
|
|
# Image Crop Implementation with CV2
|
|
|
|
|
|
|
|
*author: David Wang*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<br />
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Description
|
|
|
|
|
|
|
|
An image crop operator implementation with OpenCV.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<br />
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Code Example
|
|
|
|
|
|
|
|
Crop the face from 'avengers.jpg'.
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
|
|
|
import towhee
|
|
|
|
|
|
|
|
towhee.glob['path']('./avengers.jpg') \
|
|
|
|
.image_decode['path', 'img']() \
|
|
|
|
.face_detection.retinaface['img', ('box','score')]()\
|
|
|
|
.image_crop[('img', 'box'), 'crop'](clamp = True)\
|
|
|
|
.select['img','crop']()\
|
|
|
|
.show()
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
<img src="./result2.png" height="150px"/>
|
|
|
|
|
|
|
|
<br />
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Factory Constructor
|
|
|
|
|
|
|
|
Create the operator via the following factory method
|
|
|
|
|
|
|
|
***image_crop(clamp = True)***
|
|
|
|
|
|
|
|
|
|
|
|
**Parameters:**
|
|
|
|
|
|
|
|
**clamp:** *bool*
|
|
|
|
|
|
|
|
If set True, coordinates of bounding boxes would be clamped into image size.
|
|
|
|
|
|
|
|
<br />
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Interface
|
|
|
|
|
|
|
|
An image crop operator takes an image and bounding boxes as input. It cropes the image into ROIs(region of interest).
|
|
|
|
|
|
|
|
|
|
|
|
**Parameters:**
|
|
|
|
|
|
|
|
**img:** *towhee.types.Image (a sub-class of numpy.ndarray)*
|
|
|
|
|
|
|
|
The image need to be cropped.
|
|
|
|
|
|
|
|
**bboxes:** *numpy.ndarray*
|
|
|
|
|
|
|
|
The nx4 numpy tensor for n bounding boxes need to crop, each row is formatted as (x1, y1, x2, y2).
|
|
|
|
|
|
|
|
**Returns**: *towhee.types.Image (a sub-class of numpy.ndarray)*
|
|
|
|
|
|
|
|
The cropped image data as numpy.ndarray.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|