Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add image crop transform #78

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion match_two.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ def match_two(model, device, config, im_one, im_two, plot_save_path):

model.eval()

it = input_transform((int(config['feature_extract']['imageresizeH']), int(config['feature_extract']['imageresizeW'])))
crop_roi = None
if 'imagecrop' in config['feature_extract']:
crop_roi = tuple([int(x) for x in config['feature_extract']['imagecrop'].split(",")])
it = input_transform((int(config['feature_extract']['imageresizeH']), int(config['feature_extract']['imageresizeW'])), crop_roi)

im_one_pil = Image.fromarray(cv2.cvtColor(im_one, cv2.COLOR_BGR2RGB))
im_two_pil = Image.fromarray(cv2.cvtColor(im_two, cv2.COLOR_BGR2RGB))
Expand Down Expand Up @@ -148,6 +151,10 @@ def match_two(model, device, config, im_one, im_two, plot_save_path):
tqdm.write('====> Plotting Local Features and save them to ' + str(join(plot_save_path, 'patchMatchings.png')))

# using cv2 for their in-built keypoint correspondence plotting tools
if crop_roi is not None:
top, left, bottom, right = crop_roi
im_one = im_one[top:bottom,left:right]
im_two = im_two[top:bottom,left:right]
cv_im_one = cv2.resize(im_one, (int(config['feature_extract']['imageresizeW']), int(config['feature_extract']['imageresizeH'])))
cv_im_two = cv2.resize(im_two, (int(config['feature_extract']['imageresizeW']), int(config['feature_extract']['imageresizeH'])))
# cv2 resize slightly different from torch, but for visualisation only not a big problem
Expand Down
31 changes: 17 additions & 14 deletions patchnetvlad/tools/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import os

import torchvision.transforms as transforms
import torchvision.transforms.functional as TF
import torch.utils.data as data

import numpy as np
Expand All @@ -36,20 +37,19 @@
from patchnetvlad.tools import PATCHNETVLAD_ROOT_DIR


def input_transform(resize=(480, 640)):
def input_transform(resize=(480, 640), crop_roi=None):
trans = []
if crop_roi is not None and len(crop_roi) == 4 and all([x >= 0 for x in crop_roi]):
top, left, bottom, right = crop_roi
trans.append(transforms.Lambda(lambda x: TF.crop(x, top, left, bottom-top, right-left)))
if resize[0] > 0 and resize[1] > 0:
return transforms.Compose([
transforms.Resize(resize),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]),
])
else:
return transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]),
])
trans.append(transforms.Resize(resize))
trans.extend([
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]),
])
return transforms.Compose(trans)


class PlaceDataset(data.Dataset):
Expand Down Expand Up @@ -80,8 +80,11 @@ def __init__(self, query_file_path, index_file_path, dataset_root_dir, ground_tr
self.positives = None
self.distances = None

crop_roi = None
if 'imagecrop' in config:
crop_roi = tuple([int(x) for x in config['imagecrop'].split(',')])
self.resize = (int(config['imageresizeH']), int(config['imageresizeW']))
self.mytransform = input_transform(self.resize)
self.mytransform = input_transform(self.resize, crop_roi)

def __getitem__(self, index):
img = Image.open(self.images[index])
Expand Down