def affine_skew(self, tilt, phi, img, mask=None):
h, w = img.shape[:2]
if mask is None:
mask = np.zeros((h, w), np.uint8)
mask[:] = 255
A = np.float32([[1, 0, 0], [0, 1, 0]])
if phi != 0.0:
phi = np.deg2rad(phi)
s, c = np.sin(phi), np.cos(phi)
A = np.float32([[c, -s], [s, c]])
corners = [[0, 0], [w, 0], [w, h], [0, h]]
tcorners = np.int32(np.dot(corners, A.T))
x, y, w, h = cv2.boundingRect(tcorners.reshape(1, -1, 2))
A = np.hstack([A, [[-x], [-y]]])
img = cv2.warpAffine(img, A, (w, h), flags=cv2.INTER_LINEAR, borderMode=cv2.BORDER_REPLICATE)
if tilt != 1.0:
s = 0.8*np.sqrt(tilt * tilt - 1)
img = cv2.GaussianBlur(img, (0, 0), sigmaX=s, sigmaY=0.01)
img = cv2.resize(img, (0, 0), fx=1.0 / tilt, fy=1.0, interpolation=cv2.INTER_NEAREST)
A[0] /= tilt
if phi != 0.0 or tilt != 1.0:
h, w = img.shape[:2]
mask = cv2.warpAffine(mask, A, (w, h), flags=cv2.INTER_NEAREST)
Ai = cv2.invertAffineTransform(A)
return img, mask, Ai
python类INTER_NEAREST的实例源码
def filter_image(image, canny1=5, canny2=5, show=False):
# compute the ratio of the old height to the new height, and resize it
image = imutils.resize(image, height=scale_factor, interpolation=cv2.INTER_NEAREST)
# convert the image to grayscale, blur it, and find edges in the image
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
edged = cv2.Canny(blurred, canny1, canny2)
# show the image(s)
if show:
cv2.imshow("Edged", edged)
cv2.waitKey(0)
cv2.destroyAllWindows()
return edged
def filter_image(image, canny1=10, canny2=10, show=False):
# compute the ratio of the old height to the new height, and resize it
image = imutils.resize(image, height=scale_factor, interpolation=cv2.INTER_NEAREST)
# convert the image to grayscale, blur it, and find edges in the image
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (5, 5), 0)
edged = cv2.Canny(gray, canny1, canny2)
# show the image(s)
if show:
cv2.imshow("Edged", edged)
cv2.waitKey(0)
cv2.destroyAllWindows()
return edged
def resizeCrop(self, crop, sz):
"""
Resize cropped image
:param crop: crop
:param sz: size
:return: resized image
"""
if self.resizeMethod == self.RESIZE_CV2_NN:
rz = cv2.resize(crop, sz, interpolation=cv2.INTER_NEAREST)
elif self.resizeMethod == self.RESIZE_BILINEAR:
rz = self.bilinearResize(crop, sz, self.getNDValue())
elif self.resizeMethod == self.RESIZE_CV2_LINEAR:
rz = cv2.resize(crop, sz, interpolation=cv2.INTER_LINEAR)
else:
raise NotImplementedError("Unknown resize method!")
return rz
def create_heatmaps(img, pred):
"""
Uses objectness probability to draw a heatmap on the image and returns it
"""
# find anchors with highest prediction
best_pred = np.max(pred[..., 0], axis=-1)
# convert probabilities to colormap scale
best_pred = np.uint8(best_pred * 255)
# apply color map
# cv2 colormaps create BGR, not RGB
cmap = cv2.cvtColor(cv2.applyColorMap(best_pred, cv2.COLORMAP_JET), cv2.COLOR_BGR2RGB)
# resize the color map to fit image
cmap = cv2.resize(cmap, img.shape[1::-1], interpolation=cv2.INTER_NEAREST)
# overlay cmap with image
return cv2.addWeighted(cmap, 1, img, 0.5, 0)
def draw_seg(self, img, seg_gt, segmentation, name):
"""Applies generated segmentation mask to an image"""
palette = np.load('Extra/palette.npy').tolist()
img_size = (img.shape[1], img.shape[0])
segmentation = cv2.resize(segmentation, dsize=img_size,
interpolation=cv2.INTER_NEAREST)
image = Image.fromarray((img * 255).astype('uint8'))
segmentation_draw = Image.fromarray((segmentation).astype('uint8'), 'P')
segmentation_draw.putpalette(palette)
segmentation_draw.save(self.directory + '/%s_segmentation.png' % name, 'PNG')
image.save(self.directory + '/%s.jpg' % name, 'JPEG')
if seg_gt:
seg_gt_draw = Image.fromarray((seg_gt).astype('uint8'), 'P')
seg_gt_draw.putpalette(palette)
seg_gt_draw.save(self.directory + '/%s_seg_gt.png' % name, 'PNG')
def reset(self, mode):
if mode == MyEnv.VALIDATION_MODE:
if self._mode != MyEnv.VALIDATION_MODE:
self._mode = MyEnv.VALIDATION_MODE
self._mode_score = 0.0
self._mode_episode_count = 0
else:
self._mode_episode_count += 1
elif self._mode != -1: # and thus mode == -1
self._mode = -1
self._ple.reset_game()
for _ in range(self._random_state.randint(15)):
self._ple.act(self._ple.NOOP)
self._screen = self._ple.getScreenGrayscale()
cv2.resize(self._screen, (48, 48), self._reduced_screen, interpolation=cv2.INTER_NEAREST)
return [2 * [48 * [48 * [0]]]]
def reset(self, mode):
if mode == MyEnv.VALIDATION_MODE:
if self._mode != MyEnv.VALIDATION_MODE:
self._mode = MyEnv.VALIDATION_MODE
self._mode_score = 0.0
self._mode_episode_count = 0
else:
self._mode_episode_count += 1
elif self._mode != -1: # and thus mode == -1
self._mode = -1
self._ale.reset_game()
for _ in range(self._random_state.randint(15)):
self._ale.act(0)
self._ale.getScreenGrayscale(self._screen)
cv2.resize(self._screen, (84, 84), self._reduced_screen, interpolation=cv2.INTER_NEAREST)
return [4 * [84 * [84 * [0]]]]
def to_image(self):
"""Converts map to a viewable image.
Returns:
OpenCV image.
"""
height, width = self._map.shape
image = np.zeros((height, width, 3), dtype=np.uint8)
for i in range(width):
for j in range(height):
classification = self._map[j, i]
image[j, i] = GameMapObjects.to_color(classification)
upscaled_image = cv2.resize(image, (100, 100),
interpolation=cv2.INTER_NEAREST)
return upscaled_image
def resizeCrop(self, crop, sz):
"""
Resize cropped image
:param crop: crop
:param sz: size
:return: resized image
"""
if self.resizeMethod == self.RESIZE_CV2_NN:
rz = cv2.resize(crop, sz, interpolation=cv2.INTER_NEAREST)
elif self.resizeMethod == self.RESIZE_BILINEAR:
rz = self.bilinearResize(crop, sz, self.getNDValue())
elif self.resizeMethod == self.RESIZE_CV2_LINEAR:
rz = cv2.resize(crop, sz, interpolation=cv2.INTER_LINEAR)
else:
raise NotImplementedError("Unknown resize method!")
return rz
def visualize_weights(net, layer, zoom = 2):
"""
Visualize weights in a fully conencted layer.
:param net: caffe network
:type net: caffe.Net
:param layer: layer name
:type layer: string
:param zoom: the number of pixels (in width and height) per weight
:type zoom: int
:return: image visualizing the kernels in a grid
:rtype: numpy.ndarray
"""
assert layer in get_layers(net), "layer %s not found" % layer
weights = net.params[layer][0].data
weights = (weights - numpy.min(weights))/(numpy.max(weights) - numpy.min(weights))
return cv2.resize(weights, (weights.shape[0]*zoom, weights.shape[1]*zoom), weights, 0, 0, cv2.INTER_NEAREST)
def squeeze(image, width_max, border):
"""
This function squeezes images in the width.
:param image: Numpy array of image
:param width_max: max image width
:param border: border left and right of squeezed image
:return: Squeezed Image
"""
image_wd = image.shape[1]
image_ht = image.shape[0]
basewidth = width_max - border
wpercent = basewidth / image_wd
dim = (int(wpercent*image_wd), image_ht)
img_squeeze = cv.resize(image, dim, interpolation=cv.INTER_NEAREST)
return img_squeeze
def resizeCrop(self, crop, sz):
"""
Resize cropped image
:param crop: crop
:param sz: size
:return: resized image
"""
if self.resizeMethod == self.RESIZE_CV2_NN:
rz = cv2.resize(crop, sz, interpolation=cv2.INTER_NEAREST)
elif self.resizeMethod == self.RESIZE_BILINEAR:
rz = self.bilinearResize(crop, sz, self.getNDValue())
elif self.resizeMethod == self.RESIZE_CV2_LINEAR:
rz = cv2.resize(crop, sz, interpolation=cv2.INTER_LINEAR)
else:
raise NotImplementedError("Unknown resize method!")
return rz
def resize_sample(sample, shape=None, use_interp=True, scale=None):
if (shape and scale) or (not shape and not scale):
raise ValueError('Must specify exactly one of shape or scale, but got shape=\'{}\', scale=\'{}\''.format(shape, scale))
# Use INTER_AREA for shrinking and INTER_LINEAR for enlarging:
interp = cv2.INTER_NEAREST
if use_interp:
target_is_smaller = (shape and shape[1] < sample.shape[1]) or (scale and scale < 1) # targetWidth < sampleWidth
interp = cv2.INTER_AREA if target_is_smaller else cv2.INTER_LINEAR
if shape:
resized = cv2.resize(sample, (shape[1], shape[0]), interpolation=interp)
else:
resized = cv2.resize(sample, None, fx=scale, fy=scale, interpolation=interp)
return resized
def _resize(img, size, interpolation):
img = img.transpose((1, 2, 0))
if interpolation == PIL.Image.NEAREST:
cv_interpolation = cv2.INTER_NEAREST
elif interpolation == PIL.Image.BILINEAR:
cv_interpolation = cv2.INTER_LINEAR
elif interpolation == PIL.Image.BICUBIC:
cv_interpolation = cv2.INTER_CUBIC
elif interpolation == PIL.Image.LANCZOS:
cv_interpolation = cv2.INTER_LANCZOS4
H, W = size
img = cv2.resize(img, dsize=(W, H), interpolation=cv_interpolation)
# If input is a grayscale image, cv2 returns a two-dimentional array.
if len(img.shape) == 2:
img = img[:, :, np.newaxis]
return img.transpose((2, 0, 1))
def renderStarGauss(image, cov, mu, first, scale = 5):
num_circles = 3
num_points = 64
cov = sqrtm(cov)
num = num_circles * num_points
pos = np.ones((num, 2))
for c in range(num_circles):
r = c + 1
for p in range(num_points):
angle = p / num_points * 2 * np.pi
index = c * num_points + p
x = r * np.cos(angle)
y = r * np.sin(angle)
pos[index, 0] = x * cov[0, 0] + y * cov[0, 1] + mu[0]
pos[index, 1] = x * cov[1, 0] + y * cov[1, 1] + mu[1]
#image = image.copy()
#image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)
if first:
image = cv2.resize(image, (0, 0), None, scale, scale, cv2.INTER_NEAREST)
for c in range(num_circles):
pts = np.array(pos[c * num_points:(c + 1) * num_points, :] * scale + scale / 2, np.int32)
pts = pts.reshape((-1,1,2))
cv2.polylines(image, [pts], True, (255, 0, 0))
return image
data.py 文件源码
项目:deep-learning-for-human-part-discovery-in-images
作者: shiba24
项目源码
文件源码
阅读 22
收藏 0
点赞 0
评论 0
def make_mask(self, matfile):
d = sio.loadmat(matfile)
if "image" in matfile:
parts_mask = np.transpose(np.expand_dims(d["M"], 0), (1, 2, 0))
else:
objects = d["anno"][0, 0][1]
object_name = [objects[0, i][0][0] for i in range(objects.shape[1])]
img_shape = objects[0, 0][2].shape
parts_mask = np.zeros(img_shape + (1, ))
for index, obj in enumerate(object_name):
if obj == "person":
if not objects[0, index][3].shape == (0, 0):
for j in range(objects[0, index][3].shape[1]):
parts_mask[:, :, 0] = np.where(parts_mask[:, :, 0] == 0, merged_parts_list[objects[0, index][3][0, j][0][0]] * np.array(objects[0, index][3][0, j][1]), parts_mask[:, :, 0])
parts_mask = cv2.resize(parts_mask.astype(np.uint8), (self.insize, self.insize), interpolation = cv2.INTER_NEAREST)
# parts_mask = (parts_mask > 0).astype(np.uint8)
return parts_mask
def morph(roi):
ratio = min(28. / np.size(roi, 0), 28. / np.size(roi, 1))
roi = cv2.resize(roi, None, fx=ratio, fy=ratio,
interpolation=cv2.INTER_NEAREST)
dx = 28 - np.size(roi, 1)
dy = 28 - np.size(roi, 0)
px = ((int(dx / 2.)), int(np.ceil(dx / 2.)))
py = ((int(dy / 2.)), int(np.ceil(dy / 2.)))
squared = np.pad(roi, (py, px), 'constant', constant_values=0)
return squared
def rectify(self, l, r):
"""
Rectify frames passed as (left, right)
Remapping is done with nearest neighbor for speed.
"""
return [cv2.remap(l, self.undistortion_map[cidx], self.rectification_map[cidx], cv2.INTER_NEAREST)
for cidx in range(len(self.cams))]
def load(self, name):
image_path = os.path.join(self.dataset.image, '%s.jpg' % name)
label_path = os.path.join(self.dataset.layout_image, '%s.png' % name)
img = cv2.imread(image_path)
lbl = cv2.imread(label_path, 0)
img = cv2.resize(img, self.target_size, cv2.INTER_LINEAR)
lbl = cv2.resize(lbl, self.target_size, cv2.INTER_NEAREST)
img = self.transform(img)
lbl = np.clip(lbl, 1, 5) - 1
lbl = torch.from_numpy(np.expand_dims(lbl, axis=0)).long()
return img, lbl
def rescale_patient_images2(images_zyx, target_shape, verbose=False):
if verbose:
print("Target: ", target_shape)
print("Shape: ", images_zyx.shape)
# print "Resizing dim z"
resize_x = 1.0
interpolation = cv2.INTER_NEAREST if False else cv2.INTER_LINEAR
res = cv2.resize(images_zyx, dsize=(target_shape[1], target_shape[0]), interpolation=interpolation) # opencv assumes y, x, channels umpy array, so y = z pfff
# print "Shape is now : ", res.shape
res = res.swapaxes(0, 2)
res = res.swapaxes(0, 1)
# cv2 can handle max 512 channels..
if res.shape[2] > 512:
res = res.swapaxes(0, 2)
res1 = res[:256]
res2 = res[256:]
res1 = res1.swapaxes(0, 2)
res2 = res2.swapaxes(0, 2)
res1 = cv2.resize(res1, dsize=(target_shape[2], target_shape[1]), interpolation=interpolation)
res2 = cv2.resize(res2, dsize=(target_shape[2], target_shape[1]), interpolation=interpolation)
res1 = res1.swapaxes(0, 2)
res2 = res2.swapaxes(0, 2)
res = numpy.vstack([res1, res2])
res = res.swapaxes(0, 2)
else:
res = cv2.resize(res, dsize=(target_shape[2], target_shape[1]), interpolation=interpolation)
res = res.swapaxes(0, 2)
res = res.swapaxes(2, 1)
if verbose:
print("Shape after: ", res.shape)
return res
def __init__(self, dataDir='./datasets/facade/base', data_start = 1, data_end = 300):
self.dataset = []
self.shufflelist = []
for i in range (data_start, data_end + 1):
real_image = cv2.imread(dataDir+"/cmp_b%04d.jpg"%i)
real_image = self.resize(real_image, ipl_alg = cv2.INTER_CUBIC)
input_x_image = cv2.imread(dataDir+"/cmp_b%04d.png"%i)
input_x_image = self.resize(input_x_image, ipl_alg = cv2.INTER_NEAREST)
self.dataset.append((input_x_image, real_image))
self.shufflelist = list(range(self.len()))
def act(self, action):
action = self._actions[action]
reward = 0
for _ in range(self._frame_skip):
reward += self._ple.act(action)
if self.inTerminalState():
break
self._screen = self._ple.getScreenGrayscale()
cv2.resize(self._screen, (48, 48), self._reduced_screen, interpolation=cv2.INTER_NEAREST)
self._mode_score += reward
return np.sign(reward)
def act(self, action):
action = self._actions[action]
reward = 0
for _ in range(self._frame_skip):
reward += self._ale.act(action)
if self.inTerminalState():
break
self._ale.getScreenGrayscale(self._screen)
cv2.resize(self._screen, (84, 84), self._reduced_screen, interpolation=cv2.INTER_NEAREST)
self._mode_score += reward
return np.sign(reward)
baseline_generate_dataset.py 文件源码
项目:DocumentSegmentation
作者: SeguinBe
项目源码
文件源码
阅读 22
收藏 0
点赞 0
评论 0
def save_and_resize(img, filename, nearest=False):
resized = cv2.resize(img, (TARGET_WIDTH, (img.shape[0]*TARGET_WIDTH)//img.shape[1]),
interpolation=cv2.INTER_NEAREST if nearest else None)
imsave(filename, resized)
def save_and_resize(img, filename, nearest=False):
resized = cv2.resize(img, (TARGET_WIDTH, (img.shape[0]*TARGET_WIDTH)//img.shape[1]),
interpolation=cv2.INTER_NEAREST if nearest else cv2.INTER_LINEAR)
imsave(filename, resized)
def to_image(self):
"""Converts map to a viewable image.
Returns:
OpenCV image.
"""
image = np.zeros((self.HEIGHT, self.WIDTH, 3), dtype=np.uint8)
for i in range(self.WIDTH):
for j in range(self.HEIGHT):
classification = self._map[j, i]
image[j, i] = GameMapObjects.to_color(classification)
upscaled_image = cv2.resize(image, (160, 168),
interpolation=cv2.INTER_NEAREST)
return upscaled_image
def get_segmentation_image(segdb, config):
"""
propocess image and return segdb
:param segdb: a list of segdb
:return: list of img as mxnet format
"""
num_images = len(segdb)
assert num_images > 0, 'No images'
processed_ims = []
processed_segdb = []
processed_seg_cls_gt = []
for i in range(num_images):
seg_rec = segdb[i]
assert os.path.exists(seg_rec['image']), '%s does not exist'.format(seg_rec['image'])
im = np.array(cv2.imread(seg_rec['image']))
new_rec = seg_rec.copy()
scale_ind = random.randrange(len(config.SCALES))
target_size = config.SCALES[scale_ind][0]
max_size = config.SCALES[scale_ind][1]
im, im_scale = resize(im, target_size, max_size, stride=config.network.IMAGE_STRIDE)
im_tensor = transform(im, config.network.PIXEL_MEANS)
im_info = [im_tensor.shape[2], im_tensor.shape[3], im_scale]
new_rec['im_info'] = im_info
seg_cls_gt = np.array(Image.open(seg_rec['seg_cls_path']))
seg_cls_gt, seg_cls_gt_scale = resize(
seg_cls_gt, target_size, max_size, stride=config.network.IMAGE_STRIDE, interpolation=cv2.INTER_NEAREST)
seg_cls_gt_tensor = transform_seg_gt(seg_cls_gt)
processed_ims.append(im_tensor)
processed_segdb.append(new_rec)
processed_seg_cls_gt.append(seg_cls_gt_tensor)
return processed_ims, processed_seg_cls_gt, processed_segdb
def _py_evaluate_segmentation(self):
"""
This function is a wrapper to calculte the metrics for given pred_segmentation results
:param pred_segmentations: the pred segmentation result
:return: the evaluation metrics
"""
confusion_matrix = np.zeros((self.num_classes,self.num_classes))
result_dir = os.path.join(self.result_path, 'results', 'VOC' + self.year, 'Segmentation')
for i, index in enumerate(self.image_set_index):
seg_gt_info = self.load_pascal_segmentation_annotation(index)
seg_gt_path = seg_gt_info['seg_cls_path']
seg_gt = np.array(PIL.Image.open(seg_gt_path)).astype('float32')
seg_pred_path = os.path.join(result_dir, '%s.png'%(index))
seg_pred = np.array(PIL.Image.open(seg_pred_path)).astype('float32')
seg_gt = cv2.resize(seg_gt, (seg_pred.shape[1], seg_pred.shape[0]), interpolation=cv2.INTER_NEAREST)
ignore_index = seg_gt != 255
seg_gt = seg_gt[ignore_index]
seg_pred = seg_pred[ignore_index]
confusion_matrix += self.get_confusion_matrix(seg_gt, seg_pred, self.num_classes)
pos = confusion_matrix.sum(1)
res = confusion_matrix.sum(0)
tp = np.diag(confusion_matrix)
IU_array = (tp / np.maximum(1.0, pos + res - tp))
mean_IU = IU_array.mean()
return {'meanIU':mean_IU, 'IU_array':IU_array}
def __call__(self, in_data):
orig_img, bboxes, whole_mask, labels = in_data
_, orig_H, orig_W = orig_img.shape
img = self.model.prepare(
orig_img, self.target_height, self.max_width)
img = img.astype(np.float32)
del orig_img
_, H, W = img.shape
scale = H / orig_H
bboxes = chainercv.transforms.resize_bbox(
bboxes, (orig_H, orig_W), (H, W))
indices = get_keep_indices(bboxes)
bboxes = bboxes[indices, :]
whole_mask = whole_mask[indices, :, :]
labels = labels[indices]
whole_mask = whole_mask.transpose((1, 2, 0))
whole_mask = cv2.resize(
whole_mask.astype(np.uint8), (W, H),
interpolation=cv2.INTER_NEAREST)
if whole_mask.ndim < 3:
whole_mask = whole_mask.reshape((H, W, 1))
whole_mask = whole_mask.transpose((2, 0, 1))
if self.flip:
img, params = chainercv.transforms.random_flip(
img, x_random=True, return_param=True)
whole_mask = fcis.utils.flip_mask(
whole_mask, x_flip=params['x_flip'])
bboxes = chainercv.transforms.flip_bbox(
bboxes, (H, W), x_flip=params['x_flip'])
return img, bboxes, whole_mask, labels, scale