I want to draw a box around the watermark in my image. I have extracted the watermark and have found the contours. However, the contour is not drawn around the watermark. The contour is drawn across my full image. Kindly help me with the correct code.
The output of contour co-ordinates are:
[array([[[ 0, 0]],
[[ 0, 634]],
[[450, 634]],
[[450, 0]]], dtype=int32)]
The output image is:
My code snippet is as follows:
img = cv2.imread('Watermark/w3.png')
gr = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
bg = gr.copy()
closing = cv2.morphologyEx(bg, cv2.MORPH_CLOSE, kernel) #dilation followed by erosion
#plt.imshow(cv2.subtract(img,opening))
plt.imshow(closing)
_,contours, hierarchy = cv2.findContours(closing, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
print(contours)
print(len(contours))
if len(contours)>0 :
cnt=contours[len(contours)-1]
cv2.drawContours(closing, [cnt], 0, (0,255,0), 3)
plt.imshow(closing)