日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

最近邻插值法

發(fā)布時(shí)間:2023/12/29 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 最近邻插值法 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

文章目錄

  • 前言
  • 一、最近鄰插值法
  • 二、代碼實(shí)現(xiàn)
  • 總結(jié)


前言

??本章節(jié)進(jìn)入圖像處理,利用python語言來實(shí)現(xiàn)各種圖像處理的方法,從軟件角度去理解圖像處理方法,為后期的FPGA處理圖像做準(zhǔn)備。


一、最近鄰插值法

??最近鄰插值就是在目標(biāo)像素點(diǎn)上插入離對(duì)應(yīng)原像素點(diǎn)最近的點(diǎn)的像素值,而不考慮圖像可能的顏色變化。因此,該算法方法較為單一,原理也較為簡(jiǎn)單,計(jì)算量也是最少的。
??計(jì)算公式如下(1)所示:
??src_x:原圖的x坐標(biāo)
??src_y:原圖的y坐標(biāo)
??des_x:目標(biāo)圖像的x坐標(biāo)
??des_y:目標(biāo)圖像的y坐標(biāo)
??src_w:原圖的width寬度
??src_h:原圖的height高度
??des_w:目標(biāo)圖像的width寬度
??des_h:目標(biāo)圖像的height高度

{src_x=des_x?src_w/des_wsrc_y=des_y?src_h/des_h(1)\begin{cases} src\_x = des\_x\ * src\_w\ /\ des\_w\\ src\_y = des\_y\ * src\_h\ /\ des\_h \end{cases}\tag{1}{src_x=des_x??src_w?/?des_wsrc_y=des_y??src_h?/?des_h?(1)
??為了使得原圖和目標(biāo)圖像的幾何中心點(diǎn)重合,對(duì)應(yīng)改為公式(2):
{src_x=(des_x+0.5)?src_w/des_w?0.5src_y=(des_y+0.5)?src_h/des_h?0.5(2)\begin{cases} src\_x = (des\_x + 0.5)\ * src\_w\ /\ des\_w\ -\ 0.5\\ src\_y = (des\_y + 0.5)\ * src\_h\ /\ des\_h\ -\ 0.5 \end{cases}\tag{2}{src_x=(des_x+0.5)??src_w?/?des_w???0.5src_y=(des_y+0.5)??src_h?/?des_h???0.5?(2)
??圖像使用的是三體圖像,最近小編沉迷于《三體》電視劇,所以借用三體的一張圖片來進(jìn)行最近鄰插值法縮放。

二、代碼實(shí)現(xiàn)

""" author: caigui lin created by: 2023/2/1 description: nearest interpolation function src_img: source image(ndarray) des_size: reshaped image size(tuple) return: resized image(ndarray) """ import cv2 import numpy as np import matplotlib.pyplot as plt image = cv2.imread("three_body.jpg") image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)#cv2 color mode is BGR, plt color mode is RGB, you need reverse the color mode before implementing the nearest interpolation function. def nearest_interpolation(src_img, des_shape):src_size = src_img.shape[:-1]des_size = des_shape[:-1]src_w, src_h = src_sizedes_w, des_h = des_sizedes_img = np.zeros(des_shape)for i in range(des_h):for j in range(des_w):des_img[i, j, 0] = src_img[round((i + 0.5) * (src_w / des_w) - 0.5), round((j + 0.5) * (src_h / des_h) - 0.5), 0]des_img[i, j, 1] = src_img[round((i + 0.5) * (src_w / des_w) - 0.5), round((j + 0.5) * (src_h / des_h) - 0.5), 1]des_img[i, j, 2] = src_img[round((i + 0.5) * (src_w / des_w) - 0.5), round((j + 0.5) * (src_h / des_h) - 0.5), 2]des_img = des_img.astype(np.uint8)# display the image(uint8 data type)return des_img new_image = nearest_interpolation(image, (100, 100, 3)) plt.imshow(image) plt.imshow(new_image)

縮放前


縮放后

總結(jié)

??秦始皇執(zhí)劍道:“成計(jì)算機(jī)隊(duì)列!”。三千萬士兵,三人一組,形成計(jì)算機(jī)基本單元,與門、非門、同或、異或、三態(tài)門等等。不同功能的士兵組成計(jì)算機(jī)的CPU、總線、外存等等,他們的目的是計(jì)算三體世界的規(guī)律。看完過后,小編由衷的感嘆劉慈欣腦洞之大。小編也在思考,這秦始皇不就是FPGA嘛,擁有這么多的底層資源,想干嘛干嘛。至此,感謝你的觀看,后續(xù)將繼續(xù)推出其他插值算法,敬請(qǐng)期待。

總結(jié)

以上是生活随笔為你收集整理的最近邻插值法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。