Python Robotics Projects by Prof. Diwakar Vaish

Python Robotics Projects by Prof. Diwakar Vaish

Author:Prof. Diwakar Vaish
Language: eng
Format: mobi, epub
Tags: COM074000 - COMPUTERS / Hardware / Mobile Devices, COM051360 - COMPUTERS / Programming Languages / Python, COM067000 - COMPUTERS / Hardware / General
Publisher: Packt
Published: 2018-05-25T13:11:47+00:00


Now the first image shows you the hue: 100, saturation: 100, and value: 100. Hence, black is zero, the color is green, and saturation is 100%. In the subsequent picture, you can see the color has faded when the saturation is kept on a lower percentage. Finally, when value is reduced in the next image, then the color gets really dark.

So now coming back to the point, why hue saturation value? So now to detect any color, we simply need one unit instead of three different unit forming that color hence making the job simpler. There are various other reasons to do so as well. But at this time, it is not a concern for us.

Now moving forward, we have passed on two arguments—image, which is where the converting algorithm will take the raw data from, second is cv.Colour_BGR2HSV, which basically tells us the algorithm to use during the conversion. So as we have already discussed, we have to convert the RGB values to hue saturation values (HSV). Finally, these values will be returned to a variable named hsv:

lowerGreen = np.array([40,50,50])

upperGreen = np.array([80,255,255])

In this line, we are giving the upper and lower range values, which needs to be detected. As you can see, we are detecting a green color; hence, we would be providing the upper and lower values for both the ends. If you want to change the color that you want to detect, then you simply need to change this value and the job will be done:

mask = cv2.inRange(hsv, lowerGreen, upperGreen)

Now, we are segregating the objects that are falling in this color range and giving the value to an array. This is done by a section function named inRange(). So, there are three arguments that we need to pass. First, which image does it need to work on, what is the lower range value that it needs to detect, and the upper range value that we have provided as hsv, lowerGreen, upperGreen. The result of this would be an array that would have the value of the image that has all color to be blacked out and only the color that lies in the specific color range to be shown in a plain white color:

res = cv2.bitwise_and(image, image, mask=mask)

bitwise_and is a function of cv2 library; what it does is simply logical and of the two values of the array. The arguments that we are passing are image and the image with mask, or in other words, we are passing two images—one being the raw and the other being the mask. With this function, we are ending those two images. The result of this would an image that has a black background all around and the object that lies in the specific color range will be shown in a proper color image:

cv2.imshow('mask',mask)

We have previously used a function named cv2.inRange, and what it did was to filter out the specific color ranges that we had defined. Now, that function gave us a new array by the name of mask. What



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Popular ebooks
Linux Device Driver Development Cookbook by Rodolfo Giometti(3334)
Building Low Latency Applications with C++ by Sourav Ghosh(2609)
Fusion 360 for Makers by Lydia Sloan Cline(1977)
Networking A Beginner's Guide by Bruce Hallberg(1919)
But How Do It Know? by J. Clark Scott(1804)
Computers For Seniors For Dummies by Nancy C. Muir(1764)
Arduino Project Handbook, Volume 2: 25 Simple Electronics Projects for Beginners by Geddes Mark(1748)
Hands-On Linux for Architects by Denis Salamanca(1706)
Hack and HHVM by Owen Yamauchi(1666)
31 Days Before Your CompTIA A+ Exams (Shanette Luellen's Library) by Benjamin Patrick Conry(1657)
PrestaShop Recipes by Arnaldo Pérez Castaño(1460)
MicroPython Projects by Jacob Beningo(1432)
Hands-On Internet of Things with MQTT by Tim Pulver(1384)
Getting Started with Soldering: A Hands-On Guide to Making Electrical and Mechanical Connections by Vinck Marc de(1376)
Raspberry Pi Electronics Projects for the Evil Genius (Tab) by Norris Donald & Norris Donald(1376)
Embedded Systems Architecture by Daniele Lacamera(1369)
Healthcare Information Security and Privacy (All-In-One) by Sean Murphy(1364)
Shaping the Fourth Industrial Revolution by Klaus Schwab & Nicholas Davis & Satya Nadella(1353)
The Internet of Things (The MIT Press Essential Knowledge series) by Greengard Samuel(1342)
Raspberry Pi Beginners Guide: Ultimate Guide For Rasberry Pi, User guide To Get The Most Out Of Your Investment, Hacking, Programming, Python, Best Hardware, Beginners Guide To Rasberry Pi by Steven Giles(1329)