Reader small image

You're reading from  Raspberry Pi Computer Vision Programming. - Second Edition

Product typeBook
Published inJun 2020
Reading LevelBeginner
PublisherPackt
ISBN-139781800207219
Edition2nd Edition
Languages
Tools
Right arrow
Author (1)
Ashwin Pajankar
Ashwin Pajankar
author image
Ashwin Pajankar

Ashwin Pajankar is an author, a YouTuber, and an instructor. He graduated from the International Institute of Information Technology, Hyderabad, with an MTech in Computer Science and Engineering. He has been writing programs for over two and a half decades. He is proficient in Linux, Unix shell scripting, C, C++, Java, JavaScript, Python, PowerShell, Golang, HTML, and assembly language. He has worked on single-board computers such as Raspberry Pi and Banana Pro. He is also proficient with microcontroller boards such as Arduino and the BBC Micro:bit. He is currently self-employed and teaches on Udemy and YouTube. He also organizes programming boot camps for working professionals and software companies.
Read more about Ashwin Pajankar

Right arrow

Chapter 12: Working with Mahotas and Jupyter

In the previous chapter, we learned about and demonstrated the use of real-life applications in the area of computer vision using Raspberry Pi with OpenCV and Python 3 programming.

In this chapter, we are going to learn the basics of another computer vision library—Mahotas. We are also going to have a look at a Jupyter project and understand how we can use the Jupyter Notebook for Python 3 programming. The topics we will learn in this chapter are as follows:

  • Processing images with Mahotas
  • Combining Mahotas and OpenCV
  • Other popular image processing libraries
  • Exploring the Jupyter Notebook for Python 3 programming

After following this chapter, you will be comfortable with using Mahotas for image processing. You will also be able to confidently run Python 3 programs with the Jupyter Notebook.

Technical requirements

The code files of this chapter can be found on GitHub at https://github.com/PacktPublishing/raspberry-pi-computer-vision-programming/tree/master/Chapter12/programs.

Check out the following video to see the Code in Action at https://bit.ly/3fWRYDB.

Processing images with Mahotas

Mahotas is a Python library for image processing and computer vision-related tasks. It was developed by Luis Pedro.

It implements many computer vision-related algorithms. It has been implemented in C++ and it operates on NumPy arrays. It also has a clean interface for Python 3.

Mahotas currently has over 100 functions for image processing and computer vision, and that number keeps growing with every release. This project is under active development and there is a new release every few months. Apart from the added functionality, every new release brings improvements in performance.

Note:

You can learn more about Mahotas by referring to https://mahotas.readthedocs.io/en/latest/.

We can install mahotas on Raspberry Pi with the following command:

pip3 install mahotas

A component of Mahotas will be installed in /home/pi/.local/bin. We can add this to the PATH variable permanently, as follows:

  1. Open the ~/.profile file in editing...

Combining Mahotas and OpenCV

Just like OpenCV, Mahotas uses NumPy arrays to store and process images. We can also combine OpenCV and Mahotas. Let's see an example of this, as follows:

import cv2
import numpy as np
import mahotas as mh
cap = cv2.VideoCapture(0)
while True:
    ret, frame = cap.read()
    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    T_otsu = mh.otsu(frame)
    output = frame > T_otsu
    output = output.astype(np.uint8) * 255
    cv2.imshow('Output', output)
    if cv2.waitKey(1) == 27:
        break
cv2.destroyAllWindows()
cap.release()

In the preceding program, we converted a live frame into a grayscale version. Then, we applied a Mahotas implementation of Otsu's binarization, which converted the frame from the live video feed into a Boolean binary image. We need to convert this to the np.uint8 type and multiply it by 255 (all of which takes the form of ones in binary 8-bit) so that we can use it with cv2.imshow(). The output is...

Exploring the Jupyter Notebook for Python 3 programming

The Jupyter Notebook is a web-based interactive interface that works like the interactive mode of Python 3. The Jupyter Notebook has 40 programming languages, including Python 3, R, Scala, and Julia. It provides an interactive environment for programming that can have visualizations, rich text, code, and other components, too.

Jupyter is a fork of the IPython project. All the language-agnostic parts of IPython were moved to Jupyter and the Python-related functionality of Jupyter is provided by an IPython kernel. Let's see how we can install Jupyter on Raspberry Pi:

  1. Run the following commands one by one in Command Prompt:
    sudo pip3 uninstall ipykernel 

    The previous command uninstalls the earlier version of the ipykernel utility.

  2. The following commands install all the required libraries:
    sudo pip3 install ipykernel==4.8.0 
    sudo pip3 install jupyter 
    sudo pip3 install prompt-toolkit==2.0.5

    These commands will install...

Summary

In this chapter, we explored the basics of Mahotas, which is a NumPy-based image processing library. We looked at a few image processing-related functions and learned how to combine Mahotas and OpenCV code for image processing. We also learned the names of other NumPy- and non-NumPy-based image processing libraries. You can explore those libraries further.

The last topic we learned about, the Jupyter Notebook, is very handy for quickly prototyping ideas and sharing code electronically. Many computer vision and data science professionals now use Jupyter notebooks for their Python programming projects.

In the Appendix section of this book, I have explained all the topics that I could not list under this chapter. These topics will be immensely useful to anyone who uses Raspberry Pi for various purposes.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Raspberry Pi Computer Vision Programming. - Second Edition
Published in: Jun 2020Publisher: PacktISBN-13: 9781800207219
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Ashwin Pajankar

Ashwin Pajankar is an author, a YouTuber, and an instructor. He graduated from the International Institute of Information Technology, Hyderabad, with an MTech in Computer Science and Engineering. He has been writing programs for over two and a half decades. He is proficient in Linux, Unix shell scripting, C, C++, Java, JavaScript, Python, PowerShell, Golang, HTML, and assembly language. He has worked on single-board computers such as Raspberry Pi and Banana Pro. He is also proficient with microcontroller boards such as Arduino and the BBC Micro:bit. He is currently self-employed and teaches on Udemy and YouTube. He also organizes programming boot camps for working professionals and software companies.
Read more about Ashwin Pajankar