JeVoisBase  1.20
JeVois Smart Embedded Machine Vision Toolkit Base Modules
Share this page:
transform.py
Go to the documentation of this file.
1 # This file is part of OpenCV Zoo project.
2 # It is subject to the license terms in the LICENSE file found in the same directory.
3 #
4 # Copyright (C) 2021, Shenzhen Institute of Artificial Intelligence and Robotics for Society, all rights reserved.
5 # Third party copyrights are property of their respective owners.
6 
7 import collections
8 import numpy as numpy
9 import cv2 as cv
10 
11 class Compose:
12  def __init__(self, transforms=[]):
13  self.transforms = transforms
14 
15  def __call__(self, img):
16  for t in self.transforms:
17  img = t(img)
18  return img
19 
20 class Resize:
21  def __init__(self, size, interpolation=cv.INTER_LINEAR):
22  self.size = size
23  self.interpolation = interpolation
24 
25  def __call__(self, img):
26  return cv.resize(img, self.size)
27 
28 class CenterCrop:
29  def __init__(self, size):
30  self.size = size # w, h
31 
32  def __call__(self, img):
33  h, w, _ = img.shape
34  ws = int(w / 2 - self.size[0] / 2)
35  hs = int(h / 2 - self.size[1] / 2)
36  return img[hs:hs+self.size[1], ws:ws+self.size[0], :]
37 
38 class Normalize:
39  def __init__(self, mean=None, std=None):
40  self.mean = mean
41  self.std = std
42 
43  def __call__(self, img):
44  if self.mean is not None:
45  img[:, :, 0] = img[:, :, 0] - self.mean[0]
46  img[:, :, 1] = img[:, :, 1] - self.mean[1]
47  img[:, :, 2] = img[:, :, 2] - self.mean[2]
48  if self.std is not None:
49  img[:, :, 0] = img[:, :, 0] / self.std[0]
50  img[:, :, 1] = img[:, :, 1] / self.std[1]
51  img[:, :, 2] = img[:, :, 2] / self.std[2]
52  return img
53 
55  def __init__(self, ctype):
56  self.ctype = ctype
57 
58  def __call__(self, img):
59  return cv.cvtColor(img, self.ctype)
transform.ColorConvert.__call__
def __call__(self, img)
Definition: transform.py:58
transform.Resize.size
size
Definition: transform.py:22
transform.Resize.interpolation
interpolation
Definition: transform.py:23
demo.int
int
Definition: demo.py:37
transform.ColorConvert
Definition: transform.py:54
transform.Resize
Definition: transform.py:20
transform.Compose
Definition: transform.py:11
transform.Normalize.__call__
def __call__(self, img)
Definition: transform.py:43
transform.Resize.__init__
def __init__(self, size, interpolation=cv.INTER_LINEAR)
Definition: transform.py:21
transform.CenterCrop.size
size
Definition: transform.py:30
transform.ColorConvert.__init__
def __init__(self, ctype)
Definition: transform.py:55
transform.Normalize.mean
mean
Definition: transform.py:40
transform.CenterCrop
Definition: transform.py:28
transform.Normalize.std
std
Definition: transform.py:41
transform.Compose.transforms
transforms
Definition: transform.py:13
transform.CenterCrop.__init__
def __init__(self, size)
Definition: transform.py:29
transform.Compose.__init__
def __init__(self, transforms=[])
Definition: transform.py:12
transform.Resize.__call__
def __call__(self, img)
Definition: transform.py:25
transform.ColorConvert.ctype
ctype
Definition: transform.py:56
transform.CenterCrop.__call__
def __call__(self, img)
Definition: transform.py:32
transform.Normalize.__init__
def __init__(self, mean=None, std=None)
Definition: transform.py:39
transform.Normalize
Definition: transform.py:38
transform.Compose.__call__
def __call__(self, img)
Definition: transform.py:15