GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together. If nothing happens, download GitHub Desktop and try again. If nothing happens, download Xcode and try again. If nothing happens, download the GitHub extension for Visual Studio and try again. This is an implementation and visualization of image rotation and cropping out black borders in TensorFlow.
TensorFlow support only image rotation function tf. However, when you rotate an image with this function, there will be black noise on each border as below.
Split the code
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.Google Vision API in Python (Part 6): Detect Crop Hints
If you do not want to run the code or see the visualization, you can just copy and paste the core functions. Download the Tiny ImageNet in this link and unzip it. 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. Then TFRecords files will be created in the designated path you defined. Skip to content. Dismiss Join GitHub today GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together. Sign up.
Image rotation and cropping out the black borders in TensorFlow. Jupyter Notebook Python.Almost all the operations in this section is mainly related to Numpy rather than OpenCV.
A good knowledge of Numpy is required to write better optimized code with OpenCV. Examples will be shown in Python terminal since most of them are just single line codes. You can access a pixel value by its row and column coordinates.
For grayscale image, just corresponding intensity is returned. Numpy is a optimized library for fast array calculations. So simply accessing each and every pixel values and modifying it will be very slow and it is discouraged. Above mentioned method is normally used for selecting a region of array, say first 5 rows and last 3 columns like that. For individual pixel access, Numpy array methods, array.
But it always returns a scalar. So if you want to access all B,G,R values, you need to call array. Image properties include number of rows, columns and channels, type of image data, number of pixels etc. Shape of image is accessed by img. It returns a tuple of number of rows, columns and channels if image is color :. If image is grayscale, tuple returned contains only number of rows and columns.
Subscribe to RSS
So it is a good method to check if loaded image is grayscale or color image. Total number of pixels is accessed by img. Image datatype is obtained by img. Sometimes, you will have to play with certain region of images.
For eye detection in images, first perform face detection over the image until the face is found, then search within the face region for eyes. This approach improves accuracy because eyes are always on faces :D and performance because we search for a small area. ROI is again obtained using Numpy indexing. Here I am selecting the ball and copying it to another region in the image:.
The B,G,R channels of an image can be split into their individual planes when needed. Then, the individual channels can be merged back together to form a BGR image again.
The dark mode beta is finally here. Change your preferences any time. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Some videos have frames that have black strips like borders. I have to remove them from the frames. I came up with a crude solution:.
Here I find the columns that have the same elements and all the videos that I have have black borders. But even if I increase the maximum length in the function checkEqual from 1 to 20 or 40, the whole black strip is not deleted.
This is the original image:. This is the image after running the program:.
This problem was already solved in this answer. I think that you will find that the problem goes away if you work from the other side of the image, as you are checking the first column col - it's black so you delete it and the black col becomes col then you check the col - skipping the new col If you start from the max it would work or if you stay on any given col once you have deleted it.
Alternatively you can make a list of which to delete, reverse it and then do the deletions. Learn more. Removing borders from an image in Python Ask Question.
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It only takes a minute to sign up. Image Source. The code does it, however, it does with 2 for s and it takes a long time! How can I optimize it? When read with cv2. Now, NumPy supports various vectorization capabilities, which we can use to speed up things quite a bit. To solve our case, one method would be to look for rows and columns that have at least one pixel along rows and columns that is greater than some lower limit or threshold as a pixel value.
So, if you are sure that the black areas are absolutely zeros, you can set that threshold as 0. Next up, you can use these boolean arrays to index into the image data for extraction of valid bounding box using broadcasted indexing with np. Finally, we index into image data with it for the final extracted data, which is the required bounding box data. To crop the image while keeping the inner all black rows or columns, the implementation would be close to the previous method.
The basic idea here would be getting the start, stop indices along rows and columns that decide the bounding box. We will start off with the same mask of ANY match along rows and columns as used in the previous one.
The dark mode beta is finally here. Change your preferences any time. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. At the moment I have developed the below code which rotates an input image, padding it with black borders, giving me A.
What I want is B - the largest possible area crop window within the rotated image. I call this the axis-aligned boundED box. This is essentially the same as Rotate and crophowever I cannot get the answer on that question to work. Additionally, that answer is apparently only valid for square images. My images are rectangular. Looking for the axis aligned rectangle between two parallel lines that has maximal area is an optimization problem with one parameter, e.
Let s denote the distance between the two parallel lines it will turn out to be the shorter side of the rotated rectangle. Then the sides ab of the sought-after rectangle have a constant ratio with xs-xresp.
The Mouse Vs. The Python
This solution is only valid if this maximal rectangle fits into the rotated rectangle. Therefore the diagonal EG must not be longer than the other side l of the rotated rectangle. This leads to the equation. So, after investigating many claimed solutions, I have finally found a method that works; The answer by Andri and Magnus Hoff on Calculate largest rectangle in a rotated rectangle. Simply place this image cropped to demonstrate that it works with non-square images in the same directory as the above file, then run it.
Congratulations for the great work! Maybe this approach could be helpful to other people. I personally needed this function in TensorFlow and thanks for Aaron Snoswell, I could implement this function. If you need further implementation of example and visualization in TensorFlow, you can use this repository.
But I've modified your code so it will do it. Uses top left pixel as a guide by default, or you can also specify the color to remove. Learn more. Asked 7 years, 11 months ago.
Active 10 months ago. Viewed 26k times. Is there a simple solution to trim whitespace on the image in PIL?
ImageMagick has easy support for it in following way: convert test. To add some fuzzy cropping. May be PIL has some built in functions for it? Or there is some fast solution? Eugene Nagorny Eugene Nagorny 1, 2 2 gold badges 15 15 silver badges 31 31 bronze badges. I know the code is exactly the same there, but it can also be found here - gist.
Active Oldest Votes. It gets the border colour from the top left pixel, using getpixelso you don't need to pass the colour.The Pillow package has a very easy method of adding such borders to your images via its ImageOps module.
As usual, you will need to have Pillow installed to do any of the examples in this article. The focus of this article will be on using the ImageOps module to add our borders.
For this example, we will use this photo of a neat butterfly I took. Is it an int or a tuple or something else? Otherwise we raise an error because we passed in an invalid type. Finally we save the image. This is the result I got:. As you can see, when you just pass an integer in as your border, it applies to all four sides of the image.
If we want the border on the top and bottom to be different then the right and left, we need to specify that. Here we want to add a 10 pixel border to the left and right sides and a 50 pixel border to the top and bottom of the image. If you run this code, you should get the following result:. In this example, we tell Pillow that we want a border where the left side is 10 pixels, the top is 30 pixels, the right is 20 pixels and the bottom is 50 pixels wide. When I ran this code, I got this:.
The default was obviously black. You will note that we added a new parameter here that specifies the color we want our border to be. The default is black, which is zero 0. Here is the result:. If you do, you can get your border to look like this:. At this point you should know how to add simple borders to your photos. You can also change the color of the border to pretty much any color under the sun.
Take some time to play around with the code and see what you can come up with! Adding a Border The focus of this article will be on using the ImageOps module to add our borders.
This is the result I got: As you can see, when you just pass an integer in as your border, it applies to all four sides of the image. Wrapping Up At this point you should know how to add simple borders to your photos.