image_processing.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 # BSD 3-Clause License
3 
4 # Copyright (c) 2019, Noam C. Golombek
5 # All rights reserved.
6 
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions are met:
9 
10 # 1. Redistributions of source code must retain the above copyright notice, this
11 # list of conditions and the following disclaimer.
12 
13 # 2. Redistributions in binary form must reproduce the above copyright notice,
14 # this list of conditions and the following disclaimer in the documentation
15 # and/or other materials provided with the distribution.
16 
17 # 3. Neither the name of the copyright holder nor the names of its
18 # contributors may be used to endorse or promote products derived from
19 # this software without specific prior written permission.
20 
21 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 
32 
33 
34 import numpy as np
35 import cv2
36 
37 
38 def resize_image(height, width, image, interpolation=None):
39  """A function that resizes a provided picture.
40  Inputs: width and height to resize to
41  image to resize
42  Outputs: input_image_resized"""
43 
44  if interpolation is None:
45  if str(image.dtype).startswith(("int", "bool")):
46  interpolation = cv2.INTER_NEAREST
47  else:
48  interpolation = cv2.INTER_LINEAR # default
49 
50  image_resized = cv2.resize(image, dsize=(width, height), interpolation=interpolation)
51  return image_resized
52 
53 
54 class ResizeAndCrop(object):
55  """Resize And Crop to process and back"""
56 
57  def __init__(self, hypes, original_image_size):
58  """A function that provides the indices to start and stop cropping the picture at.
59  Inputs: hypes file to get crop parameters,
60  original_image_size
61  Define: crop_y_from, crop_y_to, crop_x_from, crop_x_to, processing_image_size"""
62 
63  def _get(h, field):
64  """ Get field from h if such present, else return False"""
65  if field in h.keys():
66  return h[field]
67  return False
68 
69  if 'jitter' in hypes.keys():
70  h_ = hypes['jitter']
71  else:
72  h_ = hypes
73 
74  # ------------- resize_image -----------------------
75  self.resize_image = _get(h_, 'reseize_image') or _get(h_, 'resize_image')
76  if self.resize_image:
77  inter_image_size = (h_['image_width'], h_['image_height'])
78  else:
79  inter_image_size = original_image_size[:2] # float
80 
81  self.inter_image_size = inter_image_size
82 
83  # ------------- crop_for_processing -----------------------
84  self.crop_for_processing = _get(h_, 'crop_for_processing')
85  if self.crop_for_processing:
86  if 'crop_x_from' in h_.keys():
87  self.crop_x_from = int(inter_image_size[0] * h_['crop_x_from'])
88  else:
89  self.crop_x_from = int(0)
90 
91  if 'crop_x_to' in hypes['jitter'].keys():
92  self.crop_x_to = int(inter_image_size[0] * h_['crop_x_to'])
93  else:
94  self.crop_x_to = int(inter_image_size[0])
95 
96  if 'crop_y_from' in h_.keys():
97  self.crop_y_from = int(inter_image_size[1] * h_['crop_y_from'])
98  else:
99  self.crop_y_from = int(0)
100 
101  if 'crop_y_to' in h_.keys():
102  self.crop_y_to = int(inter_image_size[1] * h_['crop_y_to'])
103  else:
104  self.crop_y_to = int(inter_image_size[1])
105 
107  self.crop_x_to - self.crop_x_from, self.crop_y_to - self.crop_y_from)
108 
109  else:
110  self.processing_image_size = inter_image_size
111 
112  def preprocess_image(self, image, image_uncropped=None):
113  """A function that does all of the image preprocessing
114  Inputs: image to process
115  image_uncropped empty image for postprocessing (allocated if is None)
116  Outputs: preprocessed image, image_uncropped"""
117 
118  preprocessed_image = image
119 
120  # Resize the image
121  if self.resize_image:
122  #self.inter_image_size = (h_['image_width'], h_['image_height'])
123  preprocessed_image = resize_image(self.inter_image_size[1], # -> image_height
124  self.inter_image_size[0], # -> image_width
125  image)
126 
127  # Crop the image
128  if self.crop_for_processing:
129  if image_uncropped is None:
130  image_uncropped = np.zeros(
131  (preprocessed_image.shape[0], preprocessed_image.shape[1]))
132 
133  preprocessed_image = preprocessed_image[self.crop_y_from:self.crop_y_to, self.crop_x_from:self.crop_x_to]
134 
135  return preprocessed_image, image_uncropped
136 
137  def postprocess_image(self, image,
138  output_image_uncropped,
139  resulting_image_for_shape, # image shape to resize back, only shape is used
140  filter_data=None):
141  """A function that does all of the image preprocessing for KittiSeg
142  Inputs: image to process
143  output_image_uncropped empty image for postprocessing
144  Outputs: way_prediction"""
145 
146  #Insert the cropped image into the full sized image
147  if self.crop_for_processing:
148  output_image_uncropped[self.crop_y_from:self.crop_y_to, self.crop_x_from:self.crop_x_to] = image
149  image = output_image_uncropped
150 
151  #Resize the image to its original size
152  if self.resize_image:
153  image = resize_image(resulting_image_for_shape.shape[0], resulting_image_for_shape.shape[1], image)
154 
155  # Accept all pixel with conf >= threshold as positive prediction
156  # This creates a `hard` prediction result for class street
157  if str(image.dtype).startswith("float"):
158  if filter_data is None:
159  filter_data = 0.5
160  way_prediction = image > filter_data
161  elif str(image.dtype).startswith("int"):
162  way_prediction = image.copy()
163  elif str(image.dtype).startswith("bool"):
164  way_prediction = image.copy()
165  else:
166  print(image.dtype)
167  assert str(image.dtype).startswith(("float", "int", "bool"))
168  return way_prediction
def resize_image(height, width, image, interpolation=None)
def __init__(self, hypes, original_image_size)
def preprocess_image(self, image, image_uncropped=None)
def postprocess_image(self, image, output_image_uncropped, resulting_image_for_shape, filter_data=None)


cnn_bridge
Author(s): Noam C. Golombek , Alexander Beringolts
autogenerated on Mon Jun 10 2019 12:53:26