Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
0 views

Chapter 03 - Image Processing and Acquisition using Python_Part9

The document provides code snippets for displaying and processing images using the cv2 and matplotlib.pyplot libraries in Python. It demonstrates how to read an image, convert it to grayscale, process it, and then display or save the processed image. The workflow for image processing is outlined, emphasizing the sequence of reading, processing, and visualizing or writing the image.

Uploaded by

Choky Aconk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Chapter 03 - Image Processing and Acquisition using Python_Part9

The document provides code snippets for displaying and processing images using the cv2 and matplotlib.pyplot libraries in Python. It demonstrates how to read an image, convert it to grayscale, process it, and then display or save the processed image. The workflow for image processing is outlined, emphasizing the sequence of reading, processing, and visualizing or writing the image.

Uploaded by

Choky Aconk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Image and Its Properties 53

3.5.5 Displaying Images

Throughout this book, to display images, we will use Mat-


plotlib.pyplot. Below is a sample code snippet that reads and displays
an image.

import cv2
import matplotlib.pyplot as plt

# cv2.imread will read the image and convert it into an


ndarray.
img = cv2.imread('image1.png')

# We import matplotlib.pyplot to display an image in


grayscale.
# If gray is not supplied the image will be displayed
in color.
plt.imshow(img, 'gray')
plt.show()

We are importing cv2 and matplotlib.pyplot modules. Notice that


we are aliasing matplotlib.pyplot as plt. We use cv2.imread to read
an image and we use plt.imshow to display the image. As we want
a grayscale image to be displayed, we provide a string ’gray’ to the
plt.imshow function.
Note: We can also display a DICOM image using plt.imshow
because pyDICOM’s read file also returns a data object that contains
the image data as an ndarray.

3.6 Programming Paradigm


As described in the introductory section, the workflow (Figure 3.1)
for image processing begins with reading an image and finally ends with
54 Image Processing and Acquisition using Python

either writing the image to file or visualizing it. The image processing
operations are performed between the reading and writing or visualizing
the image. In this section, the code snippet that will be used for reading
and writing or visualizing the image is presented. This code snippet will
be used in most of the programs presented in this book.
Below is a sample code where cv2 and matplotlib are used.

# cv2 module's imread to read an image as an ndarray.


# cv2 module's imwrite to write an image.
import cv2
import matplotlib.pyplot as plt

img = cv2.imread('image1.png')

# Converting img to grayscale (if needed).


img_grayscale = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# We process img_grayscale and obtain img_processed.


# The function image_processing can perform any image
# processing or computer vision operation
img_processed = image_processing(img_grayscale)

# cv2.imwrite will take an ndarray and store it.


cv2.write('file_name.png', img_processed)

# We import matplotlib.pyplot to display an image in


grayscale.
plt.imshow(img_processed, 'gray')
plt.show()

In the above code, the cv2 module is imported. Then matplotlib


.pyplot is imported as plt. We use cv2.imread to read image1.png
and return an ndarray. We use cv2.cvtColor along with the argument
cv2.COLOR BGR2GRAY to convert img, which is a three-channel
ndarray to a single-channel ndarray and we store it in img grayscale.

You might also like