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倍):

相关推荐
audyxiao00117 小时前
计算机视觉顶刊《International Journal of Computer Vision》2025年5月前沿热点可视化分析
图像处理·人工智能·opencv·目标检测·计算机视觉·大模型·视觉检测
whoarethenext21 小时前
使用 C/C++ 和 OpenCV 实现滑动条控制图像旋转
c语言·c++·opencv
SuperW21 小时前
Opencv中的copyto函数
人工智能·opencv·计算机视觉
whoarethenext21 小时前
使用 OpenCV (C++) 进行人脸边缘提取
c++·人工智能·opencv
阿幸软件杂货间21 小时前
PPT转图片拼贴工具 v3.0
python·opencv·计算机视觉·powerpoint
Blossom.1181 天前
使用Python和OpenCV实现图像识别与目标检测
人工智能·python·神经网络·opencv·安全·目标检测·机器学习
橙色小博1 天前
python中的经典视觉模块:OpenCV(cv2)全面解析
人工智能·opencv·计算机视觉
jndingxin1 天前
OpenCV CUDA模块图像处理-----对图像执行 均值漂移过程(Mean Shift Procedure)函数meanShiftProc()
图像处理·opencv
jndingxin2 天前
OPenCV CUDA模块目标检测----- HOG 特征提取和目标检测类cv::cuda::HOG
人工智能·opencv·目标检测
清醒的兰2 天前
OpenCV 图像像素的逻辑操作
人工智能·opencv·计算机视觉