<jupyter_start><jupyter_text><jupyter_code>"""
Used packages:
    cv2 (opencv)
    PIL
    skimage
    numpy
    matplotlib
"""
# opencv lib to

import cv2
im = cv2.imread('/content/drive/My Drive/image1.tif')

# emitting the red color from the image to avoid noise
im[:,:,2] = 0
# increasing the intensity of the blue color
im[:,:,0] = im[:,:,0]*3
plt.imshow(im)
# write the processed image into filtered_img.png
cv2.imwrite("filtered_img.png",im)
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image 

# reading filtered image and converting it to a grayscale
#######################################################
img = Image.open('filtered_img.png')

img.save('Cells.png', 'PNG')

grey = img.convert('L')
grey.save('Cellsgrey.png', 'PNG')

y=Image.open('Cellsgrey.png')

bthresh = y.point(lambda x: 255 if x >21 else 0)
bthresh.save('Cellsgreythreshold100.png', 'PNG')
v=Image.open('Cellsgreythreshold100.png')



from PIL import Image
pil_im = Image.open('Cellsgreythreshold100.png')
pil_imgray = pil_im.convert('LA')
pil_im.show()

pixmap = np.array(list(pil_imgray.getdata(band=0)), float)
pixmap.shape = (pil_imgray.size[1], pil_imgray.size[0])

#######################################################
# Downsampling the image to 1/4 because recursion cause stack overflow

from skimage.transform import rescale, resize, downscale_local_mean
pixmap = resize(pixmap, (pixmap.shape[0] // 4, pixmap.shape[1] // 4),
                       anti_aliasing=True)


plt.imshow(pixmap)
import matplotlib

matplotlib.image.imsave("down_sampled.png", pixmap)
plt.imshow(pixmap, cmap = "gray")
pixmap.shape
def DFS(x, y, visited, n, m,img,check_counter):
  """
  summary:
          this function (Depth first search) starts from position x,y in the given img and 
          marks any related pixels that are 255 intense

  inputs:
            x,y = starting position in the image
            n,m = image dimensions
            visited = array to mark visited pixels
            check_counter - this is a variable that we use to filter noisy pixel and not count them as cells


  outputs:  
            none as it is in-place changing

  """



  if (x >= n or y >= m):
    return

  if(x < 0 or y < 0):
    return
  
  if(visited[x,y] == 1):
    return

  if(img[x,y] == 0):
      return

  visited[x,y] = 1
  check_counter.append(1)
  # img[x,y] = 0

  DFS(x-1, y-1, visited, n, m,img,check_counter)
  DFS(x-1, y, visited, n, m,img,check_counter)
  DFS(x-1, y+1, visited, n, m,img,check_counter)
  DFS(x, y-1, visited, n, m,img,check_counter)
  DFS(x, y+1, visited, n, m,img,check_counter)
  DFS(x+1, y-1, visited, n, m,img,check_counter)
  DFS(x+1, y, visited, n, m,img,check_counter)
  DFS(x+1, y+1, visited, n, m,img,check_counter)


visited = np.zeros(pixmap.shape)
(n,m) = pixmap.shape
counter = 0



# running the depth first alogrithm

for i in range(n):
  for j in range(m):
    if(pixmap[i,j]==255):
      check_counter = []   
      DFS(i,j,visited,n,m,pixmap,check_counter)
      if sum(check_counter) >=10:
        counter += 1

      pixmap[i,j] = 255

print(counter)<jupyter_output>9
{'fraction_non_alphanumeric': 0.1640976236351959, 'fraction_numerical': 0.02151573538856776, 'mean_word_length': 3.656203288490284, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 8, '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': '348223', 'license': 'MIT', 'n_tokens_mistral': 1208, 'n_tokens_neox': 1129, 'n_words': 299, 'path': 'DFS_Flood_Fill_Algorithm_cell_counter.ipynb', 'repo_name': 'moustafa-7/Flood-Fill-Algoirth-Cell-Counter', 'size': '3114'}