An empty wall is a blank canvas.

It’s 5 here in the morning in China and I am watching the sun rise. And it’s got me thinking about all the simple things in life. How, if we have a dream, it always seems like life has a way of trying to slow us down, stop us, or just muddle with our plans.

So, with that simplicity in mind (and the sunshine beaming in) I figured today we would just keep things simple and focused. Build an easy foundation for image manipulation using Python and OpenCV.

If you are wondering what the wall is for, it is my blank canvas, my foundation if you will. My walls in my own apartment are a little empty and I figured I could use OpenCV and Python to think about ways to decorate my walls with new pictures. We will go over some of the basic ways you can alter/manipulate images, and in the next post we look at how to use bounding boxes to select areas of image and then fill in our picture frames.

In today’s post, we are going to learn how to load images, perform color conversion, manipulate images by cropping and resizing, and how to save our new images. 

For this tutorial, I used OpenCV 3.4.1 and Python 3.6.

If you have any questions regarding OpenCV and its numerous applications, please check out this link. There are numerous guides here that cover topics, such as image processing, object detection, machine learning, etc.

Load an Image

Huli (狐狸) means fox in Mandarin. Image example: ‘foxinleaves.jpg’

We are going to use the image of the fox above for this post. If you want to use the same image, simply right click on the image and save it. Otherwise, feel free to use your own image.

What’s going on here? Well, in line 1 we import the OpenCV library.

Line 4 reads in the image using the path to the image. The imread( ) function returns a NumPy array (for more information on NumPy arrays, click here). Line 5 displays the image to the screen using imshow(‘name of window’, name of image in line 4).

In lines 7 – 8, we wait until the user types any key using waitKey(0), and then we will use destroyAllWindows( ) just to make sure all windows are closed when we exit. (Note: If you only want to close one window after you call it, you can use destroyWindow(window name) instead.)

Color Transformation

In OpenCV, there are numerous ways to process and manipulate images. Here we are going to discuss only 3 of the possible conversions. For a full list or better understanding of the math behind the transformations, look at the OpenCV: Color Conversions documentation.

Running the above code will give us a gray fox.

Grayscale fox.

The transformation happens in line 7. cvtColor(image, flag) allows us to take an image and set a flag, in this case cv2.RGB2GRAY, and perform the RGB image to grayscale transformation.

Lines 9 – 11 displays the two images for comparison. Press any key to close and destroy the windows.

OpenCV is not just limited to gray scale. In the code above, if you change the flag in line 7, you can perform other color conversions. Try changing the flag to cv2.COLOR_RGB2XYZ and see what happens.

The CIE XYZ color system was created in 1931 and is designed to better reflect the colors that the human eye can perceive. Interested in CIE XYZ? Check out this great guide.

CIE 1931 fox.

Changing the flag in line 7 to cv2.COLOR_RGB2BGR will create an image where the blue field takes preference over the red and green fields.

This will convert the image using the BGR scale.

BGR fox.

Scaling (Resizing)

Let’s start by getting the original size of the image using the function shape.

You will see (357, 500, 3) displayed for this image. The first number refers to the height of the image (think of it as the y coordinates), the second number refers to the width (the x coordinates), and the third refers to the number of color channels, in this case 3, RGB or Red-Green-Blue.

We can simply specify the width and height of the portion of the image that we want to crop.

This gives you a small, 100 x 100,  distorted fox.

Small fox.

Resizing the image can and most often will cause distortion depending upon how you want to resize your image. In line 1, the function resize( ) takes the arguments (image, the new size, the method we want to use to interpolate the image). Interpolation occurs when you want to resize or distort an image. The process tries to make approximations for the unknown pixel locations by looking at the color and intensity of the surrounding pixels, and there are other methods for interpolation, as well. In this case, we will use cv2.INTER_AREA (What is INTER_AREA?), which is supposed to be better for shrinking images.

When I was studying in university, I needed to find a way to resize images without distorting them. I stumbled upon a great (and should have been obvious) method.

Running the new resizing code gives:

New small fox.

We load the image, and in line 5 select the width we would like for our resized image. In line 6, we are scaling the height of the resized image based on the proportion of new width divided by the original height, and then set the dimensions in line 7.

Cropping

Cropping is a fairly simple process using OpenCV. Since the images are just stored as arrays, and arrays have indices, we can just specify the x and y index values for the edges of the cropped portion of our image.

To crop the image, we load the image in line 1, then get the dimensions in line 2. In line 3, we select the indices in the image, image[starting y:ending y, starting x: ending x]. Then, we display the image.

Peaceful.

Saving an Image

Use cv2.imwrite(‘output file name’, image to save) to save an image.

The file will be saved in the working directory, but the directory can also be specified if needed.

Summary

After reading this blog, you should be able to load and save images, perform color transformations, scaling, and cropping on images. In the next post, we will find out how to add these pictures to our picture frames on the wall.

References

OpenCV docs – Very helpful if you are considering using OpenCV in your own projects.

If you are interested in image processing, a quick Google will turn up loads of tutorials and information.

Leave a Reply

Your email address will not be published. Required fields are marked *