2

I am trying to mask out the marking on an IC but the inpaint method from OpenCV does not work correctly.

inpainting dies

The left image is the original image (after cropping the ROI). The middle image is the mask I generated through threshholding. The right image is the result of the inpainting method.

This is what I did:

mask = cv2.threshold(img, 120, 255, cv2.THRESH_BINARY)[1]
dst = cv2.inpaint(img, mask, 3, cv2.INPAINT_NS)

I played around with the third parameter of the inpainting method but it does nothing good.

I saw an question here where someone used exactly the same approach and he also had a dark image where the contrasts are not so distinguished. I also tried both inpainting algorithms, Telea and NS.

What is the issue here?

HansHirse
  • 18,010
  • 10
  • 38
  • 67
NECben067
  • 427
  • 1
  • 4
  • 20

1 Answers1

3

Mainly, dilate the mask used for the inpainting. Also, enlarging the inpaint radius will give slightly better results.

That'd be my suggestion:

import cv2
from matplotlib import pyplot as plt

# Read image
img = cv2.imread('ic.png', cv2.IMREAD_GRAYSCALE)

# Binary threshold image
mask = cv2.threshold(img, 120, 255, cv2.THRESH_BINARY)[1]

# Remove small noise
inp_mask = cv2.morphologyEx(mask,
                            cv2.MORPH_OPEN,
                            cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5)))

# Dilate mask
inp_mask = cv2.dilate(inp_mask,
                      cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (15, 15)))

# Inpaint
dst = cv2.inpaint(img, inp_mask, 15, cv2.INPAINT_NS)

# Show results
plt.figure(1, figsize=(10, 10))
plt.subplot(2, 2, 1), plt.imshow(img, cmap='gray'), plt.title('Original image')
plt.subplot(2, 2, 2), plt.imshow(mask, cmap='gray'), plt.title('Thresholded image')
plt.subplot(2, 2, 3), plt.imshow(inp_mask, cmap='gray'), plt.title('Inpaint mask')
plt.subplot(2, 2, 4), plt.imshow(dst, cmap='gray'), plt.title('Inpainted image')
plt.tight_layout()
plt.show()

And, that'd be the output(s):

Outputs

Hope that helps!

----------------------------------------
System information
----------------------------------------
Platform:    Windows-10-10.0.16299-SP0
Python:      3.8.1
Matplotlib:  3.2.0rc3
OpenCV:      4.2.0
----------------------------------------
HansHirse
  • 18,010
  • 10
  • 38
  • 67