Unit 4 (10 marks
Unit 4 (10 marks
Unit 4 (10 marks
PannedWindow
It is a container widget which is used to handle number of panes arranged in it.
The general syntax is:
w = PannedWindow(master, option=value)
master is the parameter used to represent the parent window. There are number
of options which are used to change the format of the widget. Number of options
can be passed as parameters separated by commas. Some of them are listed
below.
Output:
Different actions are performed on the images for different applications which
include cropping, flipping, rotation, segmentation, etc. Python provides functions
for all these methods, using which we can set parameters that suit our needs.
There are different modules in Python which contain image processing tools.
The first 5 modules are widely used by the programmers and we will be
discussing numpy, scipy, OpenCV, and PIL further in the article.
Installation
As these libraries are not available directly when Python is installed, we need to
install them separately before use. Since we will be using the matplotlib library
to view the images, let us install it too.
The following commands can help in the installation of the required libraries.
pip install numpy
pip install scipy
pip install opencv-python
pip install pillow
pip install matplotlib
Before doing any operation on an image, we first need to load the image.
Output:
We can also read and save the images using misc in scipy. But these functions
are depreciated in the versions of scipy above 1.2.0. The syntax of these
functions are:
pic=misc.imread(location_of_image)
misc.imsave(‘picture_name_to_be_stored’,pic) #here pic is the name of the
variable holding the image
We can import more than one image from a file using the glob module. The
below code shows an example of reading multiple images in the location
“D:/New folder/”.
Let us first check the type of matrix, the image gets stored in.
Output:
numpy.ndarray
We can see that it is a numpy array. So, we can do all the operations we do on
the numpy matrices. The below code gives an example of getting a pixel value
at a position.
Output:
84
Output:
Getting Statistical information
As we have seen above that the image is stored in the form of a numpy array,
we can apply statistical functions like max, min on the image too. Let us see the
example,
Output:
Mean:113.48026784261067
Max:250
Min: 0
We can rotate the image using the rotate() function in the scipy module. The
below example shows the way of doing it.
Output:
We can see in the image that its size changed to fit the rectangular block around.
We can set another parameter ‘reshape’ in the rotate function to not change its
size. For example,
We can also flip the image using the flipud() function in numpy. For example,
Output:
Let us see what happens when we apply a Gaussian filter to the image.
Output:
We can see that the image got blurred. Now let us see how to sharpen the
image.
Output:
Now let us add some noise to the image and filter using both gaussian and
median filters.
Output
Output:
Output:
Interpolation in Python
Interpolation is the process of finding the unknown value that occurs between
the known values. In the case of an image, we apply interpolation when we
zoom the image. As we need to give more pixel values than the original image
and for this purpose, we need to find the intermediate pixel values. The
interpolation provides many options like bilinear, nearest, etc. Let us see what
happens when we use these options.
Output:
OpenCV
Output:
And to save the image we use the imwrite() function using the below syntax.
cv2.imwrite(filename, image)
We can also find information about the image like size, dimensions, etc. For
example,
Output:
No.ofPixels:151875
Dimension: (225, 225, 3)
We got three values in the dimension as there are three channels in the color
image.
Splitting the Channels
Output:
And we can get the blue, green, red channels using the split() function. Let us
split them and see the results.
Binarization
We can convert the image into a binary image using the threshold() function.
For example,
Output:
Edge Detection
Edges are the borders of different parts of the image that lets us differentiate
these parts. When we talk in terms of a digital image, edges are the pixels where
there is a sudden change in the pixel values. OpenCV contains a function called
canny() to detect the edges. The below codes give an example.
Output:
PIL
Opening and Saving the Image
We can read the image using the open() function. For example,
We can also get information about the image like size, format, etc.
Output:
<PIL.PngImagePlugin.PngImageFile image mode=RGB size=804×692 at
0x1F929BAEE80>
size:(804,692)
format: PNG
Convert to grayscale
We can use the convert() method to convert it to a grayscale image. For
example,
Output:
Rotating
We can use the rotate() function to rotate the image by a specified angle in an
anticlockwise direction.
Forexample,
Output:
Resize
Unlike the above operation, we do not use any resize() function here! Instead,
we use the function named thumbnail() and give the input of the required size.
Output: