0

I'll explain what I want to achieve with an example. Let's say we get the following picture: enter image description here

I would like to write code that calculates the x and y coordinates of the entire circumference/perimeter of the object inside the image with a certain colour (R 225, G 158, B 158). In this case the object is a huge circle, but it could be any shape and size.

So I basically want my code to write all the x and y coordinates of all the corners that make up the circle. And with corners I mean the corners of the pixels that you see when you zoom in on the circle, like this: enter image description here

My question is, is there an easy way to achieve this? I have searched for an answer, but I couldn't really find anyone with the same quest/problem.

Things to know:

  • The object could be a different shape in every image, it could be a country, a car, a banana, anything is possible.
  • The object is almost always completely filled in(like our example). But it could also have a hole in it (like a donut), but not more than 1 hole (so a pretzel wouldn't work). When there is a hole, I would also like to know the coordinates of the inner perimeter/circumference.
  • The colour is always the same colour (R 225, G 158, B 158)
  • The pixels containing this colour will always consists of this colour for 100%, so there will be no fading colours or anything like that.

I would love to know if you guys know of any ways to do this and maybe know of some examples I can learn from.

(I am also super new to python, so if I missed something obvious, please tell and i'll update/edit my post)

CvP
  • 324
  • 1
  • 4
  • 17
  • You could try finding all pixels which are the correct colour but are directly adjacent to pixels of a different colour. See question [here](https://stackoverflow.com/questions/138250/how-can-i-read-the-rgb-value-of-a-given-pixel-in-python) for tips on how to get started – bunji Sep 08 '17 at 14:18
  • Would this work: Going through every pixel of the image with a couple of if statements and then only keeping the x and y of the pixels that are the specific colour and have a different coloured pixel adjacent to them. I think it would, the only problem I see is that I can't distinguish between the inner perimeter and the outer perimeter when the object has a hole in it. Any ideas how I could make an if statement that helps me write the outer pixels to for example outerpixelsperimeter.txt and the inner pixels to innerpixelsperimeter.txt? – CvP Sep 08 '17 at 14:35
  • To clarify: by circumference, you mean edge, because you'e not just talking about circles, right? How do you define an inner edge? If you have a C shape against the edge of the image, is the inside of the C an inner edge? – c2huc2hu Sep 08 '17 at 16:20
  • Yes, I mean edge. I define an inner edge as an edge that doesn't connect to the outer edge. With a C you only have an outer edge (because that edge is basically one line). With an O you have an outer edge and an inner ledge, because you basically have 2 lines that don't meet. I hope this explains it better. – CvP Sep 08 '17 at 17:12
  • Another way I think you can do this, is search for a pixel in our defined colour and once you find 1 of these pixels you check if it's on the edge. From here you stop the search for pixels of the defined colour, because now you are going to check the pixels around our first found pixel and keep checking which are edge pixels. You keep doing this with every new edge pixel you find and in the end you should end up with the outside edge coordinates. Once that's done, you can do the same inside the edge coordinates and try to find a hole inside the object. – CvP Sep 10 '17 at 14:13

0 Answers0