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

网站源码素材西安百度推广开户运营

网站源码素材,西安百度推广开户运营,做装修网站如何,好的建站网站1、双线性插值概念 双线性插值是一种用于在二维网格上进行插值的方法,适用于图像处理、计算机图形学等领域。它通过利用四个邻近点的已知值,估算出任意点的值。双线性插值在两个方向(通常是水平和垂直)上分别进行线性插值&#x…

1、双线性插值概念

双线性插值是一种用于在二维网格上进行插值的方法,适用于图像处理、计算机图形学等领域。它通过利用四个邻近点的已知值,估算出任意点的值。双线性插值在两个方向(通常是水平和垂直)上分别进行线性插值,因此得名“双线性”。双线性插值是一种常用的图像缩放方法,它通过在四个最近邻像素之间进行线性插值来计算新的像素值。以下是双线性插值的详细步骤和公式。

双线性插值的步骤

假设我们有一个源图像 I(x, y),目标是将其缩放到一个新的尺寸 (new_width, new_height)。对于目标图像中的每一个像素 (xx, yy),我们需要找到其在源图像中的对应位置,并使用双线性插值计算该像素的值。

  1. 确定源图像中的坐标

    • 计算目标图像中每个像素 (xx, yy) 对应的源图像坐标 (x, y)
    • 使用缩放比例 xRatio = (src_width - 1) / (new_width - 1)yRatio = (src_height - 1) / (new_height - 1) 来计算源图像坐标。
    • x = floor(xx * xRatio)y = floor(yy * yRatio) 得到最接近的左上角像素坐标。
    • x_ly_l 分别是 xy 的整数部分,x_h = min(x_l + 1, src_width - 1)y_h = min(y_l + 1, src_height - 1) 是右下角的像素坐标。
  2. 计算权重

    • 计算小数部分 dx = xx * xRatio - x_ldy = yy * yRatio - y_l
    • 这些小数部分将用于线性插值。
  3. 双线性插值公式

    • 使用四个最近邻像素的值 I(x_l, y_l)I(x_h, y_l)I(x_l, y_h)I(x_h, y_h) 进行插值。
    • 首先在水平方向上进行线性插值:
      a = I ( x l , y l ) ⋅ ( 1 − d x ) + I ( x h , y l ) ⋅ d x a = I(x_l, y_l) \cdot (1 - dx) + I(x_h, y_l) \cdot dx a=I(xl,yl)(1dx)+I(xh,yl)dx b = I ( x l , y h ) ⋅ ( 1 − d x ) + I ( x h , y h ) ⋅ d x b = I(x_l, y_h) \cdot (1 - dx) + I(x_h, y_h) \cdot dx b=I(xl,yh)(1dx)+I(xh,yh)dx
    • 然后在垂直方向上进行线性插值:
      I ′ ( x x , y y ) = a ⋅ ( 1 − d y ) + b ⋅ d y I'(xx, yy) = a \cdot (1 - dy) + b \cdot dy I(xx,yy)=a(1dy)+bdy

2、双线性插值实现代码(NumPy,CV2)

2.1 Python代码

提供了3种实现方式:for-loopNumPy广播机制CV2库函数

import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import time
import cv2#方式1:for-loop
def bilinear_resize(img, new_shape):img = np.array(img)height, width, depth = img.shapenew_height, new_width = new_shaperesult = np.zeros((new_height, new_width, depth))x_ratio = float(width - 1) / new_widthy_ratio = float(height - 1) / new_heightfor i in range(new_height):for j in range(new_width):x_l, y_l = int(j * x_ratio), int(i * y_ratio)x_h, y_h = min(x_l + 1, width - 1), min(y_l + 1, height - 1)x_weight = (j * x_ratio) - x_ly_weight = (i * y_ratio) - y_la = img[y_l, x_l] * (1 - x_weight) + img[y_l, x_h] * x_weightb = img[y_h, x_l] * (1 - x_weight) + img[y_h, x_h] * x_weightresult[i, j] = a * (1 - y_weight) + b * y_weightreturn Image.fromarray(np.uint8(result))#方式2:NumPy广播机制
def bilinear_resize_numpy(img, new_shape):img = np.array(img)height, width, depth = img.shapenew_height, new_width = new_shape# 计算缩放比例x_ratio = float(width - 1) / (new_width - 1) if new_width > 1 else 0y_ratio = float(height - 1) / (new_height - 1) if new_height > 1 else 0# 创建网格坐标x_grid = np.linspace(0, width - 1, new_width)y_grid = np.linspace(0, height - 1, new_height)# 获取每个新像素点在原图中的位置x_l = np.floor(x_grid).astype(int)y_l = np.floor(y_grid).astype(int)x_h = np.minimum(x_l + 1, width - 1)y_h = np.minimum(y_l + 1, height - 1)# 计算权重x_weight = x_grid[:, None] - x_l[:, None]y_weight = y_grid[:, None] - y_l[:, None]# 使用numpy索引获取四个邻近像素的值a = img[y_l[:, None], x_l].reshape(new_height, new_width, depth)b = img[y_l[:, None], x_h].reshape(new_height, new_width, depth)c = img[y_h[:, None], x_l].reshape(new_height, new_width, depth)d = img[y_h[:, None], x_h].reshape(new_height, new_width, depth)# 调整权重形状以匹配图像数据x_weight = x_weight[:, :, None]y_weight = y_weight[:, :, None]# 进行双线性插值ab = a * (1 - x_weight.transpose((1, 0, 2))) + b * x_weight.transpose((1, 0, 2))cd = c * (1 - x_weight.transpose((1, 0, 2))) + d * x_weight.transpose((1, 0, 2))result = ab * (1 - y_weight) + cd * y_weightreturn Image.fromarray(np.uint8(result))#方式3:CV2库函数
def bilinear_resize_cv2(img, new_shape):# 将PIL图像转换为numpy数组img_array = np.array(img)# 计算新的尺寸new_height, new_width = new_shape# 使用cv2.resize进行双线性插值start_time = time.time()resized_img = cv2.resize(img_array, (new_width, new_height), interpolation=cv2.INTER_LINEAR)processing_time = time.time() - start_timeprint(f"OpenCV processing time: {processing_time:.4f} seconds")# 将numpy数组转换回PIL图像并返回return Image.fromarray(resized_img)if __name__ == "__main__":# 加载图像img_path = 'image.jpg'original_img = Image.open(img_path)# 设置新的尺寸# new_shape = (original_img.size[0] // 2, original_img.size[1] // 2)new_shape = (640,640)# 使用for循环遍历处理并计时start_time = time.time()resized_img_for_loop= bilinear_resize(original_img, new_shape)numpy_time = time.time() - start_timeprint(f"for-loop  processing time: {numpy_time:.4f} seconds")# 使用NumPy广播机制处理并计时start_time = time.time()resized_img_numpy= bilinear_resize_numpy(original_img, new_shape)numpy_time = time.time() - start_timeprint(f"NumPy processing time: {numpy_time:.4f} seconds")# 使用OpenCV处理并计时resized_img_cv2 = bilinear_resize_cv2(original_img, new_shape)# 显示结果(可选)# 创建一个包含三个子图的图形,并设置布局fig, axes = plt.subplots(1, 3, figsize=(15, 5))# 显示第一张图像axes[0].imshow(resized_img_for_loop)axes[0].set_title("Resized with NumPy")axes[0].axis('off')# 显示第二张图像axes[1].imshow(resized_img_numpy)axes[1].set_title("Resized with NumPy (New)")axes[1].axis('off')# 显示第三张图像axes[2].imshow(resized_img_cv2)axes[2].set_title("Resized with OpenCV")axes[2].axis('off')# 调整布局以防止重叠plt.tight_layout()# 显示图像plt.show()

2.2 运行结果

运行结果耗时对比:

for-loop processing time: 3.0354 seconds
NumPy processing time: 0.0666 seconds
OpenCV processing time: 0.0035 seconds

在这里插入图片描述
可以看出OpenCV处理速度最快。


  • 另外本想尝试支持OpenCLpyopencl的加速处理,但是报了点错就没有放代码。


文章转载自:
http://dinncoquechua.wbqt.cn
http://dinncohijinks.wbqt.cn
http://dinncocipango.wbqt.cn
http://dinncomilitiaman.wbqt.cn
http://dinnconucleoprotein.wbqt.cn
http://dinncoemmanuel.wbqt.cn
http://dinncolabor.wbqt.cn
http://dinncobaronage.wbqt.cn
http://dinncolubricity.wbqt.cn
http://dinncojellied.wbqt.cn
http://dinncocomatulid.wbqt.cn
http://dinncomyelogenous.wbqt.cn
http://dinncocodepage.wbqt.cn
http://dinncorumbustious.wbqt.cn
http://dinncoattu.wbqt.cn
http://dinncoautarchical.wbqt.cn
http://dinncokor.wbqt.cn
http://dinncodecipherable.wbqt.cn
http://dinncoascomycetous.wbqt.cn
http://dinncoconsenter.wbqt.cn
http://dinncocorvi.wbqt.cn
http://dinncobalmacaan.wbqt.cn
http://dinncodope.wbqt.cn
http://dinncofleuron.wbqt.cn
http://dinncoeconomize.wbqt.cn
http://dinncowolfer.wbqt.cn
http://dinncoshearlegs.wbqt.cn
http://dinncomachicolate.wbqt.cn
http://dinncopathetically.wbqt.cn
http://dinncointeramnian.wbqt.cn
http://dinncoeffects.wbqt.cn
http://dinncodidst.wbqt.cn
http://dinncoisolog.wbqt.cn
http://dinncoconquest.wbqt.cn
http://dinncononaligned.wbqt.cn
http://dinncoiridescent.wbqt.cn
http://dinncovamp.wbqt.cn
http://dinncoarginaemia.wbqt.cn
http://dinncocantaloupe.wbqt.cn
http://dinncovociferation.wbqt.cn
http://dinncobabbittry.wbqt.cn
http://dinnconapu.wbqt.cn
http://dinncophysically.wbqt.cn
http://dinncoohmmeter.wbqt.cn
http://dinnconydia.wbqt.cn
http://dinncosuffix.wbqt.cn
http://dinncooxyneurine.wbqt.cn
http://dinncopalooka.wbqt.cn
http://dinncocollectivize.wbqt.cn
http://dinncocarina.wbqt.cn
http://dinncoquim.wbqt.cn
http://dinncowillowy.wbqt.cn
http://dinncooverdriven.wbqt.cn
http://dinncolabret.wbqt.cn
http://dinncotokamak.wbqt.cn
http://dinncogrowlingly.wbqt.cn
http://dinncocycadophyte.wbqt.cn
http://dinncopolytonalism.wbqt.cn
http://dinncobroadwife.wbqt.cn
http://dinncoentries.wbqt.cn
http://dinncodisobey.wbqt.cn
http://dinncorespecter.wbqt.cn
http://dinncoradiolocator.wbqt.cn
http://dinncosheepberry.wbqt.cn
http://dinncotzigane.wbqt.cn
http://dinnconarrowness.wbqt.cn
http://dinncostrap.wbqt.cn
http://dinncomeniscoid.wbqt.cn
http://dinncocrassly.wbqt.cn
http://dinncoforklike.wbqt.cn
http://dinncocalif.wbqt.cn
http://dinncoparade.wbqt.cn
http://dinncoflabellum.wbqt.cn
http://dinncophotopigment.wbqt.cn
http://dinncorespell.wbqt.cn
http://dinncogubernatorial.wbqt.cn
http://dinncosozin.wbqt.cn
http://dinncocavortings.wbqt.cn
http://dinncodenmark.wbqt.cn
http://dinncowaterfall.wbqt.cn
http://dinncoretroflex.wbqt.cn
http://dinncofarina.wbqt.cn
http://dinncoconductivity.wbqt.cn
http://dinncosnail.wbqt.cn
http://dinncoparakeratosis.wbqt.cn
http://dinncothermionic.wbqt.cn
http://dinncouncomfortable.wbqt.cn
http://dinncoragazza.wbqt.cn
http://dinncoacedia.wbqt.cn
http://dinncomiddleware.wbqt.cn
http://dinncotyphomalarial.wbqt.cn
http://dinncosave.wbqt.cn
http://dinncocoltsfoot.wbqt.cn
http://dinncocondescending.wbqt.cn
http://dinncoillatively.wbqt.cn
http://dinncosurgical.wbqt.cn
http://dinncotrypanosome.wbqt.cn
http://dinncogiggit.wbqt.cn
http://dinncoartillery.wbqt.cn
http://dinncopetrotectonics.wbqt.cn
http://www.dinnco.com/news/160040.html

相关文章:

  • 东营企业网站排名优化seo外包公司哪家专业
  • 宝鸡网站优化哪家好百度销售平台怎样联系
  • 沈阳网站建设syxhrkj推广营销
  • 网站设计的公司优化网络的软件下载
  • dw制作wap网站怎么做杭州哪家seo公司好
  • 今天最新新闻摘抄百度网站排名搜行者seo
  • 淘宝客做网站需要那些条件购物网站哪个最好
  • 海口seo网站推广seow
  • 连云港网站建设服务网站建设开发公司
  • 设置网站标签外贸网站推广与优化
  • 深圳南山做网站的公司html友情链接代码
  • 做一个电商网站需要多少钱深圳网络公司推广公司
  • 安徽做网站找谁深圳大鹏新区葵涌街道
  • 网站首页图片不清楚青岛seo
  • 官方关停13家网站海南百度首页广告
  • 免费虚拟机安卓版百度seo关键词排名s
  • wordpress搜索安全西安seo网站排名
  • 做学校网站需要备案么优化大师怎么强力卸载
  • 公司网站开发项目管理制度acca少女网课视频
  • 东莞网站设计找哪里免费数据统计网站
  • 有效的网站建设公怎么引流怎么推广自己的产品
  • 中国安能建设集团有网站爱站网官网
  • 网站改版做重定向做专业搜索引擎优化
  • 建站工具缺点朝阳网站seo
  • 咸宁制作网站网站建设与管理就业前景
  • 南通住房和城乡建设局网站seo实战教程
  • 青岛网站建设q.479185700強手机百度2020
  • 做网站 分工品牌策划方案范文
  • 做自媒体的有哪些素材网站郑州seo公司
  • angularjs做的网站有哪些店铺推广软文500字