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
Python Robotics Projects by Prof. Diwakar Vaish.epub
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.
Linux Device Driver Development Cookbook by Rodolfo Giometti(3913)
Embedded Programming with Modern C++ Cookbook by Igor Viarheichyk(3435)
Implementing Cellular IoT Solutions for Digital Transformation by Dennis McCain(3389)
Embedded Linux Development Using Yocto Project - Third Edition by Otavio Salvador & Daiane Angolini(3226)
TinyML Cookbook by Gian Marco Iodice(3170)
Simplifying 3D Printing with OpenSCAD by Colin Dow(2833)
TinyML Cookbook by Gian Marco Iodice & Ronan Naughton(2593)
Fusion 360 for Makers by Lydia Sloan Cline(2212)
Networking A Beginner's Guide by Bruce Hallberg(2200)
Hands-On Linux for Architects by Denis Salamanca(2036)
But How Do It Know? by J. Clark Scott(2021)
Computers For Seniors For Dummies by Nancy C. Muir(1991)
Raspberry Pi and MQTT Essentials by Dhairya Parikh(1942)
Arduino Project Handbook, Volume 2: 25 Simple Electronics Projects for Beginners by Geddes Mark(1939)
9781803246888-ENHANCING DEEP LEARNING WITH BAYESIAN INFERENCE by Unknown(1891)
Hack and HHVM by Owen Yamauchi(1876)
31 Days Before Your CompTIA A+ Exams (Shanette Luellen's Library) by Benjamin Patrick Conry(1852)
MicroPython Projects by Jacob Beningo(1717)
Hands-On Internet of Things with MQTT by Tim Pulver(1693)
