How Do I Find Image Pixel Co-ordinates and Neighbours Using Python?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today's video is going to be super interesting we're going to be finding all of the neighborhood pixels of any pixel we specify in an image uh so that's going to be really cool and really useful so if you've been following along with the series where we're doing image segmentation via graph cuts then you might remember we're at a point now where we essentially want to determine the edges of all our nodes which represent our pixels now we can do that in multiple ways um the reason i'm doing this video is and we might not do this as a final version but the reason i'm doing this video is just to give you an idea of exactly what it is that we're trying to do it's going to be super clear super concise i'm going to go through everything line by line and what we're going to end up with is hopefully some you know useful information right so we should begin by just creating a empty file i've called mine get neighbors dot pi so once you've done that uh you want to open that so let's get neighbours.pie and this is just an empty file and first thing we're going to do is import a couple of things we're going to import numpy as mp which is what we're going to be mainly using and we also want you know what i'm like i always like to visualize things and plot things just to make it easier for you guys and for myself also so matplotlib dot pi plot um as plt and so what we want to do next so remember the whole idea of this series even if you're watching this as a standalone video what we want to do is we want to convert an image into a graph and the information we essentially need is how does any one pixel relate to all the pixels around it now it doesn't matter it's it's going to be very dependent on how many pixels we want it to affect but in this video we're just going to be doing doing it based on euclidean distance um in another video we'll probably complicate it a bit more as well so we're going to start now by i'm just going to basically create an image rather than working with a real image just so it's much smaller and we're going to do mp.random um random and we want anybody to like it's basically going to be a grayscale image just to simplify even further so what are we doing here we're creating a random um image i suppose and which is just going to consist of values from 0 to 255 and it's going to be 4 rows and 4 columns so it's 16 pixels in total and we're just going to do pot dot m show and we want to do a couple things here firstly what do we want to display we want to display our image and we need to make sure that our cmap is grayscale because that's otherwise we don't have enough um pixel values if you will we need we would need rgb values and then we can just do pot dot show and we're going to that's the very first thing we're going to do now and let's go back to our command line and execute our python script and this is the image we've essentially created right so we've got four pixels um four rows and four columns and they hold different values so this is going to be different every time of course because we're randomizing it but it doesn't really matter too much i just wanted you to get an idea of what we're working with so we're in a great place now we've created our super simple 4x4 image which is a grayscale image and it's just to give you an idea of what we're going to be working with just to give you a visualization what i want to do now is define three variables and that's going to be the width and the height and that's just going to be equal to our image dot its shape now of course these are just going to be equal to these values so we're going to have the width as 4 and the height is 4 and if we change that to 5 then our height would be 5 and so on i also want to define a variable that we're going to call the index and it's probably better for me to just show you what the index will be so we chose five and we're going to say the index is zero one two three four five so it's essentially um you know our starting at zero it's the posit it's the pixel we have at position five and what we want to work out in this video is basically is how does the surrounding pixels relate to this pixel so we'd be looking for this pixel this pixel this pixel and this pixel and depending on which euclidean distance we choose maybe also these pixels so that one this one this one and the top left one but that's getting ahead of ourselves so let's just ignore that for a moment remember guys we're trying to pinpoint all the surrounding pixels of our chosen pixel at our particular index so for that it will help if we knew where the what row and column we're on so we can kind of look above below to the left and to the right now as it is we can actually determine that or we can extract that information just based on the width of our image and the index with two little neat operations so if we just get started we're going to define our row and column as the following we're going to use floor division and the width of our image to get the row and we're going to use uh what we're going to use we're going to use our index and we're just going to use the modulus operation with the width to get our column now i've tried explaining it a couple of times but really the best way to do it is to just play around with it and then you'll see why it works and i mean you can kind of see you know if you if you have an index of five and you have the width of four then you're going to get a remainder of one which takes us on to the next row and it's to do with the fact that it's always going to be you know a multiple of the width so we can kind of determine where we are in this grid anyway let's continue so if we go ahead and do a nice print statement now and we're going to say the row is at this placeholder well and then our column is going to be at the following placeholder now this is just a simple format statement you should you've probably seen this before but it looks neat in my opinion so i'm doing it and let's go ahead and execute this python script and that's our image it doesn't really matter too much and we do in fact get the information that our pixel that we chose which remember was here is at location on row one column one which is exactly correct so the next thing we want to do now is to create our uh pixel neighborhood if you will so that's going to consist of the pixel that we have at our index and all of the surrounding pixels within a certain radius and which we're going to pre-define for now as 1.2 but again that's going to be slightly later in the video so the first thing i'm going to do is define this variable r which is just going to be an integer version of our radius now this is turning our 1.2 into an integer which is just going to equal 1. and the reason we're doing this is just for a particular calculation so just stick with me and all should be good now next we're going to create we're going to create two lists and these lists are both going to hold the vertical and horizontal neighborhoods so again our neighborhood if we just go ahead quickly and execute our python script let's tidy that up by doing a new one that doesn't help let's go back okay so our neighborhood is everything around this black pixel in this particular case so we want to create a list of these values and list of these values and we possibly like i said care about these values we're going to include those um in just a moment so okay so we've got that so how we're going to do this well we can use x equals mp.m range or a range and what this does is so for example zero three i'm just going to show you what's going on so you can kind of stick with me maybe you already know numpy now let's close this so we've just created a range zero one two so things to note here is that it starts at zero uh but it doesn't include three so it's it's as it starts at zero and it's of a size three elements okay so that's what we need to note here uh and instead of having zero here what we're going to have instead is we're going to get the maximum value of this expression now this is either going to be our column minus our radius and again we'll look at that in a moment or it's going to be zero now in a particular case that we just did our column is one and our um radius is also one uh so this is going to be zero so we are in fact going to be starting our range at zero which makes sense remember because on column zero is um both on column and row zero we've got neighboring pixels which is what we're looking for so min then is going to be similar but it's going to be in the uh uh it's going to be in the opposite direction and we want to find the minimum value here and that's going to be either the width which makes sense um and this is going to be plus r let's make it look the same plus one and why we're doing the plus one because we noticed just a moment ago that when we specified um our a range to three it didn't actually include the three right it just went to um to two so it was a size three but it went zero one two so we're adding plus one to make sure it's the correct length um so everything here seems to be in order okay so next we want to do the same height and we want to do it with width and when i say width of course i'm in row so what would and this needs to be y so essentially now we're creating here a bunch of y values you've got y values and x values which are both going to be zero if you follow if you do the logic they're both going to be zero one and two and you might be like well what's the point in that they're not our values but it's so we can uh you'll see we'll see this is going to allow us to create a mesh network which then in turn is going to allow us to do some euclidean mathematics like pythagoras theo so that's basically where we're going with this so next i'm going to create a mesh grid for you which i'm then going to display for you so you can see exactly what is going on and it will help me too so we're going to plot this now mesh network is i'm not even going to explain it but essentially yeah now let's just keep it like that i'd rather show you rather than trying to explain it with words because it's just one of those things our marker is going to be dot that's going to represent our our grid and we want to make that black and line star none i don't know if we need this probably not and we're going to do plt plt.show and i'm actually going to comment these out we no longer care about that really column is not defined um so we've got why is column not defined we got rid of our previous things so i must have um deleted that by mistake luckily i've got it saved here that should work column is still not defined let's change this to column and i put lifestyle did i no uh no object has no property line style it says liney style all right let's tidy this up voila this is what we wanted and that's what we've done with the previous previous lines of code so this is our neighborhood now right um well somewhat this represents our um pixel that we called our index pixel and then we've got pixels to the left right top bottom and in the corners also so this is what we were trying to do and now we want to do is say hey what pixels are within a range of 1.2 like our radius 1.2 now we know the distance from here to here is 1.1 as one and down here also and all these pixels are one pixel apart right so all of them have a radius of one relative to each other but these diagonal ones don't so the distance from here to here is going to be 1 squared plus 1 squared and it's going to be the square root of that which will be the square root of 2 which will be approximately 1.4 which again is not one is less than 1.2 it's more than 1.2 sorry and it's not going to be part of our neighborhood but if we change this to 1.5 it would be so this is what that's where we're going with this so continue um so now we're going to comment these up momentarily create r r is going to be like i said it's going to be pythagoras's theorem and we can use the square root function and inside our square root function we are going to have a couple of things um there's going to be an addition of course and then we're going to have these two so i'm going to be adding that and then this is going to be squared what's that going to be it's going to be x minus co squared and you can check that yourself if you want and then we're going to have y minus rho to the power of 2 also so what we're saying here is going to be the square root of x minus the the column and y minus the row which then is going to give us the distance of each one so it's going to return a a matrix of values so it's going to be really neat let's tidy that up a little bit bring that in and make sure everything is closed i almost feel like i'm no this looks good so let's print r i've done it again column okay there you go so we should probably display this actually but you probably know what's going on now and then one more time just so we have all the information okay so we've got our grid and what we're basically saying is that this is our index pixel so the distance to our index pixel is zero that's that's to be expected to this one it's one one one one and then one point four one point four one point four one point 1.4 we only care about the distance we don't care about direction or anything so they're not negative um they're all positive values that's okay so what next next we want to create a new matrix which we're going to call mask and we're going to we're going to basically say um it's going to hold the values of r but it's not going to hold the values it's going to hold true or false and we're basically going to say look if r is less than 1.2 then that particular element is going to hold the value true otherwise it's going to be false it's probably easier to just show sometimes words just go over your head um so there you go we already knew that 1.4 is bigger than 1.2 so we've got this this is our neighborhood all these ones are as far as we're concerned uh in terms of euclidean distance at least which is only one metric you can use we could also use the actual color value which we'll do later um as far as including distance is concerned these are the only um pixels that are actually in the neighborhood of uh our index pixel five so we've created that um so this is great we've got our mask now which is going to be super useful for us and remember mask is telling us which pixels are actually part of the neighborhood and which aren't um based on our radius so what we want to do now is apply that mask to our x values and y values here and i'll talk through just by in just a moment but say we want x mask we're going to print this and i should probably have a print statement as well so we know where we're at so x mask and let's just copy all of that down below so we want y mask as well please and we want y here also so firstly what are these two well x and y if you remember they were allowed us to create the mesh grid which is the graph that we see um let's plot it not this one this one so basically they hold the values the column values and the row values for all of the potential pixels in our neighborhood right but we don't care about all of the pixels and our potential name in our potential neighborhood we only care about the ones that are now um not being masked so we care about this little cross in the middle those are the values we want so all we have to do now is apply our mask to it which we've done and then what we end up with is in that particular neighborhood the locations of all of the pixels we care about and actually let's execute this again so i can just show you so remember y represents the row values and x represents the column values so if we go through this quickly what we're saying is remember our index was five so this is the pixel at the center of our neighborhood and what this is now showing us is okay well one of the pixels in our neighborhood is row zero column one which is this one then we move along then the next one is column zero which is this one row one which is this one then we've got one at column one row one and column two very one and then finally we go on to row two which is all the way down here and we go to column one which is here so this is telling us that this is our neighborhood um so hopefully you understand that so if we quickly were to change this for example to 1.5 and make sure by the way you change this to radius because i hadn't done that this was a float number we had so come down to mask equals r it's less than radius and based on what i just said we should have four more values now remember because it was a 1.4 was the cutoff and we've put it up to 1.5 so you can see that we've got way more values than we did before and there will be formal values because they're all true so that's what we're doing so now what well we don't really care about where the positions are relative to the we don't have all the information yet right we want to be able to literally say this pixel in this entire image is part of this neighborhood so we need to be able to access the pixels by index that's what we're saying so we're almost done now we've literally only got to write a couple more lines of code nature could do it yeah a couple lines of code so we're going to print this and then i'm going to talk you through it that's how i like to do it let's focus on the practical parts then the theory i don't like just waste too much time on theory theory i just think uh it makes the video too long and we want to do times the width explain it um yeah and then i'll just do this so what we've done here is we're basically this is going to give us the index of our pixel of our pixels and i'll explain to you why it works in fact the way i'm going to do this let's actually make this a bit nicer i'm going to reverse this not that it matters but i think it's better and let's just make it although it's using bitmass of course or whatever you've been taught let's put on brackets just so it's very clear we're doing the multiplication and then adding our x mask and let's talk you through it so firstly let's run the script so you can see what the values are and let's execute again so you can see that this is our grid now uh our image and we're basically this is giving us the exact locations in terms of an index of our pixels if we were to um flatten it into a line um so what have we got here well it's given us yeah the pixel index so at let me just do this quickly that's right so our app index one we've got one of our pixel neighborhoods under index four and the index five index six and index nine so that's exactly what we wanted to do so we've managed to return the index values of all the pixels are part of the neighborhood for that particular index so for five so that's what we've returned just on putting in the value five so that's great so how does it work why does it work it's quite simple if you remember the formula so we had our y index which is our rows and we multiplied it by the width why does that work okay so suppose we're on row one which we are and this is row zero then we know that the width is four okay so if we're on row one then we know that there's been at least four pixels right so we multiply it by four and say look we're on row one which is our y value so four times one is four we've got at least four pixels and then all we're going to do is we're going to add the column which we're in right so um our x value is one so we add one and then that's going to return the value hopefully you understood that to me that's quite clear if it isn't clear to you play with it ask a question and again i always suggest asking questions i don't even mind that much if you like the video or subscribe but if you've got a question you should always aim to you know understand really understand what you're doing don't just kind of get it to work but anyway i'm ranting a bit so there's one final thing we really want to do but this is a really cool start but we also want to essentially return the distances right because we'd be using those in the series later and we can simply do that by using our mask again which is why it's so cool so we created that mask but this time our distances if we remember were in this that's what that was all our euclidean distances and we just apply our mask to it and it's just going to affect this graph so that's r and we're going to apply that false we're going to apply this matrix to it and it's going to return these uh in order like it has with these so we should get 1 1 zero one one that's what should happen assuming we've made no mistakes uh so that's exactly what we got this is pretty neat and you can play around now so if you notice that these values here at one point four one four square root of two uh if we now increased the radius to one point five then we would actually just get everything in that uh original graph we drew a mesh network mesh grid whatever you want to call it so now we've got these in order and then these are the pixel indexes of um let's do that again so just to clarify so now all of we're saying that all of these pixels here are in the neighborhood of this pixel um and we can kind of prove that you know we've got we've got one at zero one and two not three but we've got one at four five and six not seven but we've got ones at eight nine and ten so that's really cool uh now what i would advise and maybe i'll do that um is to really clean the script up what you probably want is a class well probably just a function or class whatever you want which takes some parameters the only parameters we need if you've noticed is we need the index of the pixel we care about we want the radius and we need the width and the height of the image because that's how we calculate do a lot of our calculations working out where everything is and etc and other than that we don't need any information the rest is just worked out mathematically so that's everything hopefully that was useful and we'll be going further into this series still to actually do the image segmentation and it might be slightly different to this i just wanted you to really understand what we're doing so this is just going based on the radius now of course when we do image segmentation and we're adding edges to our graphs we might not just be going on a euclidean distance we might also be going on how similar the pixels are so it'd be a product of the distance and the brightness or the color uh something like that probably brightness because it's a greyscale as a single list rather than rgb which is three valleys so we're going to be playing around a bit more so subscribe and i will see you in the next video
Info
Channel: MonkHaus
Views: 5,311
Rating: 5 out of 5
Keywords: python find pixels, pixel locations python, image segmentation, graph cutting, graph cut, find pixel values, numpy image manipulation, euclidean distance between pixels using numpy, How can we find pixel coordinates in python, How to find pixel coordinates of a specific point in an image in python, How to find pixel neighbours in Python, How to find neighbours in a numpy matrix, How to determine a neighbourhood of pixels in an image
Id: RHTzPCM5vnw
Channel Id: undefined
Length: 25min 48sec (1548 seconds)
Published: Tue Nov 17 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.