This is an implementation and visualization of image rotation and cropping out black borders in TensorFlow.
TensorFlow support only image rotation function tf.contrib.image.rotate(images, angles, interpolation, name)
.
However, when you rotate an image with this function, there will be black noise on each border as below.
So, we want to cropping out this black borders in TensorFlow, especially when the image is loaded as Tensor and it has to go through preprocessing phase. The implementation include example and visualization with Tiny Imagenet.
If you do not want to run the code or see the visualization, you can just copy and paste the core functions.
In read_tfrecord.py file, _rotate_and_crop(image, output_height, output_width, rotation_degree, do_crop)
and _largest_rotated_rect(w, h, angle)
are core functions.
- Python 3.4+
- TensorFlow 1.5+
- Jupyter Notebook
- Python packages: requirements.txt
- Simply install it by running :
pip install -r /path/to/requirements.txt
in the shell
- Simply install it by running :
Download the Tiny ImageNet in this link and unzip it.
Set the path of the dataset on variable TINY_IMAGENET_DIRECTORY
in build_tfrecords.ipynb
file.
As test set does not include class labels and bounding boxes, validation set will be used as test set in this implementation. And training set will be divided with certain percentage (as you defined) into training set and validation set. Each data set (training, validation and test) will have iamges, labels and bounding box information.
To convert Tiny ImageNet to TFRecords, set each requiring path in build_tfrecords.ipynb
and run all cell.
Then TFRecords files will be created in the designated path you defined.
Note that you can set the validation ratio in the variable VALIDATION_RATIO
.
You can check and visualize TFRecords file in check_tfrecords.ipynb
.
In read_tfrecord.read_tfrecord()
function, you can set rotation_degree
and do_crop
arguments to rotate and crop images.
- Original Image
- Rotated Image
- Rotated and Cropped Image
- StackOverflow: Rotate image and crop out black borders
- TensowFlow example code for converting ImageNet data to TFRecords file format
Byung Soo Ko / kobiso62@gmail.com