Dip Lab-2

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

CEN 440 Digital Image Processing Lab

Basic Python function for Image related operation- Lab 2

Name: _________________________________

Enrollment #: _________________________________

Class: _________________________________

Objective

The aim of this introductory lab is to introduce you to the basic functions in the Python. By the
end of today’s lab, you should be able to read images from the disk display them, write them
back to the disk and perform conversions between different image classes.

Submission Requirements

You are expected to complete the assigned tasks within the lab session and show them to the
lab engineer/instructor. Some of these tasks are for practice purposes only while others
(marked as ‘Exercise’ or ‘Question’) have to be answered in the form of a lab report that you
need to prepare. Following guidelines will be helpful to you in carrying out the tasks and
preparing the lab report.

Reading an Image
1. Reading, and displaying an image using PIL
The PIL function, open(), reads an image from disk in an Image object, as shown in the
following code. The image is loaded as an object of the PIL.PngImagePlugin.PngImageFile
class, and we can use properties such as the width, height, and mode to find the size
(width x height in pixels or the resolution of the image) and mode of the image:
The following is the output of the previous code:

2. Reading, and displaying an image using Matplotlib


The next code block shows how to use the imread() function from matplotlib.image to read
an image in a floating-point numpy ndarray. The pixel values are represented as real values
between 0 and 1:

The following figure shows the output of the previous code:

3. Reading, saving, and displaying an image using scikit-image


The next code block uses the imread() function from scikit-image to read an image in a numpy
ndarray of type uint8 (8-bit unsigned integer). Hence, the pixel values will be in between 0
and 255. Then it converts (changes the image type or mode, which will be discussed
shortly) the colored RGB image into an HSV image using the hsv2rgb() function from the
Image.color module. Next, it changes the saturation (colorfulness) to a constant value for
all of the pixels by keeping the hue and value channels unchanged. The image is then
converted back into RGB mode with the rgb2hsv() function to create a new image, which
is then saved and displayed:

The next figure shows the output of the previous code—a new image with changed
saturation:
The following table shows the commonly used file formats supported by Python:

Image Format Description Recognized Extensions


TIFF Tagged Image File Format tif, tiff
JPEG Joint Photographic Experts Group jpeg, jpg
BMP Windows Bitmap Bmp
GIF Graphics Interchange Format Gif
PNG Portable Network Graphics Png

Image Size
Function size can be used to determine the dimensions of an image using PIL.

Exercise 1

Load the image ‘camera.png’, find its dimensions and number of channels and
display it
Writing Images
Images are written to disk using the function which has the following basic syntax:

Dealing with different image types and file formats

An image can be saved in different file formats and in different modes (types). Let us discuss
how to handle images of different file formats and types with Python libraries.

File formats

Image files can be of different formats. Some of the popular ones include BMP (8-bit, 24-bit, 32-
bit), PNG, JPG (JPEG), GIF, PPM, PNM, and TIFF. We do not need to be worried about the
specific format of an image file (and how the metadata is stored) to extract data from it. Python
image processing libraries will read the image and extract the data, along with some other
useful information for us (for example, image size, type/mode, and data type).

Converting from one file format to another


Using PIL, we can read an image in one file format and save it to another; for example,
from PNG to JPG, as shown in the following:

im = Image.open("../images/parrot.png")
print(im.mode)
# RGB
im.save("../images/parrot.jpg")

But if the PNG file is in the RGBA mode, we need to convert it into the RGB mode before we save
it as JPG, as otherwise it will give an error. The next code block shows how to first convert and
then save:
im = Image.open("../images/hill.png")
print(im.mode)
# RGBA
im.convert('RGB').save("../images/hill.jpg") # first convert to RGB mode

Converting from one image mode into another


We can convert an RGB image into a grayscale image while reading the image itself. The
following code does exactly that:

im = imread("images/parrot.png", as_gray=True)
print(im.shape)
#(362L, 486L)

Note that we can lose some information while converting into grayscale for some colored
images. The following code shows such an example with Ishihara plates, used to detect color
blindness. This time, the rgb2gray() function is used from the color module, and both the color
and the grayscale images are shown side by side. As can be seen in the following figure, the
number 8 is almost invisible in the grayscale version:

Image types (modes)


An image can be of the following different types:

• Single channel images—each pixel is represented by a single value:


o Binary (monochrome) images (each pixel is represented by a single
0-1 bit)
o Gray-level images (each pixel can be represented with 8-bits and
can have values typically in the range of 0-255)

• Multi-channel images—each pixel is represented by a tuple of values:


o 3-channel images; for example, the following:
▪ RGB images—each pixel is represented by threetuple (r, g, b) values, representing
red, green, and blue channel color values for every pixel.

▪ HSV images—each pixel is represented by threetuple (h, s, v) values,


representing hue (color), saturation (colorfulness—how much the color is
mixed with white), and value (brightness—how much the color is mixed with
black) channel color values for every pixel. The HSV model describes colors
in a similar manner to how the human eye tends to perceive colors.

▪ Four-channel images; for example, RGBA images—each pixel is represented


by three-tuple (r, g, b, α) values, the last channel representing the
transparency.
Dealing with different image types and file formats and performing
basic image manipulations
Converting from one image mode into another
We can convert an RGB image into a grayscale image while reading the image itself. The
following code does exactly that:

Note that we can lose some information while converting into grayscale for some colored
images. The following code shows such an example with Ishihara plates, used to detect color-
blindness. This time, the rgb2gray() function is used from the color module, and both the color
and the grayscale images are shown side by side. As can be seen in the following figure, the
number 8 is almost invisible in the grayscale version:

The next figure shows the output of the previous code—the colored image and the grayscale
image obtained from it:

Exercise 2

Write a Python program which reads the image ‘chelsea.png’ available in the
skimage distribution. Find the size of the image and the number of channels in it. If
it is a three channel image first convert it to grayscale and display it.

Exercise 3

Load the image ‘parrot.png’, binarize it with a threshold of


127. Display both original and binarized images using matplotlib and save
binarized image to disk. (Open cv Functions)
Powered by TCPDF (www.tcpdf.org)

You might also like