| 
					
				 | 
			
			
				@@ -0,0 +1,45 @@ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+from skimage.filters import threshold_otsu 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+from skimage.measure import label, regionprops 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+from skimage.morphology import binary_closing, binary_dilation, binary_erosion, binary_opening 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import matplotlib.pyplot as plt 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import pathlib 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+def binary_append(image, func, times: int = 1): 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    result = image[:] 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    for _ in range(times): 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        result = func(result) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    return result 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+all_pencils = 0 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+for index in range(1, 12): 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    image = plt.imread(f'pencils/images/img ({index}).jpg').mean(axis=2) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    thrashold = threshold_otsu(image) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    image[image < thrashold] = 1 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    image[image > thrashold] = 0 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    # Большой closing 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    result = binary_append(image, binary_dilation, 25) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    result = binary_append(result, binary_erosion, 25) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    pen_count = 0 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    labeled = label(result) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    regions = regionprops(labeled) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    for region in regions: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        min_row, min_col, max_row, max_col = region.bbox 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        width = max_col - min_col 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        height = max_row - min_row 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if (width / result.shape[1] > 0.4 or height / result.shape[0] > 0.4) and \ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        not (max_row == result.shape[0] or max_col == result.shape[1]): 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            pen_count += 1 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    plt.subplot(3,4,index) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    plt.title(f'{pen_count} карандашей') 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    plt.imshow(labeled) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    all_pencils += pen_count 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+plt.show() 
			 |