 Teruskan ke kualiti Notebook Di mana-mana anda telah memperkenalkan Github Image for Sing Notebook Ada beberapa Notebooks yang sudah diberikan untuk anda Mari kita mulakan dengan yang pertama Pytone Imaging Library Google Pytone Imaging Library Pytone PIL Library Ada sesiapa yang tahu tentang Google Equivalent untuk Pytone Imaging Library PIL yes PIL is for installing the package But PIL is PIL is the same So if for you to install Pytone Imaging Library You need to do this right Okay you got that For those who can't get into TSSG You can try TSSG2 Password is 123456 7 Okay So if you have already installed Python using Anaconda You probably should You probably already have PIL If not If this fails Then you of course just do a period stop below So for those who don't know About Jupyter Notebook Who is new to Jupyter Notebook So Jupyter Notebook is an interactive Programming interface For Python It's interactive, it's GUI It's easy to use It's each of these You just need to click on The IPYMB All the Python Notebooks Has the extension IPYMB Ipyton Notebook It uses the Ipyton Library To run the pretty little thing We see in front of us So once you click on it It's a new session So for you to run any of this cell Each of this cell it could be a markdown language It could be a Python 3 format So You could have a Python 3 Format So you also have a bar 2 bar That you can use This will tell you whether this is In markdown language This is in code I'm just going to close this bar Base On the screen Everybody can see the screen fine Okay And for you to run any of these You can click run Or you can hit shift enter They will run and go to the next cell Every time you run a cell Basically this number change The number tells you What order We have run the cell So if I restart my kernel So basically it's a new session Let me close the 2 bar And you see number 1 This means it's the first cell I've run So this is the first cell Basically all the codes inside that cell I've run it Okay So Everybody with me so far Okay The Python Imaging Library Is the library that we tend to use For images processing in Python It comes as one of the core libraries That is provided For you to run images It has all the required Required tools that you need So if you just need to look At image Okay Then you can see all the different Types of image processing you can run Just by using the image library Inside the PIL library So first What we want to do We want to first get an image So run the next cell Now you have your image How you want to see your image Just need to do this And you get your image Yup This is how you open any image Simple as that So if you want to save the image In a different format Straight away This here we have 2 jpg format Library Now we run this We're getting all the files And we can convert it into jpg Okay So for this right If let's say We have a PNG here So Let's change this to PNG instead Okay Now we should have These 2 jpg images Converted into PNG If you have any questions Just raise your hands So that's just a very simple Conversion of images Next we want to create a thumbnail What's a thumbnail I understand what's a thumbnail Basically what you see inside your windows It's called a thumbnail It's a small little Small little Very small little Thumbnail basically A smaller size image Of your original image Like this To crop You just need to use dot crop You have your image This is dot thumbnail We just next do a box Basically your box Basically shows An upper Left corner XY Of your upper left corner And your XY Of your lower right corner And you get a box Like that Okay As you can see This is probably somewhere here To somewhere here To transpose You need to have your transpose You need to add in your rotation You want to transpose it into And this here just paste it into The same box That we've cropped Resize We just need to dot resize And voila Okay Rotate Similarly just dot rotate And that's it You put in the rotation angle That you want And you get a new image So try it yourself Okay Anybody have any problems? Got problems? And I already have pillow Okay So Let's see Okay The other one you need to check Is whether you have How did you Did you download Anaconda? Ya I did This is my Anaconda This is your Anaconda This is your Anaconda So try conda install Conda install pillow Ya You need to yes first Oh ya Okay You don't have the necessary permissions Is this your own laptop? Do you have admin? Okay run the command line in admin Conda install Okay Okay you need to go to your users Right there And do a conda install The same path Never broken But I think Have you run it? Ya Ya that's good And this one is You should load the first one This is the thumbnail Per this one I don't know why Ya that's good Ya that's okay What's wrong with that? Okay Ya list of files is good Okay did you manage it so? You need to find your conda If you can run the Anaconda Window Command Sorry Anaconda Command Anaconda Prom Just now You see that Conda Anaconda Prom Somebody got prank You okay? Ya I have a very dumb question How do I make changes? You need to run From the first cell onwards How do I run? Shift enter Click on it You need to click on it first Okay so this is This is not This is not Jupyter Notebook This is GitHub Jupyter Notebook? Ya you need to Have you launch that? Okay launch that Ya Okay go to your Project repository Project repository Have you downloaded the Where did you download into? Okay so go to your documents Documents GitHub Image processing Notebook Okay Now click on that And Shift enter Okay you are good Okay so Have you run your Jupyter Notebook? You need Jupyter Notebook So is there a reason You are not installing? I just try to name this You need Jupyter Notebook Have you installed Jupyter Notebook? No Okay Ya So install Jupyter Notebook By anaconda Or however you want to Are you a programmer? Newbie? Newbie So I'll just recommend you to install anaconda Anaconda Okay you have all the links from the meetup Okay Install anaconda Okay Okay install anaconda I'll try to launch the Jupyter Notebook But you commit the error When you install Did you install everything? Ah yes On Python 3 or? The latest one Python 3 Okay Did you do you have administrator rights? Ah yes Okay Okay You might need to reinstall Okay Ya reinstall this There's something wrong with your installation This is brand new? Ah yes Ya You can search Sorry, not search Search for the error code Sorry Is it okay? No it's okay Oh you have Why do you have two? I call you I'm not sure why there's two Why is this? Ya that's not good First we start everything Okay let's restart everything We restart my computer Okay You okay? We install it But then I restarted my environment Really but she had this And you launched this from Ah you didn't launch this from Anaconda Did you launch this from? I launched from Jupyter Notebook Using Jupyter Notebook Okay so that's a different one Oh So if you launch from Jupyter Notebook Do this pip3 install Pillow Install Pillow Pip3 install Pillow Or you don't not recognise Okay So search for your You install Anaconda right? Ya search for Anaconda Yes that one, navigator And launch Jupyter Notebook From Anaconda And try again Okay So just now was just a simple exercise Now let's look at Matplotlib Okay so for images right When you're doing coding Image processing on images It's actually a lot of Mathematics involved All those algorithms you see When you use any Any image processing Applications like Give me one Any Like Adobe Photoshop Right All those They have a lot of mathematics at the back end Right at the back that you don't see So when we deal with images It's actually a lot of mathematics The libraries that we use To process all these images Are the mathematics libraries So Matplotlib NumPy And another one If we have time I'll introduce to you is the SciPy Right So Let's see So Matplotlib It's great When we deal with mathematics And plotting of graphs And when we're talking about images As I said 2 weeks ago We'll need to deal with The individual Members that represent Every single pixel In your image So again let's import our image To show an image using To plot the image on a graph Or Matplotlib graph You just need to do I am show And you have it on a graph And basically these pixels On your X axis Sorry And your Y axis 800 pixels So let's say We plot some points We plot X And we plot Y And wala We have X And Y And this here I'm drawing a white line White line from X1 Sorry from X1 To Y1 And all these you can You can Google For what all these notation stands For These stands for a red star So these stands for white Blue I think it's B Black is K And so on and so forth Okay The other thing That we usually want to plot Is the contours In an image So we are talking about The difference On the brightness of the image When we are plotting The contours of the image We are plotting about A grayscale image So from the The difference In the lightness And the darkness Of your neighbouring pixels That's where your contour lies So simple way of Plotting your contour first We need to first convert Your image into An array So let's take a look What do we have in here Okay So this array Every single element Of your array represents A grayscale value So this means It has a grayscale value Of 157, right? And from this array We can now plot the contours Now that we have the contours Just need to use the contour You have your figure Gray, all these are Comments And you just need to Plot your contours By passing in your array Your two-dimensional array And voila Okay So the difference in colours Basically Darker in these The bigger the difference That's it It's that simple Between the pixel And its surrounding neighbours So like this It only has If you want to Plot the average difference Between this pixel And the neighbourhood pixel That's what you get The other most important information About any picture Is your histogram plot So this here We are only plotting The grayscale image Of your histogram You can always plot The red Red, green Or yellow Of your image Red, green Or blue Pixel of image Just converting into RGB Plot the different channels And you get a different plot But here we are just Getting the grayscale image And we are plotting it right here And you can see From the histogram It means that We don't have A lot of white Right 255 means it's white colour So the whiter colours Is lacking And it has more darker Colours in the image Okay Now Get one of your own images Try to plot it Okay Okay, ready? Okay The path Good Great stuff You okay? So you're freaking out? Ya Okay So you're opening your image From a different path Okay Right So but you're running From which directory? So your Python library is run On the C drive C drive C drive also C drive And this here Check Where is your GitHub Where is your GitHub? I don't think she used git to install She just Ya, download You download the file directly But you did not download the image You did You download the image to where? Also to To see actually So to hear The same base Where is the image? This part here Ya And the image is For Skyline Ya Okay Let's have a look So where is your Ya Okay Okay So this here Sorry Do you need more? Ya Okay So Don't move the Don't move the mouse while I'm typing Okay Don't touch anything Backspace, right? Ya Okay No, try this Okay Anybody You okay? Okay I need to put the image into the Yes Okay, good Ya, restart thing Hopefully you can follow Okay, anybody got Any problems? Nice Pretty Everything okay? Lovely Okay Okay Okay Okay Can You okay? Okay Okay So any questions About contohs or histogram? If not, we'll move on So next we'll Introduce the NumPy library For image processing So again I already talked about How mathematics Is very useful In terms of image processing And the NumPy library Is basically the Python Library for any Numerical Calculations You need Right So we'll see What sort of numerical calculations That we're doing For the next couple Of image processing That we have So here We need to Import the NumPy library So typically For Python The NumPy library We just Use it as NP As a short form And the map prodlit as PLT Right We import our image library And you need your Magical keyword For the map prodlit inline So this basically Is just for Jupyter Notebook If you're not running On Jupyter Notebook You don't need this Okay Alright So again Let's Import Our image As an array So We have our shape Which is 600 pixel By 800 pixel 3 This means that it's RGB format It has 2 channels It doesn't just have one channel And For Most images When we are doing processing If you want to display it It's in UIN 8 But you don't have to You can process it in float UIN 16 It's up to you Right Only when you want to display it Then you need to convert it Back to UIN 8 Okay So it's an array So you can access Any Value of your array Through your normal Array Access Okay Okay So this here We are opening the image As a black and white image L And we are converting it Into a floating Marital value So let's try this This here We have Black and white image So we don't have Three dimension What do you call that? We don't have The third dimension Of your array Basically It's a 2D array We don't need a 3D array To represent The three channels We only have one 2D array To represent Each value Gray level Value of your pixel It's 600x800 pixel And it has a floating Value of 32 Simple as If you want to check Any Array Just print it out Okay This is already an array So we don't need that Okay So a couple of different Gray level transforms That we can do So what we are doing here Is that This is the first one We are inverting an image How do we invert that image? All the images Is represented from 0 to 255 You want to invert Just flip it around Do a 255 minus Your image This here We are just setting The gray levels We are setting The gray levels To make sure that It goes within The interval Of 100 to 200 And this here We are just squaring the values So let's take a look What we do to the image Okay So this is the original image And the Second image Is just an inversion Right Simple as So as you look at this image Just remember What Numerical transformation That we have just done We invert the image Just by just minusing The image from 255 Next one We are clamping the values So the images Is a more gray Color tone Softer And this here We are What did we do We squared the image So we made it more Contrast We have a darker image Okay For image resizing There is The resize This is just a very simple Function To resize your image You can straight away Do an image resize A PIM Image dot resize Basically your PILO library Once you've got Your PILO library So this here Is just transforming Your array into A PILO image And from a PILO image Use the resize Transform it back into an array That's it So now we have A new array Of 200 by 150 pixel Histogram equalization As we talked about Histogram Equalization 2 weeks ago It's great Equalization basically As we have seen This Histogram This is quite One sided It's very much Tutored towards one side We don't have anything From 200 to 255 And we have a very high Concentration Between these values About 175 to 200 Right It's high concentration So what we talk about Histogram is that This is the histogram And when we Equalize the histogram That means that We are flattening out This histogram Right here So what does that do To the image Let's have a look So this here This is basically We get the histogram Of your image We put it into The number of bins We get a cumulative distribution And we use A linear interpolation To flatten out The distribution The cumulative distribution Once you have a cumulative distribution You can flatten it out Right A cumulative distribution Basically Goes like this It culminates So the number of pixels You have Basically it goes up And once you flatten it out We can get Once you equalize The distribution It becomes a normal distribution Normalize it And then we have Our new values And that's it Okay So let's try it on our image And there you go So as you can see The previous image is a lot darker Now we have a lot more white What does that do To the histogram That's how it looks Any questions? Okay So give it a try Plot the original image And make a comparison Let's do that together So just Okay So with the histogram Side by side You can see How the original histogram Is like that We just equalized it We pulled it apart Make sure That it's more evenly distributed And we get a new image Again You can do this For the different channels If you have an RGB image You can do the different channels On the red channel The green channel And the blue channel All you need to do You have a three-dimensional array Do the same for each dimension Of your array The three different dimension Your red channel The blue channel But obviously If you do that The results Are not exactly the same As what you have For your gray level Okay Alright So if you have no questions We can take a look At your sci-fi library So for sci-fi library Is the library for all Your scientific calculations So if you need Whatever you do On a sci-fi calculator That you don't get On your normal calculator You'll find it In your sci-fi library Okay For a sci-fi library We can do more With your image So in your sci-fi library There's this thing Called the ND image The n-dimensional image And we can import the filters From that library Inside that filter That's all sorts of things So if you Let's take a look Alright There's all sorts of things That's your Gaussian filter Maximum filter Uniform filter You can play with it Okay The Gaussian filter Is one of the More famous filters That we use For blurring an image It uses the image Standard deviation To perform blurring So this here We input 5 standard deviation Means that This is the amount of blurring That we want to Achieve The higher it is The more blur your image should be And Once you've gone through A Gaussian filter We have our new image So This here Again This is on a grey level channel So if you want to do On a RGB Image This here We are going through The red Green and blue channel And we're doing that We have our new coloured image Image intensity We talked about When we do the histogram That shows A different image intensity And from there We have gradients We can calculate gradients And we have Once we calculate Our gradient magnitude And gradient angle We can perform A different filter So this here We calculate the derivatives For the different images For the X And the Y So we are calculating From the X Basically on the rows And the columns Of each Of your image So once you calculate The filters From your rows And your columns And you calculate The magnitude From these two You get Your gradient magnitude So your gradient magnitude Is basically For each row How big is your magnitude And then from each pixel How different Your pixel is From the magnitude What is the Angle of magnitude That you get Okay Next one A bit more fun Counting of objects So for any image That For an image Let's See So this is the image That we are going to get Right We want to count The number of objects In this image So for any image You can basically Create a Trash holding We talked about trash holding You do a trash holding You have black You have white You have nothing in between Okay Once you have done A binary trash holding So once that's done You can count The number of white objects Rounded by black Or black objects Rounded by white So this year We import The measurements And the morphology Packages From the ND image Again We need to work On a grey level image It doesn't work On a colour image So We need to convert it Into a grey level image And here Just by doing A simple trash holding Once we do a trash holding And we divide Your image into Black and white We use the measurements label And we can count The number of objects Okay Very simple Next Let's count The individual Zero and once So a tip On how to do that This is a slightly different Grey level, right? So instead of 150 You need to Change this value So let's try that Who can finish this first I'll give you another sticker You don't have a laptop Even with the new one Ya Oh no It's fine Because I'm a beginner So Okay Okay Sorry You okay? Could you get it? It's okay I mean if you've Done it so far Count the individual Zero and once Include the zero and once Do the same thing You just need to change the value Change the trash hold value Okay Okay, what's the new value? How many zero and once Included On top of the The other Objects In the image Who got the new value Shout it out 1730 No Just need to change this value What How many number of objects did you get? Count the number of zero and once You have Included Include the number of zero and once In this image On top of the other objects Anybody want to give a guess? Beautiful You get 188 So basically this trash holding Image trash holding Is just Converting every single value Of your array Of your image array Into either one Or zero And from there The measurements dot labour It will basically labour From here I can't We can't see the whole thing But basically You will see the surrounding images It will go There's a way they calculate There's different ways to calculate Basically they go down the rows Sorry They go Go through the rows One by one And basically once They hit A difference of value They hit a difference of value They will labour That Byte With a number So this here It will labour as zero And it will go down here With one Two, three, four Until it hits 188 Like so Correct So You're absolutely right It could be my mistake If anybody would like To basically count The number of objects There are It basically You need to look at What's the difference What is the threshold To get Between this colour And this colour You get what I mean? So you can print out The array And get the value of this byte And the value of this byte You need to set Your threshold Between these two values So just now If we put We set it to 150 That means Both this byte Both this grey number Is It falls On one side of the threshold It falls On this side of the threshold So we need to Push the threshold value Lower So that there is a difference So that if this is one This is Zero This is also zero I mean So previously if the threshold is 150 That means this is If this is zero This is one Or this is one This whole thing is one So you push the threshold lower This is zero The darker grey is one But the lighter grey is zero Okay And that's it For our session today You can play with it Try with your own images Transpose it Flip it around Rotate it Create your own histogram Your own contours Give it a try Alright Thank you