Detecting boxes from an image using opencv

I need to find boxes in the following images using opencv. I have tried using mser but i am not getting any good results. Image for Finding Boxes

My code for MSER:

mser = cv2.MSER_create()
img = cv2.imread('Lines.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
I = img.copy()
regions, _ = mser.detectRegions(I)
hulls = [cv2.convexHull(p.reshape(-1, 1, 2)) for p in regions]
mask = np.zeros((img.shape[0], img.shape[1], 1), dtype=np.uint8)
c=0
points=[]
for contour in hulls:
    [x, y, w, h] = cv2.boundingRect(contour)
    if w < 50 or h < 8 or w>120:
        continue
    c=c+1
    cv2.rectangle(I, (x, y), (x + w, y + h), (255, 0, 255), 0)
plt.figure(1,figsize=(100, 50))
plt.imshow(I)

Result for MSER: Result for MSER

3 answers

  • answered 2018-08-13 09:24 Nazar Khan

    You can use the cv2.findContours() function that is provided by opencv. You can use their tutorial over here to learn more about it. Cheers.

  • answered 2018-08-13 09:26 Kumail Raza

    Since your input image is inverted, use 'dilate' with a suitable structuring element to enlarge the extremal regions and then apply MSER.

  • answered 2018-08-13 13:47 kavko

    You could threshold your image and invert your white and black pixels so your boxes are white seperated with black lines:

    enter image description here

    Then you can search for your contours with cv2.findContours() and then draw only the ones that fit your size criteria. You can get the size of the contour with cv2.contourArea(). Those contours are your boxes. Cheers!

    Sample code:

    import cv2
    
    img = cv2.imread('table.png')
    resize = cv2.resize(img, None, fx=0.3, fy=0.3, interpolation = cv2.INTER_CUBIC)
    gray = cv2.cvtColor(resize, cv2.COLOR_BGR2GRAY)
    _,thresh = cv2.threshold(gray,50,255,cv2.THRESH_BINARY_INV)
    _, contours, _ = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
    
    for cnt in contours:
        size = cv2.contourArea(cnt)
        if 100 < size < 30000:
            cv2.drawContours(resize, [cnt], 0, (0,255,0), 1)
    
    cv2.imshow('img', resize)
    

    Result:

    enter image description here