Question: For this example, we are going to try and write a piece of code that counts how many black pixels are touching a white pixel.

For this example, we are going to try and write a piece of code that counts how many black pixels are touching a white pixel. Though this may be a contrived example, you may find the code you write to be particularly valuable when we start talking about agent based models in the next class. But for now, let's worry about the problem at hand.

How do we go about modeling this? If we consider some pixel, P, in our image at some coordinate [,][i,j] or [,][row,column], whichever convention you prefer, we can look at each of the neighboring pixels that are surrounding it, N, by search a specific set of indices, as shown in the following diagram

We are going to write a model which looks at each pixel, P, and if that pixel is a black pixel, which has a value of 00, then we will check to see if any of the neighboring points, N, are a white pixel (which take the value 255). If so, we will count it.

Do This: The first thing we need to be cautious of is what happens when we are at the "edge" of our array. If we are in the first or last column or the first or last row, then not all of the neighboring points shown in this diagram will exist. Take for example in the pixel at index [0,0][0,0]. There is no pixel at [01,0][01,0]or [0,01][0,01] because those are "off" of the image. As a group, write a function to see whether an index is on the image or not. Some of the code has been written for you.

Finish this code def onBoard(i, j, image): if i <= image.shape[0]-1 and i >= 0: # You need some more conditions here! # We've checked i, but what about j? return True

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!