import numpy as np
import cv2
import matplotlib.pyplot as plt
def select_max_region(mask):
nums, labels, stats, centroids = cv2.connectedComponentsWithStats(mask, connectivity=8)
background = 0
for row in range(stats.shape[0]):
if stats[row, :][0] == 0 and stats[row, :][1] == 0:
background = row
stats_no_bg = np.delete(stats, background, axis=0)
max_idx = stats_no_bg[:, 4].argmax()
max_region = np.where(labels==max_idx+1, 1, 0)
return max_region
img = cv2.imread('/Users/nickccnie/Desktop/images.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
mask = cv2.threshold(gray, 20, 255, cv2.THRESH_BINARY)[1]
mask = select_max_region(mask)
cv2.imwrite('/Users/nickccnie/Desktop/images1.png', mask*255)
# plt.imshow(mask)
# plt.show()
>>>>>>>