Python image processing_Python image processing.pptx

shashikant484397 457 views 28 slides Feb 22, 2024
Slide 1
Slide 1 of 28
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28

About This Presentation

Python image processing


Slide Content

ELC Activity Image Processing Introduction to Python for Image Processing

Python What is Python? Python is a high-level, general-purpose programming language.  Python in Image Processing: Python is one of the widely used programming languages for processing the Digital Images . Its amazing libraries and tools help in achieving the task of image processing very efficiently. 

Working With Python To write a Python code, many IDEs are available. Working with Anaconda : Anaconda is a distribution of the Python and R programming languages for scientific computing, that aims to simplify package management and deployment.  Working with Google Colab : Colab , or " Colaboratory ", allows you to write and execute Python in your browser, with Zero configuration required Access to GPUs free of charge Easy sharing

Getting Started with Python Starting with Google Colab : Sign-in into your Gmail account and open the new notebook in Google Colab . You will be redirected to the new notebook.

Getting Started with Python In the new notebook: Space to upload the data or images for working within the session Space to write your code Output

Getting Started with Python To Run the code in notebook:

Importing Libraries OpenCV : It is a huge open-source library that includes several hundreds of computer vision, machine learning, and image processing algorithms . It provides a comprehensive set of functions and tools that facilitate the development of applications dealing with images and videos . import cv2 Pillow : The Python Imaging Library adds image processing capabilities to your Python interpreter. This library provides extensive file format support, an efficient internal representation, and fairly powerful image processing capabilities . import PIL f rom PIL import Image

Importing Libraries NumPy : NumPy stands for Numerical Python. This library adds the support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays . import numpy as np Matplotlib : Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays. Matplotlib consists of several plots like line, bar, scatter, histogram, etc . from matplotlib import pyplot as plt

Opening and Reading Image PIL.Image.open ()  Opens and identifies the given image file . The   plt.imshow () function  in pyplot module of matplotlib library is used to display data as an image; i.e. on a 2D regular raster . img = Image. open ( "9.jpeg" ) plt.imshow ( img )

Opening and Reading Image Printing Image details print ( img.format ) print ( img.size ) print ( img.mode ) JPEG (1238 , 1280) RGB

Image Pre-Processing

Converting into Gray scale First, convert the image into a numpy array: img = np.array ( img ) Then convert the image into gray scale and show it: gray = cv2.cvtColor( img , cv2.COLOR_BGR2GRAY) plt.imshow ( gray,cmap = ' gray ' )

Applying Thresholding Apply a thresholding value according to the image and then convert it into an inverse binary image; where 1 means contour lines and 0 means background. _, threshold = cv2.threshold(gray, 110 , 255 , cv2.THRESH_BINARY_INV)

Shape Detection

Applying Morphological Operators First defining the kernel shape and size to further use in morphological operators. These kernels basically works as filter to either remove or add the pixels in the contours as per the morphological operator applied. # kernel for dilation operation kernel = cv2.getStructuringElement(cv2.MORPH_RECT,( 4 , 4 )) # kernel for closing operation kernelc = cv2.getStructuringElement(cv2.MORPH_RECT,( 6 , 6 )) # kernel for opening operation kernelo = cv2.getStructuringElement(cv2.MORPH_RECT,( 3 , 3 ))

Applying Morphological Operators Apply different morphological operators, to extract the shape boundary and discard the remaining background. # for getting the complete shape dilated = cv2.dilate(threshold, kernel, iterations= 3 ) # for connecting the disjoint edges of shapes closing = cv2.morphologyEx(dilated, cv2.MORPH_CLOSE, kernelc , iterations = 3 ) # for removing the blobs created because of the above two operations opening = cv2.morphologyEx(dilated, cv2.MORPH_OPEN, kernelo , iterations = 8 )

Applying Morphological Operators Apply different morphological operators, to extract the shape boundary and discard the remaining background. # for skeletonization thinned = cv2.ximgproc.thinning(opening) dilated_t = cv2.dilate(thinned, kernel, iterations= 6 )

Applying Morphological Operators Finally, the complete contours formed and detected. Hence, the first step of detecting the shape is completed. Original Image Detected shapes

Shape Identification

Identifying and Drawing Contours Approximately identifying only the closed contour boundaries. cv2.approxPloyDP() function approximates the entire shape. approx = cv2.approxPolyDP(contour , 0.02 * cv2.arcLength(contour, True ), True ) Draw the identified contours by using drawContours () function cv2.drawContours( img , [contour], , ( , , 255 ), 2 )) Note: All the above function will be run within the counted contours of the shape calculated by findContours () function. contours ,_ = cv2.findContours( dilated_t , cv2.RETR_TREE , cv2.CHAIN_APPROX_SIMPLE)

Identifying and Drawing Contours The identified contours will look like the following: Original Image Identified shapes

Shape Recognition

Recognizing and Putting name of shapes First find the center of the shape and then put name on it. To recognize the name of the shape, the length of the closed contours is calculated, which is earlier calculated by cv2.approxPloyDP() function. To detect the center of the point:   # finding center point of shape     M = cv2.moments(contour )     if M[ 'm00' ] != 0.0 :         x = int (M[ 'm10' ]/M[ 'm00 ' ])         y = int (M[ 'm01' ]/M[ 'm00' ])

Recognizing and Putting name of shapes Recognize the shape by the number of contours in a closed shape and put the name in the center of the each shape:     if len ( approx ) == 3 :         cv2.putText( img , 'Triangle' , (x, y),                     cv2.FONT_HERSHEY_SIMPLEX, 2 , ( 255 , , ), 5 )     elif len ( approx ) == 4 :         cv2.putText( img , 'Rectangle' , (x, y),                     cv2.FONT_HERSHEY_SIMPLEX, 2 , ( , 255 , ), 5 )     elif len ( approx ) == 5 :         cv2.putText( img , 'Pentagon' , (x, y),                     cv2.FONT_HERSHEY_SIMPLEX, 2 , ( , 255 , 255 ), 5 )     elif len ( approx ) == 6 :         cv2.putText( img , 'Hexagon' , (x, y),                     cv2.FONT_HERSHEY_SIMPLEX, 0.8 , ( 255 , , 255 ), 2 )     else :         cv2.putText( img , 'circle' , (x, y),                     cv2.FONT_HERSHEY_SIMPLEX, 2 , ( , , 255 ), 5 )

Recognizing and Putting name of shapes The final output will be like:     Original Image Final recognized shapes

Challenges Challenges: Identify the optimal Binary threshold value Irregular Shape Detection Shape detection with hazy and noisy background Smoothness and Continuity Improvement    

Resources The following resources can be referred: Gonzalez, Rafael C.  Digital image processing . Pearson education I ndia, 3 rd edition, 2009 . Jähne , Bernd.  Digital image processing . Springer Science & Business Media, 2005 . https://docs.opencv.org/4.x/d6/d00/tutorial_py_root.html https:// docs.opencv.org/3.4/d2/d96/tutorial_py_table_of_contents_imgproc.html https://www.bogotobogo.com/cplusplus/files/OReilly%20Learning%20OpenCV.pdf    

Thank You
Tags