Открыть CV разрезать изображение на более мелкие изображения на основе полей формы и контуров python - PullRequest
0 голосов
/ 27 апреля 2020

У меня есть такое изображение в формате PDF

enter image description here

Как мне вырезать изображение на основе контуров каждого поля формы, например отдельное изображение и сохранить их как .png Я хочу JPG для всех полей формы.

enter image description here

Я написал код, подобный этому, используя сопоставление объектов в открытом резюме, но линии и контуры не верны. Любая помощь в этом будет принята с благодарностью.

Мой код

import cv2
import sys
import pytesseract
import numpy as np
from pdf2image import convert_from_path 

# Read the main image 
img_rgb = convert_from_path("/mnt/g/waterlabs/images/ocr/client333.pdf", 500, fmt='jpeg', grayscale=True)[0]
img_rgb = np.array(img_rgb)

# Convert it to grayscale 
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY) 

# Read the template 
image = cv2.imread('/mnt/g/waterlabs/images/ocr/client333.jpg',0) 
template = cv2.imread('/mnt/g/waterlabs/images/ocr/client333-original.jpg',0) 

# Initiate ORB detector
orb = cv2.ORB_create()
# find the keypoints and descriptors with ORB
kp1, des1 = orb.detectAndCompute(image,None)
kp2, des2 = orb.detectAndCompute(template,None)

# create BFMatcher object
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
# Match descriptors.
matches = bf.match(des1,des2)

print(matches)

# Draw a rectangle around the matched region. 
for pt in zip(*loc[::-1]): 
    cv2.rectangle(image, pt, (pt[0] + w, pt[1] + h), (0,255,255), 2) 

# Show the final image with the matched area. 
cv2.imwrite( "/mnt/g/waterlabs/images/ocr/client333-detected.jpg", image);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...