opencv python 图像测试上采样(升采样)(cv2.pyrUp()) 下采样(cv2.pyrDown()) 池化 滑动窗口(BorderTypes)


在学习tensorflow卷积神经网络时,需要实现图像的下采样,于是便想手动实现一下,但是发现有一点麻烦,于是便寻找看opencv是否有下采样方法,找了下,还真有

When I was young, I desired to be a scientist after I grow up, but things always go oppsite to people’s wish, after suffering a seriels of …

以上纯属闭着眼睛打的

from cv2.__init__.py

def pyrUp(src, dst=None, dstsize=None, borderType=None): # real signature unknown; restored from __doc__
    """
    pyrUp(src[, dst[, dstsize[, borderType]]]) -> dst
    .   @brief Upsamples an image and then blurs it. 上采样图像,然后使其模糊。
    .   
    .   By default, size of the output image is computed as `Size(src.cols\*2, (src.rows\*2)`, but in any
    .   case, the following conditions should be satisfied:
    
    	默认情况下,输出图像的大小计算为`Size(src.cols \ * 2,(src.rows \ * 2)`),
    	但是在任何情况下,都应满足以下条件:
    .   
    .   \f[\begin{array}{l} | \texttt{dstsize.width} -src.cols*2| \leq  ( \texttt{dstsize.width}   \mod  2)  \\ | \texttt{dstsize.height} -src.rows*2| \leq  ( \texttt{dstsize.height}   \mod  2) \end{array}\f]
    .   
    .   The function performs the upsampling step of the Gaussian pyramid construction, though it can
    .   actually be used to construct the Laplacian pyramid. First, it upsamples the source image by
    .   injecting even zero rows and columns and then convolves the result with the same kernel as in
    .   pyrDown multiplied by 4.

		该函数执行高斯金字塔构造的升采样步骤,尽管实际上可以用来构造拉普拉斯金字塔。 
		首先,它甚至通过注入零行和零列来对源图像进行升采样,
		然后将结果与与pyrDown中相同的内核乘以4进行卷积。
    .   
    .   @param src input image.
    .   @param dst output image. It has the specified size and the same type as src .
    			   输出图像。 它具有指定的大小,并且与src类型相同。
    .   @param dstsize size of the output image.
    .   @param borderType Pixel extrapolation method, see #BorderTypes (only #BORDER_DEFAULT is supported)
    					  像素外推方法,请参见#BorderTypes(仅支持#BORDER_DEFAULT)
    """
    pass

公式1:
mod表示取余操作
在这里插入图片描述

def pyrDown(src, dst=None, dstsize=None, borderType=None): # real signature unknown; restored from __doc__
    """
    pyrDown(src[, dst[, dstsize[, borderType]]]) -> dst
    .   @brief Blurs an image and downsamples it. 

		使图像模糊并降低采样率。
    .   
    .   By default, size of the output image is computed as `Size((src.cols+1)/2, (src.rows+1)/2)`, but in
    .   any case, the following conditions should be satisfied:
    
    	默认情况下,输出图像的大小计算为`Size((src.cols + 1)/ 2,(src.rows + 1)/ 2)`,
    	但是在任何情况下,都应满足以下条件:
    .   
    .   \f[\begin{array}{l} | \texttt{dstsize.width} *2-src.cols| \leq 2 \\ | \texttt{dstsize.height} *2-src.rows| \leq 2 \end{array}\f]
    .   
    .   The function performs the downsampling step of the Gaussian pyramid construction. First, it
    .   convolves the source image with the kernel:
    	该函数执行高斯金字塔构造的下采样步骤。 首先,它将源映像与内核进行卷积:
    .   
    .   \f[\frac{1}{256} \begin{bmatrix} 1 & 4 & 6 & 4 & 1  \\ 4 & 16 & 24 & 16 & 4  \\ 6 & 24 & 36 & 24 & 6  \\ 4 & 16 & 24 & 16 & 4  \\ 1 & 4 & 6 & 4 & 1 \end{bmatrix}\f]
    .   
    .   Then, it downsamples the image by rejecting even rows and columns.
    	然后,它通过拒绝偶数行和列对图像进行下采样。???
    .   
    .   @param src input image. 
    			   输入图像。
    .   @param dst output image; it has the specified size and the same type as src.
    			   输出图像; 它具有指定的大小,并且与src类型相同。	
    .   @param dstsize size of the output image.
    				   输出图像的大小。
    .   @param borderType Pixel extrapolation method, see #BorderTypes (#BORDER_CONSTANT isn't supported)
    		        	  像素外推方法,请参阅#BorderTypes(不支持#BORDER_CONSTANT)
    """
    pass

公式1:
意思就是下采样后的图片尺寸的两倍与原图相比不会超过两个像素
在这里插入图片描述
比如原图尺寸为1920×1280,指定目标图片生成尺寸时
使用img = cv2.pyrDown(img, dstsize=(960, 640))可以
img = cv2.pyrDown(img, dstsize=(960, 641))可以
img = cv2.pyrDown(img, dstsize=(960, 642))就不行

公式2:
在这里插入图片描述

示例1:给图片执行两次下采样

在这里插入图片描述
代码:

# -*- coding: utf-8 -*-
"""
@File    : 20200119_图像下采样测试.py
@Time    : 2020/1/19 9:35
@Author  : Dontla
@Email   : [email protected]
@Software: PyCharm
"""
import cv2
img = cv2.imread('girl-3421489_1920.jpg')
# print(img.shape)    # (1280, 1920, 3)
img = cv2.pyrDown(img)
img = cv2.pyrDown(img)
# print(img.shape)    # (320, 480, 3)
cv2.imshow('win', img)
cv2.waitKey(0)

结果:

在这里插入图片描述

示例2:给图片重复100次上下采样

# -*- coding: utf-8 -*-
"""
@File    : 20200119_图像下采样测试.py
@Time    : 2020/1/19 9:35
@Author  : Dontla
@Email   : [email protected]
@Software: PyCharm
"""
import cv2
img = cv2.imread('girl-3421489_1920.jpg')
# print(img.shape)    # (1280, 1920, 3)
for i in range(100):
	img = cv2.pyrUp(img)
	img = cv2.pyrDown(img)
# print(img.shape)    # (320, 480, 3)
cv2.imshow('win', img)
cv2.waitKey(0)

结果:图片变模糊了(看见有一篇文章说在使用高斯变换处理上下采样时,只要图像经历过下采样,就一定会造成图像信息损失)
在这里插入图片描述
参考文章1:OpenCV-Python——上采样、下采样与拉普拉斯金字塔

参考文章2:opencv cv::BorderTypes

发布了771 篇原创文章 · 获赞 32 · 访问量 10万+

猜你喜欢

转载自blog.csdn.net/Dontla/article/details/104037374
今日推荐