Reputation: 39
For a matrix size (296 x 296), with zeroes and ones where all the ones are together in the center of the matrix forming a circle pattern, is it possible to detect and visualize only the edge cells of the circle (cells where the ones border the zeroes) and set all other values - inside and outside the circle/edge cells - to zero or null?
Is there a numpy command to visualize the transition/edge values(?)
Note: the following is not my code -- just the most straightforward minimal reproducible example I could find on stack (Python replace zeros in matrix with 'circle' of ones):
import numpy as np
import matplotlib.pyplot as plt
z = np.zeros((296,296))
# specify circle parameters: centre ij and radius
ci,cj=150,150
cr=90
# Create index arrays to z
I,J=np.meshgrid(np.arange(z.shape[0]),np.arange(z.shape[1]))
# calculate distance of all points to centre
dist=np.sqrt((I-ci)**2+(J-cj)**2)
# Assign value of 1 to those points where dist<cr:
z[np.where(dist<cr)]=1
# show result in a simple plot
fig=plt.figure()
ax=fig.add_subplot(111)
ax.pcolormesh(z)
ax.set_aspect('equal')
plt.show()
Upvotes: 2
Views: 145
Reputation: 12417
If your object does not have holes in it, this should work for horizontal and vertical edges (fixing for holes is also not too hard):
z = np.diff(z,axis=0)[:,1:]+np.diff(z)[1:]
z[z!=0] = 1
output:
Upvotes: 1