Image Processing with OpenCV and Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
working with image data in python can be extremely powerful from doing simple tasks like converting image formats cropping and filtering images all the way to using image data for machine learning in this video i'm going to give you a basic introduction of how to work with image data with two of my favorite python packages matplotlib and opencv by the end of this video you should have a good understanding of how image data is structured and some of the things that are possible with image data using python hi my name is rob i'm a data scientist i make videos about python machine learning and i also stream on twitch live coding if you enjoy this video please consider liking it and subscribing to my channel all the code i'm working on today i will share in the description as a kaggle notebook so you can copy that code and interact with it very easily okay let's get started working with image data in python okay so here i am in a kaggle notebook the nice thing about this is i'll link it in the description you should be able to copy it if you have a kaggle account and edit the data yourself and explore what we've coded up today so i'll show you here on the right side that i've imported a data set this is a cat and dog image data set so we can have some example images to work with today it should be pretty fun so we're going to start here with some basic input ports uh i import pandas as pd typically import numpy as np um we're also gonna use a package called glob that'll let us list all the files of our dogs and cat images very quickly as a list and then we're going to import the two image based packages that we'll be using today so cv2 and then we're going to import matplotlib pilab as plt and matplotlib can be used for a lot of things but it does have functionality for interacting with image data we're going to start by reading in images the very first thing we should learn right so before we do that we're gonna have to get a list of all of our images and this is where we're gonna use glob so let's uh go into the directory where i know these dogging cat images are and let's search for any jpeg images in the dog directory that um ends with jpg so this is gonna be our dog files and similarly let's just make a cats one since we know there's a cat's directory and put this here now we can see with our cat files let's uh read in an example image file so we're gonna take an example one here from our list this is the cat image file 1856 and we're gonna read it in first with matplotlib using am plt.imree this will read in this file name and you could put whatever file name you have of an image here and it should read it in it can do jpegs png files a bunch of different formats and we're going to save this as image mpl for matplotlib let's also learn how to import the image using cv2 so cv2 has the exact same function i am read and we'll read in the same cat files 20 and call this image cv2 now i also want to show you what the output of this is so that the actual data that we've read in here is actually a numpy array if we do a type on this we can see that this is a numpy array and the shape of this numpy array is um three dimensions and we'll go into what each of these mean but we're gonna print these first so let's print the matplotlib shape and the cv2 shape just to confirm that they are the same so 232 by 350 and three so let's learn a little bit about what this array represents because that's very important to understanding how to work with image data to explain this i found an illustration online that i'll link in the description of uh really showing what these values represent so the image array you can think of in the format of height width and then channels for this height and width and here we have three channels our height for this image is 232 and our width is 350. and each of these values in the array are different and they refer to the intensity of that given pixel so think of the pit image as broken down into a big grid of pixels and each of those pixels has a value you can see here that the maximum value when you read in an image is usually to around 255 so a lot of times what you'll see is actually dividing the pixel values by 255 to make it a normal number between 0 and 1 but working with the pixel values as 0 to 255 is fairly common just to visualize this even more let's actually take all of these values in this numpy array and let's flatten them into one long array with values between 0 and 255 and let's make this a panda series and we can actually plot a histogram and let's title this distribution of pixel values and let's do plot show so here we can see the distribution of the pixel values for this entire image across the three channels that we have and we could see that there are some at zero and a lot of them peak around here around 75 but this just sort of shows you that each pixel has a unique value that contributes to the image that you see so this isn't too exciting and i'm sure you want to see some images so let's display the actual image that we're we've loaded in here so let's call this display images so we're going to use matplotlib to show this image and first we'll create a subplot with a fig size so our image is set in a certain figure size and then we'll use matplotlib's im show to display this matplotlib image and if i display that here you can see that there are some grid lines and we also have the x and y values which we don't want to show so we're going to take this axis and turn those off and there we go pretty cute picture of a kitten here so we can now display an image that we've loaded in using matplotlib so next i want to talk about what these channels actually represent so we're going to talk about something called rgb or red green blue representation and that's the way that these images are loaded in when you load them in with cv2 or matplotlib there are three channels and they represent red green and blue any combination of those three channels can actually represent all the colors that your eye can perceive so three channels you can think of as these stacked numpy arrays of all red channels all green channels and all the blue channel values that's why if you look at our image shape we have this height width and then the three channels there are other ways of representing the image data but rgb is fairly common so we're going to stick with that for now so we're going to do this by making another subplot but this time we're going to do a 1 by 3 grid of plots and in our first plot we're going to show the image of this matte plot lib for only the first channel and we're going to use a color map of reds to visualize this in the color that it represents and let's just copy this three times for the other accesses we're gonna do we're gonna show the other colors so this will be greens and blues using the c map just is easier for representing what we're trying to show and then we're gonna also turn off all the grids like we did before for these three different um images and finally we're gonna show them so what do we have here okay so we have the three different channel representations of this image from above let's set titles for them green and blue there we go so each of these three look fairly similar but if you notice like up here in the top right of the image that it's darker for the red and green channel and pretty light for the blue channel that's because that part of the image has very little blue in it rep to represent this color that you see here in the top right of the image now another thing i want to touch on real quickly is the difference between the matplotlib read function and the cv2 read function because they actually read these channels in different orders and if you don't know that at first it gets pretty confusing why the images look different when you load them with cv2 so all you need to know is that cv2 reads the image in channels blue green red and matplotlib reads them in as rgb and we're going to display this as well in similar manner that we did above so let's make a sub plots a 1 by 2 fig size of 10 by 5. and in the first plot we're gonna show the cv2 version of the image and in the second version we're going to show the matplotlib version and doing all this axis turning off we'll set the titles so you might not notice that at first but it becomes very apparent when you have images that have certain colors that stand out but here let's look in the top right corner we can see that the cv2 or opencv version that we've read in has a different color and shade as the one on the right that matpot lib has and the different display packages might expect it in different orders converting the order of these channels is pretty easy using cv2's convert color and what you do is you provide it the image so we're going to provide it the cv2 image and then we use cv2's color bgr to rgb which will convert this to uh rgb format so image cv2 rgb is what we'll call this and then let's go ahead and display this like we have before using our subplots and image show this there we go so we've converted the cv2 image into the same format that the matplotlib image expects now we're just going to quickly go over some of the basic things you can do with cv2 with these image files we're going to call this section image manipulation and there are a lot of things you can do with the image file once you have it loaded in these are just a few to get you started let's go and use one of the dog image files to switch it up here a little bit and we're going to use matplotlibs image read use our dog pull from our dog files i found a good one here and then we'll um we'll go ahead and display this dog file like we have before oh look at that cute pup let's show how we can convert the color of this image similar to how we converted the channel order of the colors using cv2's convert color we're going to provide it this image and then we're going to actually use uh we're gonna use cv2's rgb to gray and that'll convert this red green blue image into a grayscale image and if we look here at the image gray shape of the numpy array it's actually now only two dimensions because there's only one channel and that's the grayscale channel so this is just the height and width of this image let's go ahead and make a subplot put this image in there let's use the grays c map when we plot this turn the axis off and show it there we go we have this image has been converted into grayscale and that's just one of the manipulations you can do next we're going to talk about resizing and scaling of an image so this can be done using cv2 using the cv2 resize function so if we provided this image then we could also provide it a new size that we want to provide but we're not going to use that instead we're going to use a function that will reduce the size by a certain percentage using fx and fy so we're going to convert this image into a quarter of the size that it was before and then we are going to use our same plot code to plot this you can see that it's a little bit more pixelated that's because it's been shrunk down small enough that when we plot it at the same scale it looks much more zoomed in but this is now a resized image and if i do the shape on this you can see the size of it is now 125 by 125 instead of the original size we don't necessarily have to make it this uh same dimensions as what it was before so let's actually use that same cv2 resize to resize this image into stretched out different dimensions so instead is giving the fx and fy for the percentage that we want to change it we're actually giving the new height and width dimensions that we want this image to be reshaped into and let's go let's go ahead and plot this and we have you can see that stretched a little bit um the height is now a lot longer than the width sometimes especially in machine learning you want to upscale or resize the image to a specific size that the model expects as an input so this is one of the ways you can do it but you can also upscale using the image resize now one important thing to know when upscaling is you need to pick an interpolation type that the mod that cv2 will use in order to know how to stretch these pixels out from their original size so let's do that we'll do cv2 resize of this image now let's make it 5000 by 5000 so this will be a lot bigger and we need to provide it the interpolation if you read in the cv2 documents there are a lot of different interpolation techniques and each of them have their own significance so check that out and try out different types of resizing but let's plot this it's gonna look pretty much the same as before but this image file if we look at the image resize shape again it is much larger than the original image shape which was just 500 by 500 we've actually made it 10 times larger of an image file so last thing we're going to do is show some manipulations you can make to the image data using filtering in cv2 and there are these things called kernels that we can apply to the image and let me show you some examples here this is from a nice article i found we'll call this cv2 kernels and you could see that by applying different kernels to the images you can get different results like filtering blurring sharpening and we're going to show how to do some sharpening and some blurring of these images just just to show it as an example so let's show how to sharpen first so what we have to do and we'll provide it this kernel that can be used for sharpening the image just like this as a numpy array and then we're going to use cv2's filter 2d we have a two-dimensional image here we'll provide it the image negative one for this is the um the depth and then we'll provide it this kernel for sharpening and that will become our sharpened image let's go ahead and display this sharpen sharpen image with the same code that we have before and you can see that the sharpening sort of distorts the image but you can see how it apply has applied to the image to make it look a little different next let's show blurring the image we're going to use a different kernel here this kernel uh will look like this if i show you just what it looks like um it has it's it's blurring every of the pixel in the kernel filter 2d on this image with the different kernel 3x3 and blurred we'll display this again like we have before turn that grid lines off set the title and show this that's our blurred image if we want to make it even more blurred we could make this larger it looks a little dark there but more or less blurred you can see how changing some of these parameters can really change the way the image looks let's put it back to nine for the blurred example okay so the last thing we're going to do is now that we've manipulated the image we know how to read it in we know how to display it we might want to save it once we've done some processing so you can save the image pretty easily using cv2 or matplotlib so with matplotlib you have this im save function and you provide it the image name here we'll name it as a png this is a different format than what we read it in is and we'll give it this uh image or let's do the blurred image and with cv2 you can you can use i am right cv2 dog dot png and then you provide it the name of the numpy array with the image you want to save now cv2 does provide an output that is true when the image is written it's a little different than matplotlib which doesn't provide any output and that's the end so thanks for watching this video i hope you learned something new in this basic tutorial of how to manipulate and work with image data using python matplotlib and opencv please let me know in the comments below if there's anything you liked or anything you want to see me make a video about in the future until next time bye
Info
Channel: Rob Mulla
Views: 83,764
Rating: undefined out of 5
Keywords: introduction to image processing with python, python image processing, image data, reading image into python, numpy image, opencv image data, matplotlib image processing, matplotlib image tutorial, opencv image tutorial, coding image data with python, opening image files with python, image processing with python, image editing with python, python image editing, image processing, data science image processing, rob mulla, image processing using python, opencv, opencv python
Id: kSqxn6zGE0c
Channel Id: undefined
Length: 20min 38sec (1238 seconds)
Published: Sun Mar 20 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.