Advertisement
Guest User

Untitled

a guest
Jul 20th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.45 KB | None | 0 0
  1. from core.OperationBase import OperationBase
  2. import random
  3. import cv2
  4. import numpy as np
  5.  
  6.  
  7. class Folding(OperationBase):
  8.  
  9.     def __init__(self, image):
  10.         OperationBase.__init__(self, image)
  11.         self.no_folding = 0
  12.         self.slices = []
  13.         self.sections = []
  14.         self.moves = []
  15.         self.src_points = []
  16.         self.moved_points = []
  17.         self.max_width = 0
  18.  
  19.     def do_operation(self, kwargs):
  20.         if 'folding_no' in kwargs and 'min_move' in kwargs and 'max_move' in kwargs:
  21.             self.no_folding = kwargs['folding_no']
  22.             self.generate_random_sections()
  23.             self.get_slices()
  24.             self.get_moves(kwargs['min_move'], kwargs['max_move'])
  25.             self.generate_points()
  26.             self.move_points()
  27.         return self.apply_folding()
  28.  
  29.     def generate_random_sections(self):
  30.         self.sections = []
  31.         start = self.rows / 10
  32.         end = self.rows / self.no_folding
  33.  
  34.         for i in range(self.no_folding):
  35.             section = random.randint(start + start / 10, end - end / 10)
  36.             start = section
  37.             end += self.rows / self.no_folding
  38.             self.sections.append(section)
  39.  
  40.     def get_slices(self):
  41.         start = 0
  42.         for i in self.sections:
  43.             end = i
  44.             self.slices.append(self.image[start:end, :])
  45.             start = end
  46.         self.slices.append(self.image[start:self.rows, :])
  47.  
  48.     def get_moves(self, min_move, max_move):
  49.         self.max_width = int(self.cols + self.cols * (max_move / 100.0))
  50.         self.moves = []
  51.         for i in range(len(self.slices)):
  52.             self.moves.append(random.randint(min_move, max_move) / 100.0)
  53.  
  54.     def generate_points(self):
  55.         self.src_points = []
  56.         for i in range(len(self.slices)):
  57.             self.src_points.append([[0, 0], [0, self.slices[i].shape[0]], [self.cols, self.slices[i].shape[0]]])
  58.  
  59.     def move_points(self):
  60.         self.moved_points = self.src_points[:]
  61.         if self.no_folding == 1:
  62.             self.moved_points[0] = [[self.cols * self.moves[0], 0], [0, self.slices[0].shape[0]],
  63.                                     [self.cols, self.slices[0].shape[0]]]
  64.             self.moved_points[1] = [[0, 0], [self.cols * self.moves[1], self.slices[1].shape[0]],
  65.                                     [self.cols + self.cols * self.moves[1], self.slices[1].shape[0]]]
  66.         elif self.no_folding == 2:
  67.             self.moved_points[0] = [[self.cols * self.moves[0], 0], [0, self.slices[0].shape[0]],
  68.                                     [self.cols, self.slices[0].shape[0]]]
  69.             self.moved_points[1] = [[0, 0], [self.cols * self.moves[1], self.slices[1].shape[0]],
  70.                                     [self.cols + self.cols * self.moves[1], self.slices[1].shape[0]]]
  71.             self.moved_points[2] = [[self.cols * self.moves[1], 0], [self.cols * self.moves[2], self.slices[2].shape[0]],
  72.                                     [self.cols + self.cols * self.moves[2], self.slices[2].shape[0]]]
  73.         elif self.no_folding == 3:
  74.             self.moved_points[0] = [[self.cols * self.moves[0], 0], [0, self.slices[0].shape[0]],
  75.                                     [self.cols, self.slices[0].shape[0]]]
  76.             self.moved_points[1] = [[0, 0], [self.cols * self.moves[1], self.slices[1].shape[0]],
  77.                                     [self.cols + self.cols * self.moves[1], self.slices[1].shape[0]]]
  78.             self.moved_points[2] = [[self.cols * self.moves[1], 0], [self.cols * self.moves[2], self.slices[2].shape[0]],
  79.                                     [self.cols + self.cols * self.moves[2], self.slices[2].shape[0]]]
  80.             self.moved_points[3] = [[self.cols * self.moves[2], 0], [self.cols * self.moves[3], self.slices[3].shape[0]],
  81.                                     [self.cols + self.cols * self.moves[3], self.slices[3].shape[0]]]
  82.  
  83.     def apply_folding(self):
  84.         images_finale = []
  85.         for i in range(len(self.slices)):
  86.             aux = cv2.getAffineTransform(np.float32(self.src_points[i]), np.float32(self.moved_points[i]))
  87.             #FLAGS=CV2.INTER_NEAREST FOR RECTS if needed
  88.             images_finale.append(cv2.warpAffine(self.slices[i], aux, (self.max_width, self.slices[i].shape[0]),
  89.                                                 flags=cv2.INTER_NEAREST))
  90.  
  91.         image = images_finale[0]
  92.         for i in range(1, len(images_finale), 1):
  93.             image = np.vstack((image, images_finale[i]))
  94.         return image
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement