s900n
s900n

Reputation: 3375

Python Opencv: Finding boxes in interrupted picture

I want to find rectangles in the figures.

I can find them when outside of the boxes are not interrupted with writings with the code below. However when the box is interrupted with handwriting I cannot find it, that is the part that I need help.

The code:

import cv2
import numpy as np
import matplotlib.pyplot as pp

image = cv2.imread('original_image.jpeg')

blur = cv2.pyrMeanShiftFiltering(image, 11, 21)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    peri = cv2.arcLength(c, True)
    approx = cv2.approxPolyDP(c, 0.015 * peri, True)
    if len(approx) == 4:
        print(approx)
        x,y,w,h = cv2.boundingRect(approx)
        if(w > 10 and h > 10):
            print(x,y,w,h)
            cv2.rectangle(image,(x,y),(x+w,y+h),(36,255,12),2)

cv2.imshow('thresh', thresh)
cv2.waitKey()
cv2.imshow('image', image)

Original Image:

Original IMage

Interrupted Image with handwriting:

Interrupted IMage

Boxes found on original image:

Result of the code with original picture

How can I find the box in the interrupted picture?

Upvotes: 3

Views: 210

Answers (1)

Sreekiran A R
Sreekiran A R

Reputation: 3421

You can use my answer which is a solution to find all the checkboxes from a given form to get the boxes. https://stackoverflow.com/a/62801071/9605907 The steps are basically like this.

  1. binarise the image
  2. find horizontal lines
  3. find vertical lines
  4. combine them
  5. get Connected components using cv2.connectedComponentsWithStats

I tried running the code with your input image and this was the result I got,

Output binary image:

binary image

Output after doing connected component analysis:

output image

You can get the code from this link.

Upvotes: 3

Related Questions