Matlab Programming – Episode 6 – Photo editing with for and if loops
hey viewers welcome to another episode of my metal a programming course we are at Episode six here and today we're going to see our first program using only for diff loops and a little bit of extra stuff do that you haven't seen before but I will explain it is kind of stuff that has to do with photo editing because that's what we're doing today so after we did an image we have to well open the image we have to display the image we have to do all kinds of stuff basically that you will see but it's all about before and if loops today and how you can actually use them in a program so let me first clear this crap because I kind of forgot to do that yeah this is not the first time I started this one so the problem is my f9 and f10 buttons have been blocked which is step and well posting things from here to the command window so I'm going to have to do that manually which means clicking a lot of buttons first of all we're going to start off with CLC clear and close all if you put a semicolon in between those you can put them all on one line because the semicolon says this is the end of the commands and a new command to start and now normally you would start on the next line but you don't have to do it so you can't actually put them all in one line and I get that right here so you're going to clear this that's the CLC the killer will clear out the workspace which is right here there was nothing in that and close all will close all the figures that we're now friggin open for the moment but we will see that later that it is actually useful so then we're going to load in our first photo our photo is called photo not Jeep jpg so a JPEG and as you can see we are using it read for that and if you want to use em wreaked and you were wondering what does it do you can type higher help em read you can also and greet this because this is quite a bit of text by the way so it might be more useful to use for example dark duck in Reid and then you get the whole document but as a separate file which is way better to well read it yeah it's just better than having it just as text and the final one I already discussed all of this but is hitting the f1 button and will pop up em read as kind of a quick help file I guess and that is useful as well so you can see how to do it for now I'm just going to tell you this is how you do it because it doesn't matter yet so in read photo JPEG and that is going to be loaded into our variable pixels and pixels is going to be just loaded now with all of those numbers so a photo is basically three colors which is the last variable in this thing you see here you have a length height and an amount of color so basically you have three different colors that are mixed together than in one image and yeah so you can see here the first one is the Rose the first number the second number is the amount of columns yeah that's true and the third one is the amount of well layers you have color layers in this case three and as you see behind it it says you int AIDS you and eight means that it has a bit depth of 8-bits of eight means that we have 262 nodes and fifty-six numbers that we can store in it and since it runs from 0 to 255 that is exactly what we expect so if I look at this we can actually look at it here by double clicking it you can see you can open it it says it's too big but you can actually see parts of it if you wanted to we're going to use this this matrix because it's an enormous matrix it's 3d and yeah it has a lot of data in it so we're first of all we're going to get the number of rows in our picture and those are basically the length of all the rows and the first column so I'm using the first column and selecting all the rows that's the colon there and then only using color one because otherwise it becomes a 2d array and I don't want that and basically if I watch this so if I copy paste this it is just a whole bunch of numbers but as you can see it's a column vector it's only one column and a lot of rows and if I want to have the length of that then it's going to give me one number and one number only and that is the amount of robes that are available so for the columns number of columns I do exactly the same thing but I used only row number one and then all the columns and then color number one you can also use color number two or three or I don't care but yeah the same thing for both of these these variables so I'm loading them in it says 532 rows 800 columns which is exactly the same as we saw here only now I have them as variables so I have those numbers they get subtracted from the matrix itself so if I change the picture then or the photo I should say then it will give me the right number so if you put in well like 800 and 532 there then if I can see further I have to change the numbers I don't want that I want this to be flexible because I'm I want to copy/paste it for my next example which is exactly what I did and you will see that all of the examples start like this so we are going to continue on and we're going to step through all of this hang on we're going to do that a little bit differently there you go I'll bring it up there you go dad is our picture and I will explain to you what it is first of all well figures so I'm using figure 1 which is what say what's posted in the top left this is figure number 1 you can have like 65,000 figures so you can basically choose whatever number you want I do recommend that you give give it a number you can also just do figure like this but I do not recommend that because then you cannot go back to it and well get data from it or post new data in it so if you give it a number you can address it as figure 1 and you can do that throughout your entire program so that would actually be very useful but as I said you can also just use figure it will open the next number so if one is already open it's going to take two and if a hundred is already open it's going to take a hundred someone anyhow subplots is a way to do this to get multiple plots in one figure normally one figure is one plot subplot works as follows if two rows you have two columns and this is position number one two three and four so as you can see one is top left two is top right three is bottom left four is both right I have titles on them but every subplot can have their own title as you can see full color red green and blue so you can see the different color channels of your picture basically then we are going to pick a method here because I actually programmed it so we have different methods to to do our thing yeah we're going to you have to please start this unfortunately run through this and it's going to sting once again no no bad picture yeah method is one which means we're going to go with method is one I have an if loop here I have three different methods methods one method to method three and I'm going to use one first so one as it says that it is going to use the average value here you can see what happens if you just take red green and blue I will show to you this is if you just take red and use that as your color spectrum basic this is if you use green this is what you get if you use blue yeah neither of those really like stands out but you can see the difference there's a lot less red and a lot more blue in the in the sky for example so you may want to choose green for example because it's kind of an average on the other hand blue gives you more of a sunny day feel and red gives you more of her well we went out at night field so we're going to run through this and we will see that it will pick method number one this by the way is done to prevent an error well not an error a warning because we're going to use the not the pixels themselves we're going to make a new matrix and plot our new values in that basically we want to keep our original values intact and we're going to use a different one a different matrix to put all our new values in and then compare the two in yet it doesn't really matter for now you can just forget it you will get an error if you don't do it but unless you work with really big photos it doesn't matter too much so what we're doing here is we're going to go with method 1 and I shouldn't press that because then the recording stops so for row number 1 to number of rows and this is why you needed to extract the number of rows and from 1 to the number of columns it's going to run this and it's going to say the value that I put in my new matrix is the average of the three colors as you can see it using row color columns or a row column for each of the three statements here and on one two and three for the three colors so that's red green and blue it's going to take the average of that mean you can also just add them and divide them by three of course also do it manually but mean is one of the functions that you get and then I'm going to put that into the red Channel in the blue chair puts well the new value so this this value is going to be put into the green channel and in the blue Channel so all three values will be the same making that you only have gray well accents which means well that's what you do if you make black and white so i'm i hope that's that's clear what it does and if i run it then we're going to skip all the way to here of course and it will take a little bit of time taking a lot more time than it normally does but that's quite all right it will eventually stop doing something yeah it is done normally this takes like half a second I don't know why it's freaking out and then we're going to open figure number two as you can see just a different figure and it says figure number two and we're going to say well let's see what it actually did so um oh did I screw it up here yes because I I worked with I worked with with doubles I calculated things with it it's no longer you and eight it's no longer an integer so I need to put it bring it back to an integer so you have to table you and eight before it otherwise it doesn't work and you get a white screen or black screen both of which can be possible so I'm anyway this is what the picture looks like that just yeah look at it it is kind of equal to the green one but it has more clarity and it has a little bit less of those it still has those those black edges in the corners that's just because they use the crappy photo camera to capture this because that means that your where your corners are going to be darker than the rest so there's not a whole lot I can do about it right now I can of course do everything about that if I wanted to but you can see it's it's resulting in a reason picture it's not bad anyway we're going to save this and we're going to do method is to and do exactly the same thing over so did clear or close all of the pictures all of the figures and now we're going to do method is to method is to is the maximum value so is it going to take the maximum value as it says here from the three colors and put them in all three channels that's as simple as it is it's the exact same thing as before and we're going to have to see what it actually results in because obviously it's going to be a little bit different from what you have we had previously and there you go as you can see the air is a little bit brighter here the faces are a little bit brighter so there's less contrast and there's a it's it's just the colors are not a maximum of each of the colors of course you can do the same thing with minimum values and that is method number three it's kind of lame but hey that's what it is I'm just showing you what it does I'm not saying that this is the best way to do it what it's figure 1 bug so we're going to run through all of this as you can see minimum values are not as min and it is once again call it 1 2 & 3 and then we're going to see the results in this next figure come on know you want to and there you can see because I've depicted minimum value the darker the air now is very dark so the air is basically the red air because the red is way worse than the blue and way worse than the green well the green is pretty bad as well but it is just a red air basically but these are no very high detail there are a lot of contrast in it you can actually see all of the veins in the stone and all in all if you combined rights well algorithms basically you can make a much better picture from your original picture and well in this case make it black and white we will see some other things in later stages but you might have noticed that well you cannot really compare the three pictures now I showed you three methods one two and three all three of them resulting in a different thing but you can't really compare them because they're never there at the same time so we're going to go into example two and fix that because here we're using a for loop for the methods so keep in mind we now have four loop in that there's an if loop in that there's a for loop and in that there's not a form so we have four loops inside of each other and that for each of the methods so in total it becomes kind of big keep also keep in mind that if you selected the wrong methods I now have an error message so if I were to do method is for yeah if I do method this for and just run this then it will say error and method selection because there is no method selected well there is a method selected but it's not good it's not a good one so I put that in there just to give me a warning every time as or when something well is entered that is not one two or three for this one it doesn't really matter because I can see that it's one two three but well in bigger if loops or sorry for loops or if loops whatever you may want to include that just to make sure that you're not running into trouble because you entered the wrong value you want to have full clarity of what's going on anyhow now we have methods one two and three and as I said they're in a for loop so what does the for loop do it runs with the variable methods so method is going to be first one then it's going to run this if statement so it does statement and it's going to go to figure which was on the end of our program before and it's going to make a figure with the number method plus a hundreds which means that if method is one the figure number is a hundred and one five methods is to 100 and 200 methods is three hundred and three thank you combines dirty went eight here before the picture the picture before I plot it because I didn't know that I should do that I just didn't do it in example one for some reason yeah so it's going to run three times once for method one once for Matthew once for methods free then it's going to say well I'm done here and we're going to put a stop right in the end there there you go so we're going to run through this entire thing and see what happens at them it's going to take a moments to your to process obviously this shouldn't if you're not recording this shouldn't take more than like a second but since I'm recording now and I'm yeah I have a lot of crap in the memory and this might actually take a while so figure one is going to stay to say you guys figure one i only plot that outside of the de for loop so it's plotted over here in the top and I'm never touching it again so yeah it's kind of easy to see so I do have all of the figures now this is number two and then number one so we have 1 2 3 and you can see the differences in the sky for example very bright sky very well average guy and very dark sky but you can also see the differences in the details so if we look at the shadows the shadows are much deeper in the minimum value and they are in the maximum value which is this one all you can pick different algorithms and say for example if blue is the main color because most of it is great so it doesn't really matter I mean most of it is almost great so if the main color is blue and it's well above everything else I want to have well for example this one or this one whatever you want you can you can just make it up you can select different regions of the photo with that or different colors should be amplified in different ways all you can do whatever you want with it it is just a method to show you how for an influx work and this is not to tell you how you should be doing photo editing because well that's not this is not the way obviously you want to pay big the whole photo and just edit it in one go only but really not the problem here then we're going to example number three and I kept all of the figures open just to show you what this close all crap does you know run through that and we're going to run this and as you will see it will close almost the figures there they go so CL see we already saw that clear we already saw that but close all I hadn't demonstrated it yet it just closes all the figures it's easy enough so we're going to start at number one I obviously had to try them out and that is why there's something there so um yeah once again we're reading in a picture this time is called underwater dot JPEG and it has a number of rows number of columns and we will show the photo in figure number one there it is so as you can see ya and I'll run this one that's a clear vision of what it actually is it is not a wonderful photo so it's completely blue and as you might notice there is a little bit of plant life over here I didn't really select this photo but yeah it's just a photo normally planned life as you can see becomes way too blue on these pictures so what we want is we want to have that that plant life to be green and the skintone to be back a little bit this is by the way your very bad picture this is a terrible camera which these guys have but well we're for the demonstration here it should be good so we split it into red green and blue once again so here you can see full color red green and blue on line and large it a little bit and as you can see blue is super way too bright red is super way yeah under lit under lids and yeah the green is the only one that's kind of average so we're going to make use of that those facts and added to the photo based on that so basically we need more wrap we need less blue and the green is okay what we're going to do is we're going to set up a new matrix that's what and we're going to turn pixels into a double so pixels is our photo and we're going to turn that into a double because if you multiply or add based on this and especially subtract then you're going to run into errors if you keep it as a un8 because you and it cannot go below zero and also cannot go above 255 so that is a problem of course because we want to or and and they're all integers we want to multiply here by 0.8 in this case and yeah it's going to run into trouble so don't do that I keep in mind you do have to switch back to you and eight later on to make sure that you can actually plot it but those are those things so making it a double or you and eight is really something that doesn't happen a lot unless you want to work with pictures because normally you don't work with unsigned integers or something like that you virtually always work with doubles so we run the the calculation here what the calculation does is for the red channel which is this one so the new red channel is going to be 0.8 times the old red red Channel plus the green Channel so we have the this is the first thing that's done it adds the red Channel and the green Channel and because the red Channel is well there's a leech slides data in that it's going to do that times 0.8 to make sure that it doesn't get too bright and then basically it well it's a it keeps the other two things the the green will still be the green and the blue will still be the blue so just showing you what this results in yeah let's run it there you go so as you can see if I put them under each other the red channel is now much much better than the old red Channel it still may but may not be perfect but you can also see that the photo itself just looks way more natural it's not that super bright under water color that you get from crappy cameras because there's not enough red light in this which we could see from the red Channel there's not enough red light penetrating the the water and that is why it becomes so blue in reality our eyes do not see that as blue our I see water as almost just transparent completely colorless and so this one is much better you can also see that the orange in a bathing suit has returned and apparently she has red hair and you can see the orange here has returned also the green here is now actually green whereas the rocks are just great so it becomes a lot better colored now well as I said before this is a very crappy picture but we do have better pictures so we're not going to change anything oh I will show you the final picture there it is so you can see it full screen so as you can see the colors in the bathing suit have completely returned the the green has returned and yeah for the rest it's just old gray rock so there's not a lot of color to return so how do I know this is the right one so if we do another picture we're going to underwater tooth and I will just get all of the breakpoints out because well we've seen pretty much what it does we're going to run this and as you can see we have a new picture here I'm going to put them in the same order that we had no don't make it screen size so this is the original picture well this is the original picture as you can see this is it is a fake picture by the way you can see that in a moment but as you can see the blue channel becomes very blue the rocks yeah drop arrow here it's green which we saw in a real picture is not the case so this is an edited picture but we will see that here because as you can see the red channel although it's it's less bright it is only less bright where it's blue so everywhere where it's blue so that's the background basically there you can see that the blue channel actually well is too too bright the green Channel is okay and the red channel is well too dark but all of the fishes are fine which means it's a fake because these fishes in the backgrounds can normally not even be seen without editing so if we edit it with the same thing you can see that I made the red channel a lot better it's almost equal to the green Channel now and yeah the the overall picture just but it becomes a little more a little bit less of that yeah annoying very very blue the rocks also become a little more rock color so a little less green but as I said this is a fake picture so I can't really make it better because there is no matter because it's a fake picture someone already did this so took the backgrounds which is probably a real photo and then put all these fishes in it and the fishes yeah they didn't do anything to it so basically there's not a lot I can make better then three is I think the best one to demo this is the best one to demonstrate the effect as you can see it's someone sitting in the water yeah let me just arrange these pictures a little bit so we are going from a real picture this time so this one is an actual picture you can see it here in well full size you can see that the blue is kind of tinting the body so the body is not actually body color the water is completely blue greenish and the scent even though it's sand color here does not stay sand colored you can see the beach for well all the way up to here or something but especially the colors that well did their skin color for example we want to fix that so we're gonna if we run the algorithm we get to this Oh first of all first of all let's you let's show this and you can see that ya descend kind of descent color stops here so the sand color itself stops here but then the sand stops somewhere here and the rest of the water just becomes well bluish and then we have a green which is completely saturated and blue which is actually not too saturated so you might want to switch it up a little bit but if we apply the same algorithm and we make the Reds a little brighter then it was then we get much better skin tone as you can see this is actual skin tone you can still see it under water you can see the sand color up to here where it mixes with the water color which is discolor for our eye so if you were to be in this this this water and you would sit where the photo camera is actually sitting this is what you would see because our eyes are way better than this crappy camera is so the the purple of course becomes purple the white becomes white yeah and the skin colour as I said becomes skin colour so it is just a big big result going from this to this or about this and well that's all because you just separate the three channels and then just learn that well the red is completely under lit so we need to brighten up the red add more red in there and then it becomes real again but as you might have noticed not every photo is exactly the same so on there's that so the last one done just as a demo this is from some Russian site it's apparently an underwater cave and we will see and well obviously this is the picture that this one is the picture that they posted online it is completely blue there is no way this is what it looks like I mean in reality this does not look blue otherwise she would bring a different colour lights because well there is no way you want to go into a blue environment like that if we look at this split you can see the red is almost just black the red channel is almost none available it's only available where he actually shines with his with his length and only then it is because the light spot is actually all the way here but only where it's super bright like that is where – red light dressage is dark completely dark so if we add a depth and we get it to be dis which is way better of course and we get this as a final picture and I will show that to you on full screen and it becomes much more of a well a real core I mean obviously it's not purple there either you can actually make it gray very easily but we don't want it great we want it to have some kind of color and is apparently well seeing from the other pictures especially picture number three this is much closer to what the picture should look like if you were really swimming there so anyway just a demonstration of what you can do with with photo editing with four and if loops because that's all I'm using here in this case even only a for loop but in the previous examples also if loops and you can do quite a lot with them and well this is something you could try out at home as I said in Reed's important.you and eight transformation back and forth so if you want to calculate with values they need to be doubles if you want to use em show they need to be you and eight so you can just do yeah to make it easier on yourself you can make a pixel start off as a you and eight but you can make after your calculation this statement pixels new which is so far a double is you and 8 pixels new and that will transfer everything to you into eight and then you don't have to do this anymore I'm going to keep it there just because well I like it like that because the this actually would transform the variable this only changes the variable inside of this statement so even though if I don't have this then after this entire thing it will still be a double and I personally like that I like to do my calculations where double and keep them as a double and then every time I have to plot them I just add you into eight so yeah as I said the only thing you need to learn here is in read to do photo editing and well this lengths statement is pretty useful also size we use that here it's kind of useful we'll give you all well all of the things that you can get out of this matrix so length width and or length height and amount of colors so it will give you three values basically and if you use zeros like this so zeros or once makes a matrix of zeros or ones with the size that you say so in this case the size is the same size as pixels was and that's why I use it like that so anyway hope you learned something today and I will see you next time