'How to remove those minor parts using opencv?

Each image is expected to contain only one corn kernel. Unfortunately, a corn may overlap with its neighbors. So I hope to keep the largest one and remove those minor parts.

sample 1: expected sample 2: unexpected sample 3: unexpected

From left to right, the first image is the expected case, and remaining two are unexpected case. Using cv2, can somebody help me change the two unexpected cases to an expected case?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source