Advertisement
VladSmirN

карта изображений

Sep 30th, 2021
966
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.17 KB | None | 0 0
  1. import math
  2. from matplotlib import pyplot as plt, patches
  3.  
  4. def image_map(filenames, projections, img_shape = (32,32), mask = np.array([]), save_path = "map.png"):
  5.  
  6.     """Выводит карту с изображениями
  7.    :param filenames: numpy массив с путями к изображениям
  8.    :param projections: numpy массив c координатами
  9.    :param img_shape:  tuple с размером изображения на карте
  10.    :param mask: numpy массив c значениями True/False , при True обводит
  11.     красной рамкой соответствующую картинку  
  12.    :param save_path: путь для сохранения карты
  13.    """
  14.     x = []
  15.     y = []
  16.     for pair in projections:
  17.         x.append(pair[0])
  18.         y.append(pair[1])
  19.     x.sort()
  20.     y.sort()
  21.     max_x = math.ceil(x[-1])
  22.     max_y = math.ceil(y[-1])
  23.     min_x = math.floor(x[0])
  24.     min_y = math.floor(y[0])
  25.  
  26.     full_size = filenames.shape[0]  
  27.     img = np.zeros((full_size, full_size, 3), np.uint8)
  28.    
  29.     max_diff = np.max([max_x - min_x, max_y - min_y])
  30.     coef = int(full_size / max_diff)
  31.  
  32.     for i, coord in enumerate(projections):
  33.  
  34.             filePath = filenames[i]
  35.             image = cv2.imread(filePath)
  36.             image = cv2.resize(image,img_shape)
  37.             image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  38.  
  39.             coord_true = (int(coef * (coord[0] - min_x)), int(coef * (coord[1] - min_y)))
  40.              
  41.             crop_im = img[coord_true[0]:coord_true[0] + img_shape[1], coord_true[1]:coord_true[1] + img_shape[0]]
  42.          
  43.             img[coord_true[0]:coord_true[0] + img_shape[1], coord_true[1]:coord_true[1] + img_shape[0]] = \
  44.                 cv2.addWeighted(image, 0.7, crop_im, 0.3, 0)
  45.  
  46.             if mask.shape[0] > 0 and mask[i]  :
  47.                  cv2.rectangle(img, (coord_true[1], coord_true[0]), (coord_true[1] + img_shape[1], coord_true[0] + img_shape[0]),
  48.                            (255, 0, 0), 5)
  49.      
  50.     plt.imshow(img)
  51.     cv2.imwrite(save_path, img[:, :, ::-1])
  52.     plt.show()
  53.  
  54.  
  55. image_map(df.filename.to_numpy(), projections_test)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement