Create line_cor.py
Browse files- line_cor.py +42 -0
line_cor.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
# use this command to install open cv2
|
| 3 |
+
# pip install opencv-python
|
| 4 |
+
|
| 5 |
+
# use this command to install PIL
|
| 6 |
+
# pip install Pillow
|
| 7 |
+
|
| 8 |
+
import cv2
|
| 9 |
+
from PIL import Image
|
| 10 |
+
|
| 11 |
+
def mark_region(imagE_path):
|
| 12 |
+
|
| 13 |
+
im = cv2.imread(image_path)
|
| 14 |
+
|
| 15 |
+
gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
|
| 16 |
+
blur = cv2.GaussianBlur(gray, (9,9), 0)
|
| 17 |
+
thresh = cv2.adaptiveThreshold(blur,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV,11,30)
|
| 18 |
+
|
| 19 |
+
# Dilate to combine adjacent text contours
|
| 20 |
+
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (9,9))
|
| 21 |
+
dilate = cv2.dilate(thresh, kernel, iterations=4)
|
| 22 |
+
|
| 23 |
+
# Find contours, highlight text areas, and extract ROIs
|
| 24 |
+
cnts = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 25 |
+
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
|
| 26 |
+
|
| 27 |
+
line_items_coordinates = []
|
| 28 |
+
for c in cnts:
|
| 29 |
+
area = cv2.contourArea(c)
|
| 30 |
+
x,y,w,h = cv2.boundingRect(c)
|
| 31 |
+
|
| 32 |
+
if y >= 600 and x <= 1000:
|
| 33 |
+
if area > 10000:
|
| 34 |
+
image = cv2.rectangle(im, (x,y), (2200, y+h), color=(255,0,255), thickness=3)
|
| 35 |
+
line_items_coordinates.append([(x,y), (2200, y+h)])
|
| 36 |
+
|
| 37 |
+
if y >= 2400 and x<= 2000:
|
| 38 |
+
image = cv2.rectangle(im, (x,y), (2200, y+h), color=(255,0,255), thickness=3)
|
| 39 |
+
line_items_coordinates.append([(x,y), (2200, y+h)])
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
return image, line_items_coordinates
|