1

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

2
  • Since you already have a binary image I would try to cv::findContours and keep only contours that can be simplified e.g. with Douglas-Peucker algorithm using 4 points. I am having a code for this but only in C++. Commented Aug 13, 2018 at 9:17
  • in python you can use the cv2.findContours function Commented Aug 13, 2018 at 9:21

4 Answers 4

4

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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.

Comments

0

I think you can use pixel patterns to identify boxes. As the example go through the each pixel in image and when you get an white pixel then findout the next pixel colour in x axis and y axis. if both are white then consider that pixel as the first pixel of box. then take next pixel of x axis and find the y axis pixel. if it is white then you have reach to the other corner of box. if pixel is not white then consider next x axis pixel. when you are in corner then find the next y axis pixel until you reach the corner. when you are in third corner consider the previous x pixel. then find previous x pixel until you reach the fourth corner. then you can save the box by pixel coordinates of four corners. I think this is more accurate. But it is time consuming and complex. but it may be new algorithm. (this is only valid if boxes are direct lines)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.