Skip to content

Instantly share code, notes, and snippets.

@jennyfolkesson
Last active August 30, 2016 20:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jennyfolkesson/8370a4ab5c930fff21f0002e682a5fac to your computer and use it in GitHub Desktop.
Save jennyfolkesson/8370a4ab5c930fff21f0002e682a5fac to your computer and use it in GitHub Desktop.
import cv2
def downsample(im):
return im[::2, ::2, :]
def smoothIm(im, kernelSize=5):
return cv2.GaussianBlur(im, (kernelSize, kernelSize), 0)
def constantPadIm(im, rightPad=True, bottomPad=True, destSize=256, constantVal=0):
"""
For the edge cases where a downsampled tile is less than the standard tile size, the tile needs to be padded.
The assumption is that this will happen to the right and at the bottom of the image, but it can be done at
top/left to by setting rightPad/bottomPad to False.
"""
print("dest", destSize)
imShape = im.shape
assert imShape[0] <= destSize, "Image rows need to be fewer than what it will be padded to"
assert imShape[1] <= destSize, "Image cols need to be fewer than what it will be padded to"
if rightPad:
lPad = 0
rPad = destSize - imShape[1]
else:
lPad = destSize - imShape[1]
rPad = 0
if bottomPad:
bPad = destSize - imShape[0]
tPad = 0
else:
bPad = 0
tPad = destSize - imShape[0]
# Replace with isRGB once in repository
if len(imShape) >= 3 and imShape[2] == 3:
constantVal = (constantVal, constantVal, constantVal)
return cv2.copyMakeBorder(im, tPad, bPad, lPad, rPad, borderType=cv2.BORDER_CONSTANT, value=constantVal)
def addBorderToTile(im, topPad=0, bottomPad=0, leftPad=0, rightPad=0, borderType=cv2.BORDER_DEFAULT):
"""
Pad the edge tiles to generate desired boundary conditions, e.g. top left tile will get top and left side padded.
Padding should be filter size / 2 -1, e.g. a 5x5 filter should have a 2 pixel padding
Input parameters:
im = Tile image
righPad = How many pixels the tile should be padded with on the right side.
leftPad = How many pixels the tile should be padded with on the left side.
topPad = How many pixels the tile should be padded with on the top side.
bottomPad = How many pixels the tile should be padded with on the bottom side.
borderType = How we would like to handle our borders, default is reflective padding.
"""
return cv2.copyMakeBorder(im, topPad, bottomPad, leftPad, rightPad, borderType)
def smoothAndDownsample(im, kernelSize=5, rightPad=True, bottomPad=True, destSize=256, constantVal=0):
"""This is a mock for creating a lower level of a pyramid.
In reality the smoothing needs to be done on padded images."""
# The amount of padding needed given kernel size
padPixels = kernelSize // 2
# pad image with padPixels
# Padding is outside of this scope, it needs to load neighboring tiles
# For edge tiles, addBorderToTile can be used to take care of boundary effects
im = smoothIm(im, kernelSize=kernelSize)
# Remove padding from image here
im = downsample(im)
if im.shape[0] < destSize or im.shape[1] < destSize:
im = constantPadIm(im, rightPad=rightPad, bottomPad=bottomPad, destSize=destSize, constantVal=constantVal)
# How do we deal with tiles with only partial content? Do we need to store locations where padding has occurred somewhere?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment