As @John Zwinck mentions that this can be done using floodfill, but I believe your problem is that you want to go back to the original black background and keep the contours of the closed shapes. Although you can use contoursto understand this, here is a fairly simple approach that will remove all open and open line segments from the image, even if they are attached to a closed form. But keep the edges closed by curves.
:

python, ++ cv2.
import cv2
import numpy as np
im = cv2.imread('I7qZP.png',cv2.CV_LOAD_IMAGE_GRAYSCALE)
im2 = im.copy()
mask = np.zeros((np.array(im.shape)+2), np.uint8)
cv2.floodFill(im, mask, (0,0), (255))
im = cv2.erode(im, np.ones((3,3)))
im = cv2.bitwise_not(im)
im = cv2.bitwise_and(im,im2)
cv2.imshow('show', im)
cv2.imwrite('fin.png',im)
cv2.waitKey()