OpenCV Python Tutorial For Beginners 26 - Understanding image Histograms using OpenCV Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome to the next video on open CV tutorial for beginners using Python in this video we will discuss about histograms in open CV so what is a histogram so you can consider histogram as a graph or a plot which gives you an overall idea about the intensity distribution of an image so let me give you some examples and then I will be able to explain you better how histogram works and why they are useful so to start with I have this example which is a very normal example here I'm creating 200 by 200 pixel image using numpy zeroes which essentially mean that we are going to get a 200 by 200 pixel image of black pixels so let me just just start this example and you can see this is the final result so all the pixels here in this image are black and the size is 200 by 200 now let's say we want to calculate or find out the histogram of this image so there are several ways of finding out histogram of an image so let's see them one by one so first of all we are going to find out the histogram using the mat plot libb because the plot using matplotlib you can draw easily so let's use that first of all so for that what I'm going to do is I'm going to use PLT because I have already imported this matplotlib library as PLT so PLT r dot hist there is a function called PLT dot hist which calculates the histogram of an image and because it's just a grayscale image or it's just a black image so it's easier to find out the histogram so you what you can do here is the first argument here will be your image or your source so I'm going to just say image dot Ravel's okay so there is a method called Ravel's the second argument here will be a maximum number of pixel values so I'm going to just say 256 the third argument here will be the range so the range will vary from 0 to 256 okay so this is all you need to find out the histogram using the mat plot lib and you just need to show this plot in a mat plot lab window so you can just say PLT dot show so that's it so let's run this code and let's see what happens so you see this plot using matplotlib and also our original image so as we have created the image of 200 by 200 pixel of black pixels so all the intensity of this graph you can see is zero so you can see here 200 multiplied by 200 is equal to 40000 so these are the number of pixels so on the y-axis you will see the total number of pixels and here the intensity so intensity starts from 0 to 256 so this graph is showing how many number of pixels inside an image which have this pixel values so in our example all the pixels inside this image have the pixel value 0 that's why this graph is like this so all the 40,000 pixels inside the image have the pixel value 0 so you will get this type of histograms so once again the histogram is a graph or a plot which gives you the overall idea about the intensity distribution of an image now histogram is just another way of understanding the image by looking at the histogram of an image you can get the intuition about the contrast brightness intensity distribution at stretcher now let's improve this example which we have so I'm going to just close this window and let's say I want to add some white pixel also inside this image so what I'm going to say is I'm going to just see b2 dot rectangle so I'm going to just add the rectangle inside this image and the source here will be the IMG variable then where I want to introduce this a rectangle so I want to introduce this rectangle at this point which will be let's say which starts from 0 comma 100 and the second point here will be let's say 200 comma 200 ok so this will be 200 and the next value here will be the color so let's say we want to add the white pixels so this will be 255 which will be net the maximum value and then the next argument will be the thickness so I'm going to just say minus 1 which will fill this rectangle inside this image so when I run now this code you will see this graph and this image so you can see half of this image contains black pixels and half of this image contains the white pixels and we already know that the size of this image is 200 by 200 that's why here in the graph you will see 20,000 pixels are black which means that 20,000 pixels have the pixel value 0 and 20,000 pixels have the pixel value 255 that's why you see this here so you can see you can easily find out the pixel intensity of an image easily using histograms now next we are going to add some more pixels into this image and this time what we are going to do is we are going to add the rectangle or inside the same image so let's say it goes from 0 comma 50 to 100 comma 100 and the color here we are going to provide the pixel value of 127 let's say okay so which is the half of 0 and 255 approximately so I'm going to run this example once again and now you will see this kind of image so you can see half of the pixels here are white that means 20 thousand pixels have the pixel value of 255 so you can see here now around 15 thousand pixels here in the half of this image have the pixel value of 0 that's why you can see this line here and we have added the rectangle of pixel value 127 also so around you can see around 5000 pixels here have the pixel value of 127 so this is how the histogram is going to work so let's use now the original image so some kind of image instead of this black or white image so now what I'm going to do is I'm going to just once again declare a variable and then I'm going to just say cb2 dot I am Reed and we are going to read some file so let's say I have this lamb Lenna dot jpg image so I'm going to just read that I hope text is correct JP G and we are going to read this image in the grayscale mode so I'm going to just say zero here and now I'm going to run this example once again and you can see this Lenna image is loaded in the grayscale mode and here is the histogram of this image so these are all the pixel intensities inside this image so you can see from this graph that most number of pixels contained inside this image have the pixel value around 150 now you can also find out the pixel intensity of different colors so till now we have been just using the grayscale mode or black or white pixels but you can also use the same histogram for the BGR values also so let's see how we can undo that so what we are going to do is let me just remove this code all I'm going to just leave it commented and here I'm going to just say B comma G comma R and there is a method we have already seen which is called C V dot split which is going to split your image into PG R values so we are going to just give the source which is our image and then if you want to show these BG r values you can just show in the I'm sure window so BG R and here also be G and R and when you want to show the histogram of BG r values then also you can use matplotlib dot hist method you just need to change this source from image to be gr so be G and are okay so now what we are going to do is we are going to run our code and let's see what happens so it's giving me this error because I'm reading this image in the grayscale mode so I'm going to remove this extra parameter from I am read because we want to read this image in the color form and then only we will be able to get the BG our channels right in the grayscale mode there are no BG our channels so I'm going to run this script once again and let's see what happens so you can see this histogram of blue channels and green channels and the red channels and these are the images which are loaded in these different channels so this is the image which is loaded in the blue channel and this is the green and this is the red Channel and you can see the histogram of each channel differently using matplotlib so let me just close all these windows now there is a method in CV 2 also which is called calc hist which is going to give you the histogram of an image so for that what you can do is I'm going to just just comment all the this code because I just want to show how you can use the CB to calc hist method ok so what you can do is you can use methods so let's say a hist and then see we dot calc hist and this method takes a few arguments so the first argument here will be the image so it's the source which you give but the only special thing is you just give this image in the square brackets okay the second argument here is the channel so it is the index of channels for which we calculate the histogram so here in our case because we are going to read the image in grayscale mode we can just give the channel zero here so for one channel you can give zero here for different channel you can give zero one to value the next argument here is the image mask so to find a histogram of full image it is given as none because or because our image is loaded in the grayscale mode so we can give here none the next value is the hist sighs so this his size is the representation of bin count and this is also given in the square bracket so we are going to just say 256 here the next argument is the range so range will vary from 0 to 256 so minimum and the maximum range of the x axis you can say so 256 and then we can just show this hist or histogram inside the PLT so the PLT dot plot method so dot plot and then we can just give this histogram value here okay so let's run this code and let's see what happens so you can see you get the histogram of this image using the opencv calc hist method and what are the uses of the histogram so our histogram can tell you whether or not your image has been properly exposed so when you take a digital image it's very useful it can also tell you whether the lighting conditions were flat or harsh when you took image and using the histogram you can also make the adjustments which will work best for your digital images so this the usefulness of the histograms we will see in the later videos this was just the basics about the histograms in OpenCV so I hope you enjoyed this video and I will see you in the next video you
Info
Channel: ProgrammingKnowledge
Views: 62,910
Rating: undefined out of 5
Keywords: OpenCV Tutorial, Python (Programming Language), Python 3.6, Python, Python Tutorial, Python course, Online Course, OpenCV, OpenCV Tutorial for Beginners, Computer Vision, Computer Vision Basics, Computer Vision Tutorial, Windows, Linux, Image Processing, OpenCV Python Tutorial, OpenCV Python, blurring images, Gaussian Pyramid, findContours, drawContours, Draw Contours, threshold, Histograms, image Histograms, Histograms using OpenCV
Id: F9TZb0XBow0
Channel Id: undefined
Length: 16min 11sec (971 seconds)
Published: Thu Jul 11 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.