Understanding the PIXELS using OPENCV | Machine Learning | Beginner | Python | Image Processing

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back so today we are going to see a concept in opencv where we are going to learn how can we access the individual pixels of an array because obviously when an image is red it is read in the form of array so we are going to see how can we read the pixels of an array which in turn we mean is that we are going to read the pixels of the an image so we are going to see how can we read the pixels select those pixels and then we are going to see how can we manipulate those pixels and when we read the pixels what value do we get in return from that function whatever we do so that is what the goal of this video so let's get to the coding session and see how can we do all these things before going into the code let's first understand how the image is represented and when we want to read any pixel value what value do we get in return so say i have an image so this is image and what it what it contains is an image of cat see there is a cat now when i read this in open cv right when i read this in opencv it is an rgb image what does it mean it means it has a color intensity for red a color intensity for green and a color intensity for blue colors so now when this image is red inside this open cv or any image processing library what we get in return we have this and then we have this and then we have this so in actual what we have is the image of the cat in red color the image of the cat in blue color and the image in of the cat in the green color obviously i wrote the this sequence wrong so it is r g and b so the concept is that every square represents the corresponding color right every color represents the corresponding color now when an image is red and it is represented using these three squares with three color channels now when we want to read the first pixel so see this is the first pixel here let me change the color so say this pixel intensity not the first pixel the pixel at this location we want to read it and same the pixel of this location and the pixel at that location since these images this these images the image with the red channel the image with green channel and the image with blue channel right they are of the same size right so this pixel value this pixel value this pixel value is at the same location so when i read this pixel value right when i read this pixel value i will get a tuple i will get a tuple with value belonging to r with value belonging to green with value belonging to blue so suppose i get an value of 2 4 4 for the red and then 0 for the green and for blue i get 255 right so this is the value for blue this is the value for green this is the value for the red pixel so whenever i read a pixel in an rgb image the pixel values that i get returned as the intensities of three colors with r g and b so every time i read a pixel what i internally mean is that i'm reading three colors of rgb intensities so this is how an image is read inside the opencv and that is will be the reason when we will see that we will get a tuple of three pixel values of three pixel values with r g and b for every pixel we read and for every range of pixels we will be reading so let's now see this all this theory in action and using the code first thing that we are going to do is import opencv that is using the cv2 binding then we are going to write a small function that uh we will use matplot library for displaying the images in our uh in our notebook so we'll write definition will show and we will pass it the image that we want to display and then all we need to do is say plot dot i'm show and give the image right so the image that we are going to give is the obviously the image with color channel right with color channel say for now let's just pass in the image and then we want to plot to show that's all we will do in this function now we'll obviously import matplot library dot pi plot as plt because we are going to use that now this is the matte plot library uh module and is not found so what we will do is we will first install this using pip or conda whatever feels good so we'll pip install matplot library it will take some time depending on the connection that you probably have if you have a fast connection it will be if you have a small connection it will be and now in my case it is now it is installing the connected package that's blue and all that thing now let's go back inside this file after it completely installs it now rerun this cell and now the matte plot library will be and will be imported now let us read the image that we want to work with so this is the uh sorry so this is the image that we will be seeing in our case so you can see it's pretty huge and big image now let's read this image so we'll say in image equal to cb2 dot i am read function cv2 dot i am read and give this the path of the image that is input dot jpg right closing the quotation marks now let's see how can we show this image obviously we will call the show image function show image function and we'll pass the image that we just read so that is in image and now i will run it and we will see definitely there is an image of this now you can see that the color is somewhat different here because the image in this case in the case of cd2 it is read in the bgr format so blue g green and r the opposite of rgb format and plot definition of the plot library is showing this image in rgb format that is why the colors get inverted so you can see if i open this image again so every red color will be converted to blue color because it's rgb converted to pgr now we can obviously use some terminology to convert the color but let's not do that here let's first modify this function a little bit so that we don't get these numbers around these axis that x is and we will off axis and we will also increase the plot size let's run this again let's run this again we'll definitely see an image of a bigger size now what we want is we want to get the pixel at this location at zeroth location we want to get the pixel at zero location since in this case when we are seeing this image it's a two it this image has two axis that is the x-axis that's the x-axis and the y-axis that is the width and the height right so we will definitely corresp whenever we are going to see or extract the pixel we do have to give the location along the x axis and location along the y axis now let's see the value of that image so we'll say any pixel equal to in image right so in image and since we are this is 2d so we'll use two brackets in image at zeroth location and zeroth location and then we will print this any image any pixel sorry and obviously you will see there's a tuple it's 168 103 and 29 so this is the color intensity for this b blue this is the color intensity for g this is the color intensity for red in this image in this image right in this image now what does it mean it means that i'm accessing the pixel value at zeroth index that's zero along the row and zero along the columns now instead of using this double square brackets we can use the single square brackets that is in image and give the single square bracket and use this format and when we print this you can see we get an array with the same values that is 168 103 and 29 so this is how we get the pixel values now let's say i want to get the pixel value at say 200 comma 200 right so definitely this is the pixel value at 200 comma 200 so we'll go 200 pixels here 200 pixels here at the top of the image we go 200 pixels here 200 pixels here see that image is here that pixel is here so we got the pixel value at this point right this is how we access the individual pixels but it's not fun it's no fun to use these individual pixels we do not require them what we require is a range of the pixels right so how can we obviously get the range of the pixels let's try that let's say in image right and say obviously we have two axis so we'll say comma because we are accessing the two axis here so the first axis i want to go for 0 we'll use slicing operation we go for 0 to say 1 5 0 and along the other axis we go 0 to 1 5 and 0. now let's see what we get obviously we get an array of these pixel values now instead of printing these pixel values let's store this in some variable let's say small image small and when we run this now this small contains the all the range values that is zero so what what this means is that i'm going from the top of this cell let this be the top of this let this be the top of the image so i'm going from top 0 to 150 pixel and then from top 0 to 150 pixel i'm going from top 10 to 150 pixels so obviously i will get some portion of the image now let's say that i have small this image contained this small variable contains some range array that is containing some pixel values now let us see what does this small consist let's try to show let's try to convert this uh image that's these pixel values into an image all we need to do is say show image and pass in this small and when we run this we'll get some portion of this image right this is the portion of the image right so now let's say let's try to extract the balloon of this image right let's try to extract the balloon of this image in the same way now the first thing that we need to do is first let's see what's the shape of this image what's the size of this image so we'll see in image dot shape and we'll get the shape that is thousand by thousand so thousand by thousand is the shape of this image so it is 1000 pixels this and thousand pixels in height so what i can guess here is that we must go to 600 pixels probably and then go almost 200 pixels right so let's do that in so balloon is equal to an image right we'll go use since we have region two so uh what we need to do is extract this balloon so i think we should go first we should go up to this point it seems 600 to i think 600 to 600 we need to go to 600 we need to go to 600 to this point that is say 700 right 600 say 600 right to say 900 so i will go from 600 pixel to 900 pixel and then i will go to this pixel from this pixel i think this is 300 to 500 right then i will show this image and what i will pass the is the balloon oh precise but not exactly so i got some balloon inside it so obviously i'm a little bit off here right so let's go from 500 to 900 and then go from rather 300 we should go for 500 to say 800 and then rerun this and now we got a balloon but obviously we got a bit to the right so you got the concept of this extracting the range of the pixels from an image this is the same concept that we will be using when we have to crop the image so you can see i how i got this balloon inside this frame so this is the concept of accessing the pixel values individual pixel values and accessing the range of the pixel values in this so to keep in mind the only thing to keep in mind is that whenever we are accessing a pixel value in a colored image we are getting three values for each pixel one value for red the other for green and the other for blue so this is the same concept this ranging concept seek this slicing concept is the same concept that is used inside the cropping mechanisms so i hope you understood this concept i hope you saw how easy it is to work with pixels and extract the pixel ranges from an image and how can we do some stuff weird stuff in this operation in these operations if you are creative enough so thank you for watching this video i hope you liked this video understood the concepts and if you do understood the concepts in this video please give this video a big thumbs up and do subscribe to our channel thank you for watching god bless you take care bye bye allah hafiz
Info
Channel: M Zean
Views: 16,097
Rating: undefined out of 5
Keywords: machinelearning, deeplearning, python, sklearn, pandas, ai, Machine learning, Deep learning, Machine learning for beginners, deep learning made easy, nlp, cnn, dnn, rnn, image classification, tensorflow, tf datsets, pytorch, torchaudio, machine learning python, machine learning projects, nlp python, tensorflow image classification, python programming, python course, python for beginners, pandas for kids, python coding
Id: Pcb7g3_WeXQ
Channel Id: undefined
Length: 16min 14sec (974 seconds)
Published: Wed May 11 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.