Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
614 views
ubuntu2004
Kernel: Python 3 (system-wide)
import numpy as np import matplotlib.pyplot as plt import matplotlib.image as mpimg # import the image module A = mpimg.imread("bw.png") # read the image into an array A A = np.mean(A, axis=2) print(np.round(A, 2)) # print x, rounding to 2 d.p.
[[0.27 0.35 0.27 0.37 0.31 0.25 0.29 0.29] [0.37 0.46 0.51 0.53 0.45 0.31 0.3 0.28] [0.45 0.6 0.75 0.8 0.57 0.43 0.34 0.31] [0.46 0.76 0.9 0.89 0.82 0.45 0.32 0.34] [0.51 0.79 0.9 0.92 0.75 0.49 0.43 0.29] [0.44 0.56 0.75 0.76 0.59 0.56 0.62 0.42] [0.36 0.48 0.49 0.47 0.45 0.61 1. 0.56] [0.28 0.34 0.31 0.26 0.31 0.4 0.49 0.37]]
print(A.shape)
(8, 8)
import scipy.ndimage as sn # import the scipy.ndimage package # Label the connected components (blobs) of binary image A_threshold A_labels, n = sn.label(A_threshold) print("number of blobs:", n) # Count the number of pixels in each blob and return as array "sizes" sizes = sn.sum(A_threshold, A_labels, range(1, n+1)) print("sizes:", sizes) # Get the index of the largest value in sizes (corresponding to the largest blob) idx = np.argmax(sizes) print("index:", idx) # Determine the co-ordinates of the largest blob (returned as row, column indices) y_pos, x_pos = sn.center_of_mass(A_threshold, A_labels, idx + 1) print("co-ordinates of largest blob:", x_pos, y_pos)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) /tmp/ipykernel_846/1674472634.py in <module> 2 3 # Label the connected components (blobs) of binary image A_threshold ----> 4 A_labels, n = sn.label(A_threshold) 5 print("number of blobs:", n) 6 NameError: name 'A_threshold' is not defined
plt.imshow(A) # show the original image plt.scatter(x_pos, y_pos, color="black", marker="x", s=200)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) /tmp/ipykernel_846/3753039670.py in <module> ----> 1 plt.imshow(A) # show the original image 2 plt.scatter(x_pos, y_pos, color="black", marker="x", s=200) NameError: name 'plt' is not defined
def largest_blob_coords(image, thresh): # Threshold the image bw = (image > thresh).astype(int) # Label the blobs labels, n = sn.label(bw) # Remaining code ... return x_pos, y_pos
num_frames = 20 # Create arrays for storing blob coordinates x_pos_array = np.zeros(num_frames) y_pos_array = np.zeros(num_frames) for i in range(num_frames): f = data[i, y_min:y_max, x_min:x_max] # Add code to obtain the x and y coordinates of the largest particle in the current frame here # ... # Store the current coordinates in the corresponding position x_pos_array[i] = x_pos y_pos_array[i] = y_pos
--------------------------------------------------------------------------- NameError Traceback (most recent call last) /tmp/ipykernel_846/2136523447.py in <module> 2 3 # Create arrays for storing blob coordinates ----> 4 x_pos_array = np.zeros(num_frames) 5 y_pos_array = np.zeros(num_frames) 6 NameError: name 'np' is not defined
plt.figure(figsize=(5,5)) ax = plt.gca() plt.plot(x_pos_array, y_pos_array) plt.xlim(0, x_max-x_min) plt.ylim(0, y_max-y_min) ax.invert_yaxis() ax.set_aspect('equal')
--------------------------------------------------------------------------- NameError Traceback (most recent call last) /tmp/ipykernel_846/4140317672.py in <module> ----> 1 plt.figure(figsize=(5,5)) 2 ax = plt.gca() 3 4 plt.plot(x_pos_array, y_pos_array) 5 plt.xlim(0, x_max-x_min) NameError: name 'plt' is not defined