9 Powerful Tips and Tricks for Working with Image Data using skimage in Python

The colored images have more information as compared to the grayscale images but the size of the images is very large.

The number of pixels in RGB is 3 times more.

This can present quite a challenge when we’re restricted by low compute power.

Hence, grayscale images are often used to reduce the computational complexity.

So if the size of your dataset is very large, you can choose to go for grayscale over colored.

  2.

Changing Image Format In the last section we discussed about two important formats in which we can load the images – RGB and grayscale.

In this section we will learn how to convert an image from one format to another.

To start with, we will read an image in RGB format and convert it into the grayscale format.

The function we will use here is rgb2gray– View the code on Gist.

Other two popular formats are HSV (hue, saturation, value) and HSL (hue, saturation, lightness) which are alternative representations of the RGB format.

Let me briefly explain what each of these terms mean.

Hue is a degree on the color wheel where 0 is for red, 120 is green, 240 is blue and again 360 would be red.

Saturation represents the percentage of that color, where 0 is white and 100  is the full color.

Value denotes the mixture of the colors with varying amounts of black or white paint.

Lightness is another way to show the shade of the image where 0 is black and 1 is white.

The image shown below will make your understanding more clear- Source: Wikipedia   Changing the image to any of these formats will be the same as we did for converting to grayscale.

We can use the functions rgb2hsl and rgb2hsv to convert into HSL and HSV format respectively.

Here I have demonstrated the conversion of image to HSV format.

View the code on Gist.

  3.

Resizing Images using skimage One of the biggest challenges in computer vision is that we require a huge amount of data for training our model.

The data we collect is often from different sources which might result in variation in the size of the images.

This might be a problem while extracting features from the images, or using the same for data augmentation.

Ideally, the size of the images should be the same when we’re building our model.

If we’re using a pre-trained model, it is important to resize and normalize the input data to the same format over which the network was originally trained.

This is why resizing images is an important image preprocessing step.

Here, we are going to use the resize function from skimage.

The input to this function will be the image we want to update and the required dimensions for the new image: View the code on Gist.

  4.

Rescaling (Upscale/Downscale) Images using skimage Rescaling images is another common computer vision technique.

This implies scaling the images by a particular factor.

For example – reducing the size of each image by half (downscale) or increasing the size of images by a factor of 2 (upscale).

You might argue that we can simply use the resize function for this task, what is the difference?.If the original size of all the images is the same, say (300, 300), we can directly use the resize function and specify the required dimensions (150, 150).

But if the size of the images is different (like the images shown below), the resize function cannot be used.

This is because the ‘half’ of each image would be different.

And this is a use case you will encounter a lot in your computer vision journey.

So here, we can use the rescale function and specify the scaling factor.

All the images will be scaled by this factor, based on the original size of the image.

Here is an example: View the code on Gist.

  5.

Rotate an Image by Different Angles using skimage So far we have looked at resizing and rescaling the images.

Let’s turn our focus and see how we can change the orientation of images.

But before we dive into that, we should discuss why we need to change the image orientation in the first place.

Consider the below images.

The first image is slightly tilted (which may be due to the camera orientation).

To fix this orientation problem, we will need to rotate the image by a certain angle.

We can use the rotate function of skimage and specify the angle by which we need the image to be rotated: View the code on Gist.

This looks great!.The orientation problem is all fixed.

But if you look closely, the picture is cropped around the corners.

This is because, during the rotation, the size of the image remains the same causing the area around the corner to get cropped.

We are not losing any important information in this scenario but that might not always be the case.

This obstacle is taken care of by the resize parameter in the rotate function (by default the parameter value is False): View the code on Gist.

We can also use the rotation concept for data augmentation.

For those who are not familiar with the term, Data Augmentation is a technique of generating more samples for training the model, using the available data.

Say you are building an image classification model to identify images of cats and dogs.

Take a look at the sample images shown below.

Both the images on the left would be classified as ‘dog’ and the images on the right would be classified as ‘cat’:       What did we change here?.We simply rotated the images by 180 degrees and generated the new images.

Think about it – you can double the size of the training data by simply adding one new image against every image in the existing data!.  6.

Flip Images Horizontally and Vertically We can flip an image both horizontally and vertically.

This creates a mirror image along the horizontal/vertical axis.

We can use this technique for both image preprocessing and image augmentation.

Although there is no direct function for this in skimage, we can use NumPy to perform this task.

NumPy provides functions flipud and fliplr for flipping the images across the horizontal and vertical axis respectively.

The internal working of the function is very simple.

For a horizontal flip, the rows remain intact while the entries in the columns are reserved.

Let us take the same cat/dog example and use the flip function on it: View the code on Gist.

Looking good!.  7.

Crop Images You must have used the cropping function on your phone a gazillion times.

I do it way too often!.You can crop images inside your Python notebook as well using skimage.

We crop images to remove the unwanted portion of the image or to focus on a particular part of the image.

Let’s say we have the below image from a basketball match (left image).

In its current form, the shape of the image is 1067 x 1600.

Now, I want to remove 100 pixels from all 4 sides of the image.

This would mean that we remove 100 pixels from the top, bottom, left and right of the image, thus focusing on the object at the center: There are two ways to achieve this: First, simply specify the new image shape you want.

For this example, it would be image [100:967, 100:1500].

This will work fine for a single image.

What if we have multiple images?.We would have to mention the new image shape for every image (not ideal).

Another way could be to calculate the cropped image dimension using the current image shape, which can be determined using the command image.

shape.

So the height of the new image would be [100:img.

shape[0]-100] and the width will be [100:img.

shape[1]-100].

So, let’s crop the above image using the second method: View the code on Gist.

  8.

Altering Image Brightness using skimage How often do you fiddle around with the brightness of an image you took in a bad light?.Despite the recent advances in how cameras function, low light imaging is a headache.

skimage will fix that for us.

Images with different brightness can be used to make our computer vision model robust to changes in lighting conditions.

This is important for systems that work in outdoor lighting, for instance, CCTV cameras on traffic signals.

The brightness of images can be changed using the adjust_gamma function in skimage, which uses a method called gamma correlation.

For any given image, the pixel values are first normalized between 0 – 1 and then multiplied by a specified gamma value.

The resulting pixel values are scaled back to the range 0-255.

For gamma greater than 1, the output image will be darker than the input image.

While for gamma less than 1, the output image will be brighter than the input image.

View the code on Gist.

  9.

Using Filters in skimage We can use Filters to modify or enhance an image’s features.

You’ll be pretty familiar with filters if you’ve ever played around with images on social media platforms.

We can use filters for various purposes, such as smoothing and sharpening the image, removing noise, highlighting features and edges in the image, etc.

When we apply a filter on an image, every pixel value is replaced by a new value generated using surrounding pixel values.

The simplest filter is the median filter, where the pixel values are replaced with the median of neighboring pixels.

View the code on Gist.

Another popular filter is the sobel filter.

We use this when we want to highlight the edges in an image.

If you want to learn in detail about how the filter works, you can refer to this article.

3 Beginner-Friendly Techniques to Extract Features from Image Data using Python View the code on Gist.

  End Notes Congratulations on taking your first step in computer vision!.It can appear to be a daunting field initially, but if you have a structured thinking mindset and a good grasp on how machine learning algorithms work, you’ll quickly pick up the nuances of working with image and video data.

There are other things we can do using skimage, such as extracting the edges from an image, or adding noise to an image, among other things.

I want you to take these two up for starters, and try them out in Python.

That’s how you learn new concepts!.And as I mentioned at the start of the article, you should check out our comprehensive course on computer vision: Computer Vision using Deep Learning I look forward to hearing your thoughts and feedback on this article.

Connect with me in the comments section below!.You can also read this article on Analytics Vidhyas Android APP Share this:Click to share on LinkedIn (Opens in new window)Click to share on Facebook (Opens in new window)Click to share on Twitter (Opens in new window)Click to share on Pocket (Opens in new window)Click to share on Reddit (Opens in new window) Related Articles (adsbygoogle = window.

adsbygoogle || []).

push({});.

. More details

Leave a Reply