opencv / opencv_contrib

Repository for OpenCV's extra modules
Apache License 2.0
9.43k stars 5.76k forks source link

Wrong cv2.xphoto.inpaint documentation or bug #2098

Closed Treiblesschorle closed 5 years ago

Treiblesschorle commented 5 years ago
Detailed description

The following doc seems to be wrong or there is a bug in the cv2.xphoto.inpaint function.

The doc for the mask parameter states:

mask (CV_8UC1), where non-zero pixels indicate valid image area, while zero pixels indicate area to be inpainted

However, it seems that the mask must be 255 for the valid image area and not just non-zero. Either the doc is wrong or the there is a bug in the function.

Test Code
import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt
from scipy.misc import imread

img = imread('C:/lenna.png')
mask = 255 * np.ones((img.shape[0], img.shape[1]), np.uint8)
mask = cv.circle(mask, (256, 256), 50, 0, -1)
res = np.zeros(img.shape, np.uint8)
cv.xphoto.inpaint(img, mask, res, cv.xphoto.INPAINT_SHIFTMAP)

plt.imshow(res)
plt.show()

If you multiply the mask with something else than 255, the output is a black image.

Also, see this question on the opencv forum: http://answers.opencv.org/question/211657/how-to-use-cv2xphotoinpaint-in-python3/

saskatchewancatch commented 5 years ago

The documentation is right. That's how a mask should behave. How the mask is used within the implementation is wrong however. I have a PR coming with a fix shortly