Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- from matplotlib import pyplot as plt, patches
- def image_map(filenames, projections, img_shape = (32,32), mask = np.array([]), save_path = "map.png"):
- """Выводит карту с изображениями
- :param filenames: numpy массив с путями к изображениям
- :param projections: numpy массив c координатами
- :param img_shape: tuple с размером изображения на карте
- :param mask: numpy массив c значениями True/False , при True обводит
- красной рамкой соответствующую картинку
- :param save_path: путь для сохранения карты
- """
- x = []
- y = []
- for pair in projections:
- x.append(pair[0])
- y.append(pair[1])
- x.sort()
- y.sort()
- max_x = math.ceil(x[-1])
- max_y = math.ceil(y[-1])
- min_x = math.floor(x[0])
- min_y = math.floor(y[0])
- full_size = filenames.shape[0]
- img = np.zeros((full_size, full_size, 3), np.uint8)
- max_diff = np.max([max_x - min_x, max_y - min_y])
- coef = int(full_size / max_diff)
- for i, coord in enumerate(projections):
- filePath = filenames[i]
- image = cv2.imread(filePath)
- image = cv2.resize(image,img_shape)
- image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
- coord_true = (int(coef * (coord[0] - min_x)), int(coef * (coord[1] - min_y)))
- crop_im = img[coord_true[0]:coord_true[0] + img_shape[1], coord_true[1]:coord_true[1] + img_shape[0]]
- img[coord_true[0]:coord_true[0] + img_shape[1], coord_true[1]:coord_true[1] + img_shape[0]] = \
- cv2.addWeighted(image, 0.7, crop_im, 0.3, 0)
- if mask.shape[0] > 0 and mask[i] :
- cv2.rectangle(img, (coord_true[1], coord_true[0]), (coord_true[1] + img_shape[1], coord_true[0] + img_shape[0]),
- (255, 0, 0), 5)
- plt.imshow(img)
- cv2.imwrite(save_path, img[:, :, ::-1])
- plt.show()
- image_map(df.filename.to_numpy(), projections_test)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement