-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathAspectAwareProcessor.py
More file actions
36 lines (30 loc) · 1.06 KB
/
AspectAwareProcessor.py
File metadata and controls
36 lines (30 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# -*- coding: utf-8 -*-
"""
Created on Mon Jan 7 17:10:06 2019
@author: DELL
"""
import imutils
import cv2
class AspectAwareProcessor:
def __init__(self, width, height, inter = cv2.INTER_AREA):
self.width = width
self.height = height
self.inter = inter
# Resize image keeping the ratio
def preprocess(self, image):
(h, w) = image.shape[:2]
dW = 0
dH = 0
if h > w:
image = imutils.resize(image, width=self.width)
dH = int((image.shape[0] - self.height)/2.)
else:
image = imutils.resize(image, height=self.height)
dW = int((image.shape[1] - self.width)/2.)
if(dH < 0):
image = cv2.copyMakeBorder(image, -dH, -dH, dW, dW, cv2.BORDER_REPLICATE)
elif dW < 0:
image = cv2.copyMakeBorder(image, dH, dH, -dW, -dW, cv2.BORDER_REPLICATE)
else:
image = image[dH:dH+self.height, dW:dW+self.width]
return cv2.resize(image, (self.width, self.height), interpolation = self.inter)