# A beginner’s guide to image processing using NumPy

Since images can also be considered as made up of arrays, we can use NumPy for performing different image processing tasks as well from scratch. In this article, we will learn about the image processing tasks that can be performed only using NumPy.

Mostly we find the uses of NumPy in the problems where we are required to perform mathematical and logical operations on different sizes of arrays. Since images can also be considered as made up of arrays, we can use NumPy for performing different image processing tasks as well from scratch. In this article, we will learn about the image processing tasks that can be performed only using NumPy. This will be helpful for beginners to understand image processing from its very basics. The major operations to be performed using NumPy are listed below, which we will cover in this article.

2. Cropping image
3. Colours separation
4. Transformation
5. Grayscale conversion
6. Image segmentation

``````import numpy as np
import matplotlib.pylab as plt
%matplotlib inline
image = plt.imread("/content/drive/MyDrive/Yugesh/image processing using numpy/images.jfif")
print(image)``````

Output:

Here we can see the raw form of the image. Since mathematically the images are made up of pixel values in the above output we can see that we have some numbers which define the colours in the image and the image is basically an array or NumPy array. We can also plot the image using the matplotlib library.

`imgplot = plt.imshow(image)`

Output:

## Cropping image

After loading the image we are ready to perform actions on the image. As in very basic we can perform basic crop operations on our image. For NumPy, crop operation can be performed by slicing the array.

``````crop_img = image[20:199,:200,:]
imgplot = plt.imshow(crop_img)
``````

Output:

Here we can see that we have cropped our image. Now we can move forward to our next image processing step.

## Colour separation

Since we know that every image is made of pixel values and these pixel values represent three integers that are known as the RGB value of its colour. To separate the image in these colours we are required to pull out the correct slice of the image array.

``````fig, axs = plt.subplots(nrows=1, ncols=3, figsize=(20,8))

for c, ax in zip(range(3), axs):
rgb_img = np.zeros(image.shape, dtype="uint8")
rgb_img[:,:,c] = image[:,:,c]
ax.imshow(rgb_img)
ax.set_axis_off()
``````

Output:

Here in the output, we can see that we have separated the RGB of the image for this we mapped the values in the range 0 to 1 and cast to type uint8. Let’s move toward our next step of image processing.

## Transformations

In this step, we will perform the colour transformation. For this purpose, we can treat the image pixel as a point in space. Treating this way to image pixels allows us to perform a transformation to the colour point. Rotating the colour point can be an example of the above statement. Here we are applying Numpy’s Einstein notation function which is a method of applying a rotation matrix, pixel-wise, to the image.

``````def do_normalise(image):
return -np.log(1/((1 + image)/257) - 1)
def undo_normalise(image):
return (1 + 1/(np.exp(-image) + 1) * 257).astype("uint8")
def rotation_matrix(theta):
return np.c_[
[1,0,0],
[0,np.cos(theta),-np.sin(theta)],
[0,np.sin(theta),np.cos(theta)]
]
img_norm = do_normalise(image)
img_rot = np.einsum("ijk,lk->ijl", img_norm, rotation_matrix(np.pi))
img = undo_normalise(img_rot)

imgplot = plt.imshow(img)
``````

Output:

Here in the output, we can see that applied sigmoid to colour space worked and we are applying the rotation of the colour of the pixels continuously. Now in the next step, we will look at how we can convert an image to a grayscale image.

## Grayscale conversion

We can also use NumPy for transforming the image into a grayscale image. By Taking the weighted mean of the RGB value of the image we can perform this.

``````rgb_weights = [0.2989, 0.5870, 0.1140]
grayscale_image = np.dot(image[...,:3], rgb_weights)
imgplot = plt.imshow(grayscale_image)
``````

Output:

Here is the image of the output of our grayscale conversion process. Let’s move on to our next step of image processing.

## Image segmentation

This is one of the most used image processing steps where we segment different regions of images. There are various ways to do so like foreground and background. For example, in this article, we are going to see how we can perform segmentation by converting the image into grayscale and finding a threshold. Pixels in the image that are above the threshold are in one region and others are in another region.

``````def simple_threshold(image, threshold=128):
return ((image > threshold) * 255).astype("uint8")

thresholds = [100,120,128,138,150]

fig, axs = plt.subplots(nrows=1, ncols=len(thresholds), figsize=(20,5));
gray_im = to_grayscale(image)

for t, ax in zip(thresholds, axs):
ax.imshow(simple_threshold(gray_im, t), cmap='Greys');
ax.set_title("Threshold: {}".format(t), fontsize=20);
ax.set_axis_off();
``````

Output:

Here in the above output, we can see that we have segmented the image into two regions using different threshold values.

Final words

In this article, we have discussed different tasks of image processing that we have performed using the NumPy library. Also, we have used the matplotlib library for the visualization of images after processing. By Looking at the above points, we can say that we can perform other tasks as well by just using some other logic.

References

Yugesh is a graduate in automobile engineering and worked as a data analyst intern. He completed several Data Science projects. He has a strong interest in Deep Learning and writing blogs on data science and machine learning.

## Oct 11-13, 2023 | Bangalore

### Telegram group

Discover special offers, top stories, upcoming events, and more.

### Discord Server

Stay Connected with a larger ecosystem of data science and ML Professionals

#### Subscribe to our Daily newsletter

##### MOST POPULAR

This blended learning programme from IIMA aims to equip candidates with essential skills to build data confidence and make informed decisions aligned with business objectives.

### Google and Apple Compete for AI-Powered Accessibility

On the global accessibility awareness day, both Google and Apple released AI-powered accessibility features

During the recent Google I/O 2023 conference, Google introduced watermarking and metadata to all images generated by AI to promote transparency

### Data is Gold, Twitter the Goldmine to Train AI Models

Every tweet posted on the platform, becomes the property of the social media giant and can be used by others who have access to its API

### Meta Finally Cracks the LLM Code Without RLHF

So far, Meta has been reluctant with LLMs and chatbots, but is now rushing into the open-source without reinforcement learning with human feedback (RLHF)

### Republic, News 18 and Others Break Fake AI News

A fake AI photograph of an explosion near the Pentagon surfaced on the internet and media houses circulated the hoax image on their channels

### Intel Aurora: A Last Ditch Effort for Supercomputer Dominance

Intel’s long-delayed supercomputer, Aurora, might be what it needs to come back to power in the HPC market.

### AI Takes the Centre Stage at Microsoft Build 2023

One of the most notable announcements made by Microsoft at Build was the integration of Bing with ChatGPT

### Uncensored Models are Double-edged Swords That Need to be Unleashed

Embracing uncensored models is crucial for scientific exploration, freedom of expression, diversity, storytelling, and composable nature of the open-source AI community

### The Context Length Hitch with GPT Models

OpenAI rival, Anthropic AI has opened up the context window massively with its own chatbot Claude, pushing it to sound 75,000 words or 100,000 tokens.