MCPcopy Create free account
hub / github.com/ActiveVisionLab/DFNet / sample_homography_np

Function sample_homography_np

script/utils/utils.py:125–239  ·  view source on GitHub ↗

Sample a random valid homography. Computes the homography transformation between a random patch in the original image and a warped projection with the same image size. As in `tf.contrib.image.transform`, it maps the output point (warped patch) to a transformed input point (original

(
        shape, shift=0, perspective=True, scaling=True, rotation=True, translation=True,
        n_scales=5, n_angles=25, scaling_amplitude=0.1, perspective_amplitude_x=0.1,
        perspective_amplitude_y=0.1, patch_ratio=0.5, max_angle=pi/2,
        allow_artifacts=False, translation_overflow=0.)

Source from the content-addressed store, hash-verified

123 save_image_saliancy(features[None,...].permute(1,0,2,3).cpu(), fn, normalize=True)
124
125def sample_homography_np(
126 shape, shift=0, perspective=True, scaling=True, rotation=True, translation=True,
127 n_scales=5, n_angles=25, scaling_amplitude=0.1, perspective_amplitude_x=0.1,
128 perspective_amplitude_y=0.1, patch_ratio=0.5, max_angle=pi/2,
129 allow_artifacts=False, translation_overflow=0.):
130 """Sample a random valid homography.
131
132 Computes the homography transformation between a random patch in the original image
133 and a warped projection with the same image size.
134 As in `tf.contrib.image.transform`, it maps the output point (warped patch) to a
135 transformed input point (original patch).
136 The original patch, which is initialized with a simple half-size centered crop, is
137 iteratively projected, scaled, rotated and translated.
138
139 Arguments:
140 shape: A rank-2 `Tensor` specifying the height and width of the original image.
141 perspective: A boolean that enables the perspective and affine transformations.
142 scaling: A boolean that enables the random scaling of the patch.
143 rotation: A boolean that enables the random rotation of the patch.
144 translation: A boolean that enables the random translation of the patch.
145 n_scales: The number of tentative scales that are sampled when scaling.
146 n_angles: The number of tentatives angles that are sampled when rotating.
147 scaling_amplitude: Controls the amount of scale.
148 perspective_amplitude_x: Controls the perspective effect in x direction.
149 perspective_amplitude_y: Controls the perspective effect in y direction.
150 patch_ratio: Controls the size of the patches used to create the homography. (like crop size)
151 max_angle: Maximum angle used in rotations.
152 allow_artifacts: A boolean that enables artifacts when applying the homography.
153 translation_overflow: Amount of border artifacts caused by translation.
154
155 Returns:
156 A `Tensor` of shape `[1, 8]` corresponding to the flattened homography transform.
157 """
158
159 # print("debugging")
160
161
162 # Corners of the output image
163 pts1 = np.stack([[0., 0.], [0., 1.], [1., 1.], [1., 0.]], axis=0)
164 # Corners of the input patch
165 margin = (1 - patch_ratio) / 2
166 pts2 = margin + np.array([[0, 0], [0, patch_ratio],
167 [patch_ratio, patch_ratio], [patch_ratio, 0]])
168
169 from numpy.random import normal
170 from numpy.random import uniform
171 from scipy.stats import truncnorm
172
173 # Random perspective and affine perturbations
174 # lower, upper = 0, 2
175 std_trunc = 2
176 # pdb.set_trace()
177 if perspective:
178 if not allow_artifacts:
179 perspective_amplitude_x = min(perspective_amplitude_x, margin)
180 perspective_amplitude_y = min(perspective_amplitude_y, margin)
181 perspective_displacement = truncnorm(-1*std_trunc, std_trunc, loc=0, scale=perspective_amplitude_y/2).rvs(1)
182 h_displacement_left = truncnorm(-1*std_trunc, std_trunc, loc=0, scale=perspective_amplitude_x/2).rvs(1)

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected