import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt

img = cv.imread('C:\\Users\\Admin\\Desktop\\Untitled-9.jpg')
img = cv.resize(img, (400,400))
img = cv.cvtColor(img, cv.COLOR_BGR2GRAY)

cv.imshow('src' , img)
ret, thresh = cv.threshold(img, 127, 255, cv.THRESH_BINARY_INV)

# noise removal
kernel = np.ones((3,3),np.uint8)
opening = cv.morphologyEx(thresh,cv.MORPH_OPEN,kernel, iterations = 2)
# sure background area
sure_bg = cv.dilate(opening,kernel,iterations=3)
# Finding sure foreground area
dist_transform = cv.distanceTransform(opening,cv.DIST_L2,5)
ret, sure_fg = cv.threshold(dist_transform,0.7*dist_transform.max(),255,0)
# Finding unknown region
sure_fg = np.uint8(sure_fg)
unknown = cv.subtract(sure_bg,sure_fg)

# Marker labelling
ret, markers = cv.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 = cv.watershed(img,markers)
img[markers == -1] = [255,0,0]

cv.imshow('img' , img)

cv.waitKey(0)
cv.destroyAllWindows()

