AyKarsi
AyKarsi

Reputation: 9675

Remove circles from technical drawing image

When OCRing technical drawing, most (all?) ocr engines have problems with surrounding geometry and sometimes falsely interpret a line as letter.

To improve the quality of the OCR, I first want to remove certain elements from the drawing, mainly circles and rectangels, from the image.

The drawings are all black & white and look very similar to the below example.

What is the best way to achieve this? I've played around with image magick and opencv with little success...

tech drawing

Upvotes: 3

Views: 911

Answers (1)

nathancy
nathancy

Reputation: 46600

Here's a partial solution. This problem can be broken up into two steps:

1) Remove rectangles by removing horizontal + vertical lines

We create vertical and horizontal kernels then perform morph close to detect the lines. From here we use bitwise operations to remove the lines.

Detected vertical lines (left) and horizontal lines (right)

Removed lines

import cv2

image = cv2.imread('1.jpg')

vertical_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1,15))
remove_vertical = 255 - cv2.morphologyEx(image, cv2.MORPH_CLOSE, vertical_kernel)

horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (15,1))
remove_horizontal = 255 - cv2.morphologyEx(image, cv2.MORPH_CLOSE, horizontal_kernel)

result = cv2.add(cv2.add(remove_vertical, remove_horizontal), image)

cv2.imshow('result', result)
cv2.waitKey()

2) Detect/remove circles

There are several approaches to remove the circles

  1. Use cv2.HoughCircles(). Here's a good tutorial to detect circles in images using Hough Circles
  2. Construct a cv2.MORPH_ELLIPSE kernel using cv2.getStructuringElement() then perform morphological operations to isolate the circle contours
  3. Use simple shape detection with contour approximation and contour filtering to detect the circles. This method uses cv2.arcLength() and cv2.approxPolyDP() for contour approximation. One tradeoff with this method is that it only works with "perfect" shapes. Take a look at detect simple geometric shapes and opencv shape detection

Upvotes: 2

Related Questions