Digital Imaging Applications | Image Processing in Python

Share on facebook
Share on twitter
Share on linkedin
Share on telegram
Share on whatsapp

Contents

This article was published as part of the Data Science Blogathon

Introduction

Digital image processing consists of the various techniques and methods involved in manipulating images on a computer.. Various types of operations are performed on images, constituting digital image processing.

Understand what an image actually is?

The image is basically a two-dimensional signal. The signal function is f (x, Y), where the value of x and y at a point generates the pixel at the point. The image is basically a two-dimensional array consisting of numbers between 0 Y 255.

Various factors are involved in image processing. Image processing has a few main reasons.

Image Processing Help at:

1. Improvement of the digital information stored by us.

2. Automating work with images.

3. Better image optimization leading to efficient storage and transmission.

Over the years, image processing has improved a lot and there are many modern commercial image processing applications.

Image Processing Uses:

1. Image correction, sharpness and resolution correction

Often, we wish we could improve old images. And that is possible today. Zoom, the sharpness, edge detection and high dynamic range edits are included in this category. All these steps help to improve the image. Most editing programs and image correction code can do these things easily..

2. Filters in the edition of applications and social networks

Most editing apps and social media apps offer filters these days.

53395ip1-6522631

Above is an example of the original image and the leaked image. Filters make the image look more visually appealing. Filters are usually a set of functions that change the colors and other aspects of an image that make the image look different.. Filters are an interesting application of image processing.

3. Medical technology:

In the field of medicine, image processing is used for various tasks, as positron emission tomography, X-ray imaging, medical CT scan, UV images, cancer cell imaging and much more. The introduction of image processing in the field of medical technology has vastly improved the diagnostic process.

60463ip2-2232351

(Image source: https://axisimagingnews.com/radiology-products/imaging-equipment/x-ray/image-processing-software-mimics-grid-use-improve-image-quality)

The image on the left is the original image. The image on the right is the processed image. We can see that the processed image is much better and can be used for a better diagnosis.

4. Computer vision / machine:

One of the most interesting and useful applications of image processing is computer vision. Computer vision is used to make the computer see, identify things and process the whole environment as a whole. An important use of computer vision is driving cars, drones, etc. CV helps detect obstacles, recognize paths and understand the environment.

27096ip3-3538997

(Source: streets of Paris in the eyes of Tesla's autopilot https://youtu.be/_1MHGUC_BzQ)

This is how typical computer vision works for car autopilots. The computer captures live images and analyzes other cars, the road and other obstacles.

5. Pattern recognition:

Pattern recognition is part of image processing that involves artificial intelligence and machine learning. Image processing is used to discover various patterns and appearances in images. Pattern recognition is used for handwriting analysis, image recognition, computer-assisted medical diagnosis and much more.

6. Video Processing:

Video is basically fast moving images. Various image processing techniques are used in video processing. Some video processing methods are noise removal, image stabilization, frame rate conversion, detail enhancement and much more.

Getting started with image processing in Python:

Let's start with some basic image-related tasks in Python. We will use PIL.

PIL

Python Image Library is used for various image processing tasks.

Installation:

pip install pillow

With PIL installed, now we can move on to the code.

First, we work with some matplotlib functions.

import matplotlib.image as img
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline

The following image will be read. It's called image1.jpg.

42886image1-7291166
# reading jpg image 
img = img.imread('image1.jpg')
plt.imshow(img)
98055ip4-5854312

The image is read.

# modifying the shape of the image
lum1 = img[:, :, 0] 
plt.imshow(lum1)

Now the shape of the image is modified.

51525ip5-5387815

Now we will change it to the color map “hot”. To read more about the colormap, visits is Link.

plt.imshow(lum1, cmap ='hot') 
plt.colorbar()

The image output looks:

48136ip6-6598254

Now we try a different colormap.

imgplot = plt.imshow(lum1)
imgplot.set_cmap('nipy_spectral')

Image output:

87772ip7-9911686

The reason for using color maps is that, often in various applications and uses, it is useful to have a uniform color map. Read more about Colourmaps: Choose color maps in Matplotlib.

Now let's take a look at why we call an image a 2D matrix.

#data type of lum1

print(type(lum1))

Departure:

print(lum1)

[[ 92 91 89 … 169 168 169]
[110 110 110 … 168 166 167]
[100 103 108 … 164 163 164]

[ 97 96 95 … 144 147 147]
[ 99 99 98 … 145 139 138]
[102 102 103 … 149 137 137]]

The points are there to show that there are many more data points in between. But one thing is for sure, is that they are all numerical data.

Let's find the size of the array.

len(lum1)

Departure: 320

Departure: 658

This gives us the number of pixels and the dimensions of the image: 320 * 658.

We will also check it later.

Now we work with PIL.

from PIL import Image

We will use this image file, called: people.jpg.

72542people-3071034
img2 = Image.open('people.jpg')
plt.imshow(img2)
49030ip8-8376315

The image is read.

Now, we change the size of the image.

img2.thumbnail((50, 50), Image.ANTIALIAS)  # resizes image in-place
imgplot = plt.imshow(img2)
60029ip9-1215722
imgplot1 = plt.imshow(img2, interpolation="nearest")
94054ip10-6779429
imgplot2 = plt.imshow(img2, interpolation="bicubic")
59943ip11-3882488

But, Why do we purposely blur images in image processing? Good, often for computer vision and pattern recognition algorithms, it becomes difficult to process images if they are very sharp. Therefore, blur is done to soften images. Blur also makes the color transition in an image, from side to side, be much smoother.

Now, Let's check the dimensions of the car picture, what we worked on before.

#some more interesting stuff
file="image1.jpg"
with Image.open(file) as image: 
    width, height = image.size 
#Image width, height is be obtained
57607ip12-8743302

These are the dimensions we got as well. Then we can conclude that the image is 320 * 658.

Let's also try rotating and transposing the image.

#Relative Path 
img3 = Image.open("image1.jpg")  
#Angle given 
img_rot= img3.rotate(180)  
#Saved in the same relative location 
img_rot.save("rotated_picture.jpg")
13558ip13-9088558

This is the rotated image.

#transposing image  
transposed_img = img3.transpose(Image.FLIP_LEFT_RIGHT)
#Saved in the same relative location 
transposed_img.save("transposed_img.jpg")
37431ip14-7324078

This is the transposed image.

Last words:

Image processing has several important applications and, over time, methods and processes will also improve.

About me:

Prateek Majumder

Data science and analytics | Digital Marketing Specialist | SEO | Content creation

Connect with me on Linkedin.

Thanks.

Media shown in this digital imaging article is not the property of DataPeaker and is used at the author's discretion.

Subscribe to our Newsletter

We will not send you SPAM mail. We hate it as much as you.