Code Monkey home page Code Monkey logo

image-rotation-and-cropping-tensorflow's Introduction

Image Rotation and Cropping in TensorFlow

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.

Goal

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.

Core Functions

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.

Prerequisites

  • 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

Prepare the Tiny ImageNet

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.

Convert to TFRecords

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.

Visualize Original, Rotated and Cropped Image

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

Example1

  • Rotated Image

Example2

  • Rotated and Cropped Image

Example3

Reference

Author

Byung Soo Ko / [email protected]

image-rotation-and-cropping-tensorflow's People

Contributors

kobiso avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.