<jupyter_start><jupyter_text>Watershed<jupyter_code># noise removal
kernel = np.ones((3,3),np.uint8)
opening = cv2.morphologyEx(frame_threshed_mask,cv2.MORPH_OPEN,kernel, iterations = 2)
# sure background area
sure_bg = cv2.dilate(opening,kernel,iterations=2)
# Finding sure foreground area
dist_transform = cv2.distanceTransform(opening,cv2.DIST_L2,5)
cv2.normalize(dist_transform, dist_transform, 0, 1, cv2.NORM_MINMAX)
plt.imshow(dist_transform)
ret, sure_fg = cv2.threshold(dist_transform,0.2*dist_transform.max(),255,0)
# Finding unknown region
sure_fg = np.uint8(sure_fg)
unknown = cv2.subtract(sure_bg,sure_fg)

# Marker labelling
ret, markers = cv2.connectedComponents(sure_fg)
# Add one to all labels so that sure background is not 0, but 1
markers = markers+1
# Now, mark the region of unknown with zero
markers[unknown==255] = 0

markers = cv2.watershed(img_clr,markers)
img_clr[markers == -1] = [255,0,0]

plt.imshow(img_clr)
np.unique(markers)
from skimage.measure import regionprops, label
regions = regionprops(markers)

print('Number of tree logs (method 2):', len(regions) - 1)
plt.imshow(markers)
#contour & convex hull
markers.shape
ret3, markers_thres = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
markers_adap_thres = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 3, 1)
plt.imshow(markers_adap_thres)
plt.imshow(markers_thres)
markers.astype('uint8')
a, b, c = cv2.findContours(markers_thres, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
len(b)
all_convex_hull = []
for i in b:
    all_convex_hull.append(cv2.convexHull(i))
d = cv2.drawContours(img_clr, sorted(all_convex_hull, key=cv2.contourArea, reverse=True)[0:10], -1, (0, 0, 255), 8)
plt.imshow(d)
all_convex_hull
contour_area_list = []
for i in all_convex_hull:
    contour_area_list.append(cv2.contourArea(i))

sorted(contour_area_list, reverse=True)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12244897959183673, 'fraction_numerical': 0.03976975405546834, 'mean_word_length': 7.206008583690987, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '591884', 'license': 'Apache-2.0', 'n_tokens_mistral': 839, 'n_tokens_neox': 769, 'n_words': 144, 'path': 'Tree Log Counting/LogCounting-Latest (HSV, Watershed).ipynb', 'repo_name': 'vSanjeev/MSA-8395-Traditional-Image-Analytics', 'size': '1911'}