I am assembling a jigsaw puzzle with opencv in python, and making good progress. It is here on colab.
The puzzle piece images come from kaggle and piece edges are quite messy.
Some pieces here:
I applied some morphological operations, that helps smooth the contours, but this is far from perfect.
for filename in tqdm.tqdm(sorted(glob.glob("scans/kaggle/*.jpg"))):
img_original = cv2.imread(filename)
h, w = img_original.shape[:2]
img_gray = cv2.cvtColor(img_original, cv2.COLOR_BGR2GRAY)
_, img_binary = cv2.threshold(img_gray, 30, 255, cv2.THRESH_BINARY)
# no blur, it causes more harm than good for these images
raw_contours, _ = cv2.findContours(img_binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
raw_contours = [c for c in raw_contours if cv2.contourArea(c) > 100e3]
# draw fully filled contours
img_contours = np.zeros((h, w), dtype=np.uint8)
for contour in raw_contours:
cv2.drawContours(img_contours, [contour], 0, (255, 255, 255), -1)
# remove small connected dirt
img_corrected = cv2.morphologyEx(img_contours, cv2.MORPH_OPEN, np.ones((9, 9), dtype=np.uint8))
# get the clean contour
contours, _ = cv2.findContours(img_corrected, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
contours = [c for c in contours if cv2.contourArea(c) > 100e3]
Do you have any suggestion to improve contours?