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.
python类pyrUp()的实例源码
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 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 increase_grid_resolution(self, res=0.2, polyorder=2):
"""Gaussian pyramide based upscaling of topographic grid
This function checks the current topographic resolution in the center
of the grid. Based on this, an upsacling factor is determined and the
:func:`cv2.pyrUp` is used to increase the resolution using
interpolation. Note, that this does not increase the actual resolution
of the topographic data grid.
:param float res: desired grid resolution in km (default: 0.2)
:param int polyorder: order of polynomial used for interpolation
(default: 2)
:returns:
- :class:`TopoData`, new object with desired grid resolution
.. note::
This functionality is only available, if :mod:`cv2` is installed
"""
lons = self.lons
lats = self.lats
vals = self.data
if not all(mod(x, 2) == 0 for x in [len(lons), len(lats)]):
print ("Fatal error, odd array size detected, no upscaling possible."
" Return current data dict")
return False
c_lon = len(lons) / 2 - 1 #center longitude index
c_lat = len(lats) / 2 - 1 #center latitude index
p1 = LatLon(lats[c_lat], lons[c_lon])
p2 = LatLon(lats[c_lat + 1], lons[c_lon + 1])
dist = (p2 - p1).magnitude #distance between 2 points on grid
res_fac = dist / res #factor for increasing the spatial resolution
if res_fac <= 1:
print ("No interpolation of topodata necessary: topo raw data "
"already has desired resolution...\nCurrent resolution: %s"
+ str(dist) + "km\nDesired resolution: %s km" %(dist, res))
return self
fac = int(ceil(log2(res_fac))) #the corresponding up-factor for the scale space
print("Increasing spatial topography resolution by factor %s" %(2**fac))
for k in range(fac):
vals = pyrUp(vals)
p_lons = poly1d(polyfit(arange(len(lons)), lons, polyorder))
p_lats = poly1d(polyfit(arange(len(lats)), lats, polyorder))
lons_new = p_lons(linspace(0, len(lons) - 1, vals.shape[1]))
lats_new = p_lats(linspace(0, len(lats) - 1, vals.shape[0]))
return TopoData(lats_new, lons_new, vals, self.data_id + "_interp")