Fourth commit
This commit is contained in:
@@ -5,46 +5,55 @@
|
||||
# @File : Blue_noise_sampling.py
|
||||
# @Software: PyCharm
|
||||
|
||||
'''
|
||||
计算新图形(放大后或缩小后)的坐标点像素值对应于原图像中哪一个像素点填充的。
|
||||
src是原图,dst是新图,原来的图像宽度/高度除以新图像的宽度/高度可以得到缩放比例,假如是缩小图片括号内的数字小于1,放大则大于1,相当于系数,再乘以新图片的宽度/高度,就实现了缩放。
|
||||
'''
|
||||
|
||||
from PIL import Image
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
import math
|
||||
|
||||
|
||||
# # 最近邻插值算法
|
||||
# # dstH为新图的高;dstW为新图的宽
|
||||
# def blueNoiseSampl(img, dstH, dstW):
|
||||
# scrH, scrW, t = img.shape # src原图的长宽
|
||||
# retimg = np.zeros((dstH, dstW, 3), dtype=np.uint8)
|
||||
# for i in range(dstH - 1):
|
||||
# for j in range(dstW - 1):
|
||||
# scrx = round(i * (scrH / dstH))
|
||||
# scry = round(j * (scrW / dstW))
|
||||
# retimg[i, j] = img[scrx, scry]
|
||||
#
|
||||
# return retimg
|
||||
#
|
||||
#
|
||||
# im_path = './data/th.png'
|
||||
# image = np.array(Image.open(im_path))
|
||||
#
|
||||
# plt.figure(figsize=(16, 8))
|
||||
#
|
||||
# plt.subplot(1, 2, 1)
|
||||
# plt.imshow(image)
|
||||
#
|
||||
# image1 = blueNoiseSampl(image, image.shape[0] * 2, image.shape[1] * 2)
|
||||
# # 从array转换成image
|
||||
# image1 = Image.fromarray(image1.astype('uint8')).convert('RGB')
|
||||
# image1.save('./data/picture13.png')
|
||||
# plt.subplot(1, 2, 2)
|
||||
# plt.imshow(image1)
|
||||
# plt.show()
|
||||
from PIL import Image
|
||||
|
||||
'''
|
||||
srcX=newX*(srcW/newW)
|
||||
srcY=newY*(srcH/newH)
|
||||
src是原图,dst是新图,原来的图像宽度/高度除以新图像的宽度/高度可以得到缩放比例,
|
||||
假如是缩小图片括号内的数字小于1,放大则大于1,相当于系数,再乘以新图片的宽度/高度,就实现了缩放。
|
||||
'''
|
||||
|
||||
def blueNoiseSampl(img, newH, newW):
|
||||
'''
|
||||
|
||||
:param img: 图片
|
||||
:param newH: 新图的高
|
||||
:param newW: 新图的宽
|
||||
:return: 新图
|
||||
'''
|
||||
scrH, scrW, t = img.shape # 原图的长宽
|
||||
retimg = np.zeros((newH, newW, 3), dtype=np.uint8) # 生成 newH* newW *3 的零矩阵
|
||||
for i in range(newH - 1):
|
||||
for j in range(newW - 1):
|
||||
scrx = round(i * (scrH / newH)) # round对其四舍五入
|
||||
scry = round(j * (scrW / newW))
|
||||
retimg[i, j] = img[scrx, scry] # new image
|
||||
return retimg
|
||||
|
||||
|
||||
# 图片展示函数
|
||||
def showImage(picPath):
|
||||
'''
|
||||
|
||||
:param picPath: 图片地址
|
||||
:return: 样图
|
||||
'''
|
||||
# 获取图片矩阵
|
||||
image = np.array(Image.open(picPath))
|
||||
# 设置画布
|
||||
plt.figure(figsize=(16, 8))
|
||||
# 合并
|
||||
plt.subplot(121)
|
||||
plt.imshow(image)
|
||||
# 调用采样函数
|
||||
image1 = blueNoiseSampl(image, image.shape[0] * 2, image.shape[1] * 2)
|
||||
# 图片保存
|
||||
Image.fromarray(np.uint8(image1)).save("./data/picture13.png")
|
||||
plt.subplot(122)
|
||||
plt.imshow(image1)
|
||||
plt.show()
|
||||
return image1
|
||||
|
Reference in New Issue
Block a user