OpenCV单通道图像按像素成倍比例放大(无高斯平滑处理)

OpenCV中的resize函数可以对图像做任意比例的放大(/缩小)处理,该处理过程会对图像做高斯模糊化以保证图像在进行放大(/缩小)后尽可能保留源图像所展现的具体内容(消除固定频率插值/采样带来的香农采样信息损失),但在有些场景中该方法不适用,如:部分应用场景只为了展现图像具体像素的色彩信息,则其就不需要对具体输入的图像做高斯平滑处理,则此场景需要自行实现,实现代码如下:

python 复制代码
def enlarge_without_gauss(img, ratio:int):
    h,w = img.shape
    img_x_ratio = np.zeros((img.shape[0]*ratio, img.shape[1]*ratio), dtype=np.uint8)
    for h in range(img.shape[0]):
        for w in range(img.shape[1]):
            img_x_ratio[h*ratio:h*ratio+ratio, w*ratio:w*ratio+ratio] = img[h,w]
    return img_x_ratio

与OpenCV自带的resize函数放大对比简易比较代码如下:

python 复制代码
import numpy as np
import cv2

def enlarge_without_gauss(img, ratio:int):
    h,w = img.shape
    img_x_ratio = np.zeros((img.shape[0]*ratio, img.shape[1]*ratio), dtype=np.uint8)
    for h in range(img.shape[0]):
        for w in range(img.shape[1]):
            img_x_ratio[h*ratio:h*ratio+ratio, w*ratio:w*ratio+ratio] = img[h,w]
    return img_x_ratio

# 随机产生一张单通道图像
img = np.random.rand(80, 120)
img = (img * 255).astype(np.uint8)
cv2.imshow("img", img)

# 设置缩放比例
RATIO = 8

# 按具体像素位放大
img_enlarge_xN_without_gauss = enlarge_without_gauss(img, RATIO)
cv2.imshow("enlarge_xN_without_gauss", img_enlarge_xN_without_gauss)

# 使用opencv自带函数resize放大
h, w = img.shape
img_resize_xN = cv2.resize(img, (w*RATIO, h*RATIO))
cv2.imshow("resize_xN", img_resize_xN)

cv2.waitKey(0)

随机产生的原图如下:

按像素放大效果(设置的8倍):

使用opencv resize函数放大(设置的8倍):

相关推荐
那雨倾城1 小时前
PiscCode使用OpenCV实现漂浮方块特效
python·opencv
大魔王(已黑化)4 小时前
OpenCV —— 绘制图形
人工智能·opencv·计算机视觉
bright_colo5 小时前
Python-初学openCV——图像预处理(四)——滤波器
python·opencv·计算机视觉
Mikowoo0075 小时前
09_opencv_遍历操作图像像素
opencv·计算机视觉
230L1_78M69Q5487H6 小时前
【基于OpenCV的图像处理】图像预处理之二值化处理以及图像的仿射变换
图像处理·opencv·计算机视觉
xw33734095641 天前
彩色转灰度的核心逻辑:三种经典方法及原理对比
人工智能·python·深度学习·opencv·计算机视觉
蓝桉8021 天前
opencv学习(图像金字塔)
人工智能·opencv·学习
牵牛老人1 天前
OpenCV学习探秘之二 :数字图像的矩阵原理,OpenCV图像类与常用函数接口说明,及其常见操作核心技术详解
opencv·学习·矩阵
Gession-杰1 天前
OpenCV图像梯度、边缘检测、轮廓绘制、凸包检测大合集
人工智能·opencv·计算机视觉
水军总督1 天前
OpenCV+Python
python·opencv·计算机视觉