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

Creating a negative of an image

In terms of pure mathematics, when we invert the colors of an image, it creates a negative of the image. This inversion operation can be computed by subtracting the color of a pixel from 255. If it is a color image, we invert the color of all the planes. For a grayscale image, we can directly compute the inversion by subtracting it from 255, as follows:

import cv2
img = cv2.imread('/home/pi/book/dataset/4.2.07.tiff', 0)
negative = abs(255 - img)
cv2.imshow('Grayscale', img)
cv2.imshow('Negative', negative)
cv2.waitKey(0)
cv2.destroyAllWindows()

The following is the output of this:

Figure 5.6 – A negative of an image

Figure 5.6 – A negative of an image

Try to find the negative of a color image, we just need to read the image in color mode in the preceding program.

Note:

The negative of a negative will be the original grayscale image. Try this on your own by computing the negative of the negative again for our...

lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
Raspberry Pi Computer Vision Programming. - Second Edition
Published in: Jun 2020Publisher: PacktISBN-13: 9781800207219

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