You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
35 lines
946 B
Python
35 lines
946 B
Python
![]()
5 years ago
|
import cv2
|
||
|
import logging
|
||
|
|
||
|
d = 1
|
||
|
|
||
|
while True:
|
||
|
try:
|
||
![]()
5 years ago
|
output = ('bounding_box/input%d.jpg'%d)
|
||
![]()
5 years ago
|
|
||
|
# Load image, convert to grayscale, and find edges
|
||
|
image = cv2.imread('rotated/input%d.jpg'%d)
|
||
|
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
||
|
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_OTSU + cv2.THRESH_BINARY)[1]
|
||
|
|
||
|
# Find contour and sort by contour area
|
||
|
cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
||
|
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
|
||
|
cnts = sorted(cnts, key=cv2.contourArea, reverse=True)
|
||
|
|
||
|
# Find bounding box and extract ROI
|
||
|
for c in cnts:
|
||
|
x,y,w,h = cv2.boundingRect(c)
|
||
|
ROI = image[y:y+h, x:x+w]
|
||
|
break
|
||
|
|
||
|
cv2.imwrite(output,ROI)
|
||
|
cv2.waitKey()
|
||
|
|
||
|
d+=1
|
||
|
|
||
|
except:
|
||
|
logging.exception("message")
|
||
|
print("All pages must be ready!")
|
||
|
break
|