前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >opencv: cv2.resize 探究(源码)

opencv: cv2.resize 探究(源码)

作者头像
JNingWei
发布2018-09-28 16:14:32
2.8K0
发布2018-09-28 16:14:32
举报
文章被收录于专栏:JNing的专栏

  我们 习惯的坐标表示 是 先 x 横坐标,再 y 纵坐标。在图像处理中,这种惯性思维尤其需要担心。

  因为在计算机中,图像是以矩阵的形式保存的,先行后列。所以,一张 宽×高×颜色通道=480×256×3 的图片会保存在一个 256×480×3 的三维张量中。图像处理时也是按照这种思想进行计算的(其中就包括 OpenCV 下的图像处理),即 高×宽×颜色通道

  但是问题来了,cv2.resize这个api却是个小例外。因为它的参数输入却是 宽×高×颜色通道

  查看官方文档 Geometric Image Transformations

resize Resizes an image. C++: void resize(InputArray src, OutputArray dst, Size dsize, double fx=0, double fy=0, int interpolation=INTER_LINEAR )¶ Python: cv2.resize(src, dsize[, dst[, fx[, fy[, interpolation]]]]) → dst C: void cvResize(const CvArr* src, CvArr* dst, int interpolation=CV_INTER_LINEAR ) Python: cv.Resize(src, dst, interpolation=CV_INTER_LINEAR) → None Parameters: src – input image. dst – output image; it has the size dsize (when it is non-zero) or the size computed from src.size(), fx, and fy; the type of dst is the same as of src. dsize – output image size; if it equals zero, it is computed as: \texttt{dsize = Size(round(fx*src.cols), round(fy*src.rows))} Either dsize or both fx and fy must be non-zero.

  由以下语段可知, cv2.resizedsize 的参数输入是 x轴×y轴,即 宽×高

dst – output image; it has the size dsize (when it is non-zero) or the size computed from src.size(), fx, and fy; the type of dst is the same as of src.

  自己写了一个代码实例来验证它:

代码语言:javascript
复制
import cv2
import numpy as np
import random


seq = [random.randint(0, 255) for _ in range(256*480*3)]
mat = np.resize(seq, new_shape=[256, 480, 3])
print ('mat.shape = {}'.format(mat.shape))
cv2.imwrite('origin_pic.jpg', mat)
origin_pic = cv2.imread('./origin_pic.jpg')
print ('origin_pic.shape = {}'.format(origin_pic.shape))
resize_pic = cv2.resize(src=origin_pic,
                        dsize=(int(origin_pic.shape[1] * 2),
                               int(origin_pic.shape[0] * 1))
                        )
print ('resize_pic.shape = {}'.format(resize_pic.shape))
cv2.imshow('resize_pic', resize_pic)
cv2.waitKey(0)
cv2.destroyAllWindows()

  Output:

代码语言:javascript
复制
mat.shape = (256, 480, 3)
origin_pic.shape = (256, 480, 3)
resize_pic.shape = (256, 960, 3)

  成功应验了文档里的参数说明。



本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017年07月24日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档