def genMipMap(texCube):
#assume width = height and is 2^x
nLevel = int(min(10, math.log(texCube.shape[1], 2))) + 1
texMipMapList = []
texMipMapList.append(texCube)
for k in range(1, nLevel):
prevCube = texMipMapList[k-1]
if(len(prevCube.shape) == 3):
newCube = np.ones((6, prevCube.shape[1] / 2, prevCube.shape[2] / 2))
else:
newCube = np.ones((6, prevCube.shape[1] / 2, prevCube.shape[2] / 2, 4))
for f in range(0, 6):
newCube[f] = meanDownsample(prevCube[f])#cv2.pyrDown(prevCube[f])
texMipMapList.append(newCube.astype(np.float32))
return texMipMapList
python类pyrDown()的实例源码
def features(image, channel, levels=9, start_size=(1983, 1088), ):
"""
Extracts features by down-scaling the image levels times,
transforms the image by applying the function channel to
each scaled version and computing the difference between
the scaled, transformed versions.
image : the image
channel : a function which transforms the image into
another image of the same size
levels : number of scaling levels
start_size : tuple. The size of the biggest image in
the scaling pyramid. The image is first
scaled to that size and then scaled by half
levels times. Therefore, both entries in
start_size must be divisible by 2^levels.
"""
image = channel(image)
if image.shape != start_size:
image = cv2.resize(image, dsize=start_size)
scales = [image]
for l in xrange(levels - 1):
logger.debug("scaling at level %d", l)
scales.append(cv2.pyrDown(scales[-1]))
features = []
for i in xrange(1, levels - 5):
big = scales[i]
for j in (3,4):
logger.debug("computing features for levels %d and %d", i, i + j)
small = scales[i + j]
srcsize = small.shape[1],small.shape[0]
dstsize = big.shape[1],big.shape[0]
logger.debug("Shape source: %s, Shape target :%s", srcsize, dstsize)
scaled = cv2.resize(src=small, dsize=dstsize)
features.append(((i+1,j+1),cv2.absdiff(big, scaled)))
return features
def pyramid(image, minSize):
yield image
if image.shape[0] < minSize[0] and image.shape[1] < minSize[1]:
# image too small - upscaling until we hit window level
image = cv2.pyrUp(image)
while (image.shape[0] <= minSize[0] or image.shape[1] <= minSize[1]):
yield image
image = cv2.pyrUp(image)
else:
# image too big - downscaling until we hit window level
image = cv2.pyrDown(image)
while (image.shape[0] >= minSize[0] or image.shape[1] >= minSize[1]):
yield image
image = cv2.pyrDown(image)
# Malisiewicz et al.
def render(self,frame):
numDownSamples = 2
img_rgb = frame
# number of downscaling steps
numBilateralFilters = 7
# number of bilateral filtering steps
# -- STEP 1 --
# downsample image using Gaussian pyramid
img_color = img_rgb
for _ in xrange(numDownSamples):
img_color = cv2.pyrDown(img_color)
# repeatedly apply small bilateral filter instead of applying
# one large filter
for _ in xrange(numBilateralFilters):
img_color = cv2.bilateralFilter(img_color, 9, 9, 7)
# upsample image to original size
for _ in xrange(numDownSamples):
img_color = cv2.pyrUp(img_color)
# convert to grayscale and apply median blur
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_RGB2GRAY)
img_blur = cv2.medianBlur(img_gray, 7)
# detect and enhance edges
img_edge = cv2.adaptiveThreshold(img_blur, 255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY, 9, 2)
# -- STEP 5 --
# convert back to color so that it can be bit-ANDed with color image
img_edge = cv2.cvtColor(img_edge, cv2.COLOR_GRAY2RGB)
final = cv2.bitwise_and(img_color, img_edge)
return cv2.medianBlur(final,7)
def render(self,frame):
canvas = cv2.imread("pen.jpg", cv2.CV_8UC1)
numDownSamples = 2
img_rgb = frame
# number of downscaling steps
numBilateralFilters = 3
# number of bilateral filtering steps
# -- STEP 1 --
# downsample image using Gaussian pyramid
img_color = img_rgb
for _ in xrange(numDownSamples):
img_color = cv2.pyrDown(img_color)
# repeatedly apply small bilateral filter instead of applying
# one large filter
for _ in xrange(numBilateralFilters):
img_color = cv2.bilateralFilter(img_color, 9, 9, 3)
# upsample image to original size
for _ in xrange(numDownSamples):
img_color = cv2.pyrUp(img_color)
# convert to grayscale and apply median blur
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_RGB2GRAY)
img_blur = cv2.medianBlur(img_gray, 3)
# detect and enhance edges
img_edge = cv2.adaptiveThreshold(img_blur, 255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY, 9, 2)
return cv2.multiply(cv2.medianBlur(img_edge,7), canvas, scale=1./256)
def doPyrDown(im):
# Do the pyrdown by default half size
out = cv2.pyrDown(im)
return out
def pyrDownUp(img, times=1):
img_temp = img
for i in xrange(int(times)):
img_temp = cv.pyrDown(img_temp)
img_new = img_temp
for i in xrange(int(times)):
img_new = cv.pyrUp(img_new)
return img_new
# img = cv.imread("./test.jpg")
# img_new = pyrDownUp(img, 2)
# cv.imwrite("./new.jpg", img_new)
def getRGB(self,rgb):
self.rgb=rgb
#self.down=cv2.pyrDown(rgb)
self.down[:,:,:]=self.rgb[:,:,:]
#print self.down.shape
#self.down.shape=(150,200)
def _prep_topo_data(self, grid_points=100):
"""Prepare topography data for map
This function prepares high resolution topography for plotting, i.e.
it reduces the resolution based on the map size
:param int grid_points: number of plotted grid points (default: 100)
"""
if not isinstance(self.topo_data, TopoData):
try:
self.load_topo_data()
except:
raise
topo = self.topo_data
# determine the nu
pyr_steps = int(ceil(log2(float(len(topo.lons)) / grid_points)))
z_max = float(topo.max)
z_min = float(topo.min)
z_order = floor(log10(topo.alt_range))
X,Y = meshgrid(topo.lons, topo.lats)
x, y = self(X, Y)
z = topo.data
if not CV2_AVAILABLE:
print ("Could not reduce resolution of topographic data, opencv "
"library is not available")
else:
if pyr_steps > 0:
for k in range(pyr_steps):
x = pyrDown(x)
y = pyrDown(y)
z = pyrDown(z)
return (x, y, z, z_min, z_max, z_order)