当前位置: 首页 > news >正文

珠海市斗门建设局网站郑州seo代理外包公司

珠海市斗门建设局网站,郑州seo代理外包公司,烟台网站建设报价,建站行业新闻什么是图像金字塔 图像金字塔是对图像的一种多尺度表达,将各个尺度的图像按照分辨率从小到大,依次从上到下排列,就会形成类似金字塔的结构,因此称为图像金字塔。 常见的图像金字塔有两类,一种是高斯金字塔&#xff0…

什么是图像金字塔

图像金字塔是对图像的一种多尺度表达,将各个尺度的图像按照分辨率从小到大,依次从上到下排列,就会形成类似金字塔的结构,因此称为图像金字塔。

常见的图像金字塔有两类,一种是高斯金字塔(Gaussian Pyramid),另一种的拉普拉斯金字塔(Laplacian Pyramid)。

一般在图像处理中,高斯代表“模糊”,而拉普拉斯代表“差异”。

高斯金字塔通过不断对图像进行模糊且下采样而获得,下采样的因子一般是2倍。随着分辨率越来越小,图像会越来越模糊,高斯金字塔的最底层就是原始图像本身。

拉普拉斯金字塔在高斯金字塔的基础上,对所有层进行上采样(一般也是2倍上采样),然后使用原高斯金字塔结果减去通分辨率的上采样结果得到每一层差异,即为拉普拉斯金字塔。注意拉普拉斯金字塔中分辨率最小的图片等同于高斯金字塔通分辨率图片,其他层均为“求差”得到的结果。另外还需注意图像先下采样再上采样后不能复原,因为下采样会产生信息缺失,简单上采样无法弥补回这些信息缺失。

为什么要使用图像金字塔

图像金字塔有很多应用,特别是面对多尺度任务时尤为有用。比如在目标检测任务中,检测对象在图像中的大小往往非常多变,在单一图像尺度下进行滑框寻找往往不能覆盖所有目标,所以就需要在多个尺度下进行滑框,传统的目标检测和基于深度学习的目标检测均是如此。

拉普拉斯金字塔中,大部分的数值接近于0,所以一定程度上可以用于图像压缩。拉普拉斯金字塔还常用于图像融合,基于拉普拉斯金字塔的图像融合,融合边界的过渡往往会相对自然一些。

金字塔的构建

基础函数

  • 模糊(卷积)
    在金字塔的构建中,上下采样均需要做模糊,下采样中做模糊是为了防止锯齿现象,上采样中做模糊是因为图像金字塔分解中的上采样比较“特别”,不做模糊不行。这里一般使用一个固定的5x5卷积核做模糊,也可以1x5的卷积核,使用行列分离的卷积方法做模糊。

  • 下采样
    先对图像做模糊,然后直接每隔一个像素抽一个数据即可实现2倍下采样。

  • 上采样
    将每个像素扩展成2x2的小区域,原像素放在左上角,其他3个位置补0,然后将卷积核乘以4,再对扩展后的图像做模糊即可。
    上采样还需注意一个关于数据类型的细节:拉普拉斯金字塔才需要用到上采样,生成拉普拉斯金字塔的过程中需要求差操作,并且拉普拉斯金字塔常常跟图像重建会扯上关系,而uint8在求差或者重建时会引起数据截断误差,所以有可能需要用到非uint8数据类型来作为输出。

特别注意:通过上面描述可以发现,在图像金字塔构建时,上下采样的操作非常简单粗暴,不需要用到常规resize时的图像插值。

代码如下:
文件起名resample.py

# -*- coding: utf-8 -*-
import cv2
import numpy as npdef blur(image, kernel_scale=1.0):"""Blur image using a fixed kernel. Kernel scale can be set.Parameters----------image: image data read by opencv.kernel_scale: the scale factor of kernel."""blur_kernel = np.array([[1, 4, 6, 4, 1],[4, 16, 24, 16, 4],[6, 24, 36, 24, 6],[4, 16, 24, 16, 4],[1, 4, 6, 4, 1]]) / 256.blurred_image = cv2.filter2D(image, ddepth=-1,kernel=blur_kernel * kernel_scale,borderType=cv2.BORDER_REFLECT101)return blurred_imagedef pyramid_down(image):"""Down sample an image by 2x.Parameters----------image: image data read by opencv."""blurred_image = blur(image)image_down = blurred_image[::2, ::2]return image_downdef pyramid_up(image, dst_size=None, dtype=np.uint8):"""Up sample an image by 2x. The output size and data type can be set.Parameters----------image: image data read by opencv.dst_size: the output size. Note that the difference of dst_size and2*image_size should be <=2.dtype: the output data type."""# check dst_sizeheight, width = image.shape[:2]if dst_size is None:dst_size = (width * 2, height * 2)else:if abs(dst_size[0] - width * 2) > 2 or \abs(dst_size[1] - height * 2) > 2:raise ValueError(r'the difference of dst_size and 2*image_size 'r'should be <=2.')# create a new buffer that has the dst_sizedst_width, dst_height = dst_sizeif image.ndim == 2:image_up = np.zeros(shape=(dst_height, dst_width), dtype=dtype)else:channel = image.shape[2]image_up = np.zeros(shape=(dst_height, dst_width, channel),dtype=dtype)image_up[::2, ::2] = imageimage_up = blur(image_up, 4.0)return image_up

高斯金字塔 & 拉普拉斯金字塔

高斯金字塔的实现非常简单,不断地使用pyramid_down进行下采样即可,没什么特别需要注意的。
拉普拉斯金字塔需要注意一些事项:1、在生成拉普拉斯金字塔的过程中需要求差,为了不引起数据类型的截断误差, 需要把待求差的两个变量的类型先从uint8转为float32,然后再做求差操作。2、由拉普拉斯金字塔重建图像的过程如果使用uint8也容易产生截断误差,所以在做加法时也需要转float32。

代码如下:
文件起名pyramid.py

# -*- coding: utf-8 -*-
import numpy as np
from resample import pyramid_down
from resample import pyramid_updef image_to_gaussian_pyramid(image, level, cut_size=(3, 3)):"""Build gaussian pyramid for an image. The size of the output component isarranged in descending order.Parameters----------image: input image data read by opencv.level: level of output pyramid.cut_size: the minimal size of pyramid component, smaller than which thebuilding process will be stopped."""gaussian_pyramid = [image]if level <= 1:return gaussian_pyramidfor i in range(level - 1):# check down-sampled image size, should be >= cut_sizeheight, width = image.shape[:2]height_down = (height + 1) // 2width_down = (width + 1) // 2if width_down < cut_size[0] or height_down < cut_size[1]:break# down sampleimage = pyramid_down(image)gaussian_pyramid.append(image)return gaussian_pyramiddef gaussian_to_laplacian_pyramid(gaussian_pyramid):"""Build a laplacian pyramid from gaussian pyramid. The size of the outputcomponent is arranged in ascending order."""laplacian_pyramid = [gaussian_pyramid[-1]]level = len(gaussian_pyramid)if level == 1:return laplacian_pyramidfor i in range(level - 1, 0, -1):up_size = gaussian_pyramid[i - 1].shape[:2][::-1]image_up = pyramid_up(gaussian_pyramid[i], up_size)# compute difference, use float type to avoid exceeding uint8 limitdiff = np.float32(gaussian_pyramid[i - 1]) - np.float32(image_up)laplacian_pyramid.append(diff)return laplacian_pyramiddef image_to_laplacian_pyramid(image, level, cut_size=(3, 3)):"""Build a laplacian pyramid from an image. The size of the output componentis arranged in an ascending order.Parameters----------image: input image data read by opencv.level: level of output pyramid.cut_size: the minimal size of pyramid component, smaller than which thebuilding process will be stopped."""gaussian_pyramid = image_to_gaussian_pyramid(image, level, cut_size)laplacian_pyramid = gaussian_to_laplacian_pyramid(gaussian_pyramid)return laplacian_pyramiddef laplacian_pyramid_to_image(laplacian_pyramid):"""Reconstruct an image from laplacian pyramid."""image = laplacian_pyramid[0]level = len(laplacian_pyramid)for i in range(1, level):up_size = laplacian_pyramid[i].shape[:2][::-1]image = pyramid_up(image, up_size, np.float32)image = np.float32(image) + laplacian_pyramid[i]image = np.uint8(np.clip(np.round(image), 0, 255))return imagedef get_pyramid_index(original_index, level):"""Get the index of a certain pyramid component corresponding to an index oforiginal imageParameters----------original_index: the index of original image.level: level for pyramid component."""if level < 0:raise ValueError("level can NOT be less than 0")if level == 0:return original_indexbase = 2 ** levelmod = original_index % baseif base == 2 * mod:# decimal part is 0.5return int(round(original_index / base / 2)) * 2else:return int(round(original_index / base))

以下代码是金字塔分解的demo示例。

# -*- coding: utf-8 -*-
import cv2
import numpy as np
from pyramid import image_to_gaussian_pyramid
from pyramid import image_to_laplacian_pyramiddef gaussian_pyramid_test():level = 5image = cv2.imread(r'undead.png')height, width, channel = image.shapegau_pyr = image_to_gaussian_pyramid(image, level)# plotoutput = np.zeros((height, width * 2, channel), dtype=np.uint8)x = 0for i in range(level):height, width = gau_pyr[i].shape[:2]output[:height, x:x + width] = gau_pyr[i]x += widthcv2.imwrite('gaussian_pyramid_test.png', output)def laplacian_pyramid_test():level = 5image = cv2.imread(r'undead.png')height, width, channel = image.shapelap_pyr = image_to_laplacian_pyramid(image, level)# plotoutput = np.zeros((height, width * 2, channel), dtype=np.float32)x = width * 2for i in range(level - 1, -1, -1):height, width = lap_pyr[i].shape[:2]if i == 0:output[:height, x - width:x] = lap_pyr[i]else:output[:height, x - width:x] = lap_pyr[i] * 10x -= widthcv2.imwrite('laplacian_pyramid_test.png', output)if __name__ == '__main__':gaussian_pyramid_test()laplacian_pyramid_test()

得到的结果如下,分别是5层的高斯金字塔和拉普拉斯金字塔,注意拉普拉斯金字塔除了最小分辨率是正常图像外,其他分量均为残差。为了能够把残差看的更清楚,乘以了一个系数。

请添加图片描述
请添加图片描述

金字塔的应用

图像融合

这张图打魔兽的伙计们应该都挺熟的,这是阿尔萨斯在巫妖王形态和人类王子形态的融合体。
请添加图片描述
下面我分别找到了原始巫妖王形态和人类王子形态的图像,并作了一些裁减,让图像分辨率一样,并且上下位置也大概能拼在一起。
请添加图片描述
请添加图片描述

下面图像是直接对上述两个原始图像各切掉一部分,并拼接在一起的结果,可以看到整体过渡是不自然的。
请添加图片描述

下面我们将使用金字塔重建的方法进行拼接,让边缘的过渡自然一些。

# -*- coding: utf-8 -*-
import cv2
import numpy as np
from pyramid import image_to_laplacian_pyramid
from pyramid import laplacian_pyramid_to_image
from pyramid import get_pyramid_indexif __name__ == '__main__':x1 = 160x2 = 154level = 5undead = cv2.imread(r'undead.png')human = cv2.imread(r'human.png')h1, w1 = undead.shape[:2]h2, w2 = human.shape[:2]laplacian_pyramid_undead = image_to_laplacian_pyramid(undead, level)laplacian_pyramid_human = image_to_laplacian_pyramid(human, level)laplacian_pyramid_blending = []for i in range(level):k = level - i - 1k1 = get_pyramid_index(w1 - x1, k)k2 = get_pyramid_index(x2, k)a = laplacian_pyramid_undead[i]b = laplacian_pyramid_human[i]splicing = np.concatenate([a[:, :k1], b[:, k2:]], axis=1)laplacian_pyramid_blending.append(splicing)blending_image = laplacian_pyramid_to_image(laplacian_pyramid_blending)cv2.imwrite('laplacian_pyramid_blending.png', blending_image)

上面代码的大体流程:1、两张图分别做拉普拉斯金字塔分解;2、把金字塔的各层分量都从鼻子中间拼接在一起;3、对拼接后的金字塔进行重建,即可得到输出。

拼接后的金字塔以及拼接结果如下:
请添加图片描述

下面是5层金字塔做拼接融合的结果。除了嘴部的过渡实在搞不定以外,其他部分如鼻子,额头和下巴的过渡比直接拼接的效果顺滑很多。
请添加图片描述

下面是3层金字塔做拼接融合的结果。过渡的顺滑性显然就不如5层金字塔。
请添加图片描述


文章转载自:
http://unfettered.pwmm.cn
http://dalmazia.pwmm.cn
http://depancreatize.pwmm.cn
http://investitive.pwmm.cn
http://siderography.pwmm.cn
http://cg.pwmm.cn
http://underarm.pwmm.cn
http://antimorph.pwmm.cn
http://rhapsodize.pwmm.cn
http://wear.pwmm.cn
http://chappie.pwmm.cn
http://calico.pwmm.cn
http://externalize.pwmm.cn
http://disjunction.pwmm.cn
http://blackart.pwmm.cn
http://smallholding.pwmm.cn
http://skite.pwmm.cn
http://tasimeter.pwmm.cn
http://sunspot.pwmm.cn
http://morbific.pwmm.cn
http://radiodiagnosis.pwmm.cn
http://chimera.pwmm.cn
http://cheap.pwmm.cn
http://inbent.pwmm.cn
http://firefly.pwmm.cn
http://cyclopaedist.pwmm.cn
http://nemoricolous.pwmm.cn
http://anisometric.pwmm.cn
http://kilogauss.pwmm.cn
http://cajole.pwmm.cn
http://worryingly.pwmm.cn
http://plasmalemma.pwmm.cn
http://actualize.pwmm.cn
http://labyrinthitis.pwmm.cn
http://shenzhen.pwmm.cn
http://tillicum.pwmm.cn
http://bootable.pwmm.cn
http://obsess.pwmm.cn
http://jalousie.pwmm.cn
http://honduranean.pwmm.cn
http://tulipomania.pwmm.cn
http://suplex.pwmm.cn
http://sorriness.pwmm.cn
http://ragbag.pwmm.cn
http://manito.pwmm.cn
http://tasmania.pwmm.cn
http://holdman.pwmm.cn
http://harbour.pwmm.cn
http://jiao.pwmm.cn
http://thallophyte.pwmm.cn
http://clipper.pwmm.cn
http://stammer.pwmm.cn
http://oncology.pwmm.cn
http://maintopmast.pwmm.cn
http://butternut.pwmm.cn
http://statics.pwmm.cn
http://diagrammatic.pwmm.cn
http://vbi.pwmm.cn
http://hobgoblin.pwmm.cn
http://unleash.pwmm.cn
http://boyishly.pwmm.cn
http://snot.pwmm.cn
http://shyness.pwmm.cn
http://shuttle.pwmm.cn
http://crevette.pwmm.cn
http://lyncher.pwmm.cn
http://forgettery.pwmm.cn
http://kiddywinky.pwmm.cn
http://sagacious.pwmm.cn
http://volution.pwmm.cn
http://peahen.pwmm.cn
http://vexatiously.pwmm.cn
http://raffia.pwmm.cn
http://visuosensory.pwmm.cn
http://reorder.pwmm.cn
http://anodize.pwmm.cn
http://infauna.pwmm.cn
http://csiro.pwmm.cn
http://dramalogue.pwmm.cn
http://stateside.pwmm.cn
http://copemate.pwmm.cn
http://semideify.pwmm.cn
http://lichenometric.pwmm.cn
http://tropotaxis.pwmm.cn
http://worriless.pwmm.cn
http://numerary.pwmm.cn
http://burstone.pwmm.cn
http://concretist.pwmm.cn
http://innigkeit.pwmm.cn
http://linocutter.pwmm.cn
http://raob.pwmm.cn
http://phycology.pwmm.cn
http://bye.pwmm.cn
http://palaeethnology.pwmm.cn
http://theia.pwmm.cn
http://monster.pwmm.cn
http://gollywog.pwmm.cn
http://pansy.pwmm.cn
http://unholiness.pwmm.cn
http://worker.pwmm.cn
http://www.dt0577.cn/news/83781.html

相关文章:

  • python做的网站如何打开怎么优化自己网站的关键词
  • 网站模板如何用合肥百度网站排名优化
  • 圣都装饰seo企业顾问
  • 关于做网站的策划方案自媒体平台注册入口
  • 网站开发jsp 很少2345网址导航下载
  • 做网站都需要学什么网站改版seo建议
  • win7 iis默认网站设置seo优化与sem推广有什么关系
  • 网站开发发布搜索引擎优化的方式有哪些
  • 西安360免费做网站企业网站的推广方法有哪些
  • 三门峡做网站的公司电商网站规划
  • 嘉兴做网站建设的公司哪家好好的网络推广平台
  • 做网站应该拿多少提成如何优化seo
  • 室内设计网站公司引流推广接单
  • 个人网站备案可以做项目网站西安今天刚刚发生的新闻
  • 安徽六安疫情源头seo建站优化推广
  • 上海专业网站建设精英专业做加盟推广的公司
  • 服务器网站访问慢百度手机助手app免费下载
  • 大型移动网站建设青岛seo外包公司
  • 涿州网站制作seo合作
  • 公司网站url百度指数是什么
  • wordpress 建站公司互联网金融
  • 招聘网站建设价格武汉排名seo公司
  • 图片手机网站建设网店代运营需要多少钱
  • 中英文公司网站站长平台工具
  • 有什么可以做兼职的网站吗景区营销案例100例
  • 网站模板 家百度推广怎么运营
  • 岳阳网站制作公司外贸网站建设报价
  • wordpress微信公众莫停之科技windows优化大师
  • 企业如何做网络推广关键词优化排名详细步骤
  • 云盘做网站空间百度指数在线查询工具