0

I'm working on segmentation of plant epithelial cells. My goal is to isolate cells from microCT scans and then run various metric analyses on them. Right now I am able to segment and edge detect these cells but my edge detections have gaps in them and the filled in portions are jagged. In order to fix these issues, I am converting to polar coordinates, finding the interpolation values, using linear interpolation to get smooth curves and then converting back to Cartesian Coordinates. I am running into an error where I get strange spikes in the shapes after I convert back to Cartesian Coordinates. I've attached my code and an example shape below:

##Canny Edge Detection of Edges##

#Get edge of image
#dilation = cv2.dilate(props[150].image,kernel=K,iterations = 1)
img_edges = feature.canny(props[150].image, mode='constant') #, sigma=.1)

#Sanity check
#plt.imshow(img_edges)

#Get coordinates
coords = np.argwhere(img_edges)

#Rearrange coords
coordinates = np.column_stack((coords[:,0], coords[:,1]))
sorted_coordinates = coordinates[coordinates[:, 0].argsort()]

#Finding the centroid 
xX = [p[0] for p in coords]
yY = [p[1] for p in coords]
centroid = (sum(xX) / len(coords), sum(yY) / len(coords)) #xX,yY
(x_cent, y_cent) = centroid

#Plotting shape
plt.plot(coords[:,0],coords[:,1],".") #y,x
plt.scatter(sorted_coordinates[:,0],sorted_coordinates[:,1])
plt.show()

##Converting to polar coordinates##
#Defining Conversion
def cart2pol(x, y):
    rho = np.sqrt(x**2 + y**2)
    phi = np.arctan2(y, x)
    return(rho, phi)

#Calculating distances from contours
rs, rhos = [], []
for i in range(0,len(coords),2): #5
    #r, rho = cart2pol(coords[i,0]-props[150].centroid_local[0], coords[i,1]-props[150].centroid_local[1])
    r, rho = cart2pol(coords[i,0]-centroid[0], coords[i,1]-centroid[1])
    rhos.append(rho)
    rs.append(r)
    
#Ensuring phi-values are in ascending order
L = sorted(zip(rhos,rs), key=operator.itemgetter(0)) #sorted(zip(rhos,rs), key=operator.itemgetter(0))
new_rho, new_r = zip(*L)

#plt.plot(new_x,new_y)
plt.scatter(new_rho, new_r, s=1) #, zorder=2)
plt.scatter(rhos, rs, s=1)
plt.title('Pre-interpolation')
plt.ylabel('r')
plt.xlabel('theta')
plt.show()

##Post-interpolation## 

# Convert the new_x and new_y lists to numpy arrays
new_x = np.array(new_rho) * 1
new_y = np.array(new_r) * 1

# Sort the new_x and new_y arrays based on new_x
sorted_indices = np.argsort(new_x)
sorted_x = new_x[sorted_indices]
sorted_y = new_y[sorted_indices]

# Create evenly spaced x-values for interpolation
interpolation_range = np.linspace(sorted_x.min(), sorted_x.max(), 1000)#110)

# Perform linear interpolation
linear_interpolation = interp1d(sorted_x, sorted_y, kind='linear')(interpolation_range)

# Plot the original data
plt.scatter(new_x, new_y, label='Original Data', c='black', s = 4)

# Plot the interpolated data
plt.plot(interpolation_range, linear_interpolation, c='r', label='Linear Interpolation')

# Set labels and title for the plot
plt.xlabel('X')
plt.ylabel('Y')
plt.title('Linear Interpolation')

# Show legend
plt.legend()

# Display the plot
plt.show()

##Transforming back to Cartesian Coordinates##

def pol2cart(rho, phi):
    x = rho * np.cos(phi)
    y = rho * np.sin(phi)
    return(x, y)

x_cart,y_cart= pol2cart(linear_interpolation,interpolation_range)
xn_cart,yn_cart= pol2cart(Xn,Yn)

plt.plot(coords[:,0],coords[:,1],".",c='red',linewidth=1)
plt.plot(x_cart+centroid[0],y_cart+centroid[1], c='blue', linewidth=2) #73,73
#plt.plot(yn_cart,xn_cart, c='blue', linewidth=2) #73,73
fig1 = plt.gcf()
aspect_ratio = 1
fig1.set_size_inches(4, 4 / aspect_ratio) 
plt.show()

enter image description here

enter image description here

enter image description here

enter image description here

At first I thought this was an issue of rearranging the data points ( a version of the traveling salesman problem ), so I rearranged the x-values in ascending order but that didn't seem to fix it. Next I tried different types of interpolation and using scipy.findpeaks to removes strange peaks. Though the latter is a fix for this specific shape, it doesn't generalize very well to many different shapes. I've been stuck on this issue for 2 weeks now and would appreciate some help on this. I am fairly new to python and feel like I am missing something maybe simple?

2
  • It could be that the line is "too vertical" for 1d interpolation to perform well. One idea - you could try subsampling or averaging points along x. If there are fewer samples this problem may be less likely. Better maybe is to do 2d interpolation instead of 1d interpolation. You'll need to put the 2d points in some kind of of order, but seems like you have that already. Going further, you could even "detect" where the gaps are and interpolate only there. Commented Jun 5, 2023 at 0:38
  • 1
    Thank you for the suggestions! I ended up solving the issue by averaging, resorting according to polar angle and then sorting again according to nearest neighbor! That seemed to do the trick :) Commented Jun 5, 2023 at 18:09

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.