pythonpictureanalysis/Blue_noise_sampling.py
2022-07-28 21:35:42 +08:00

67 lines
1.9 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# -*- coding: utf-8 -*-
# @Time : 2022-7-26 0026 19:09
# @Author : Qing
# @Email : derighoid@gmail.com
# @File : Blue_noise_sampling.py
# @Software: PyCharm
from pathlib import Path
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
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