简 介: 本博文给你展示了如何通过指定输出结果的宽高来对图像进行尺寸变化。你也了解如何根据比例因子来保持图像的纵横比不发生变化,从而在改变图像尺寸的过程中图像不发生形变。最后也讨论了不同的插值方法如何改变尺寸变化的结果的。

关键词resizeOpenCV图片尺寸

#mermaid-svg-ldM3brlEazVc6CZJ .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-ldM3brlEazVc6CZJ .label text{fill:#333}#mermaid-svg-ldM3brlEazVc6CZJ .node rect,#mermaid-svg-ldM3brlEazVc6CZJ .node circle,#mermaid-svg-ldM3brlEazVc6CZJ .node ellipse,#mermaid-svg-ldM3brlEazVc6CZJ .node polygon,#mermaid-svg-ldM3brlEazVc6CZJ .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-ldM3brlEazVc6CZJ .node .label{text-align:center;fill:#333}#mermaid-svg-ldM3brlEazVc6CZJ .node.clickable{cursor:pointer}#mermaid-svg-ldM3brlEazVc6CZJ .arrowheadPath{fill:#333}#mermaid-svg-ldM3brlEazVc6CZJ .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-ldM3brlEazVc6CZJ .flowchart-link{stroke:#333;fill:none}#mermaid-svg-ldM3brlEazVc6CZJ .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-ldM3brlEazVc6CZJ .edgeLabel rect{opacity:0.9}#mermaid-svg-ldM3brlEazVc6CZJ .edgeLabel span{color:#333}#mermaid-svg-ldM3brlEazVc6CZJ .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-ldM3brlEazVc6CZJ .cluster text{fill:#333}#mermaid-svg-ldM3brlEazVc6CZJ div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-ldM3brlEazVc6CZJ .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-ldM3brlEazVc6CZJ text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-ldM3brlEazVc6CZJ .actor-line{stroke:grey}#mermaid-svg-ldM3brlEazVc6CZJ .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-ldM3brlEazVc6CZJ .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-ldM3brlEazVc6CZJ #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-ldM3brlEazVc6CZJ .sequenceNumber{fill:#fff}#mermaid-svg-ldM3brlEazVc6CZJ #sequencenumber{fill:#333}#mermaid-svg-ldM3brlEazVc6CZJ #crosshead path{fill:#333;stroke:#333}#mermaid-svg-ldM3brlEazVc6CZJ .messageText{fill:#333;stroke:#333}#mermaid-svg-ldM3brlEazVc6CZJ .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-ldM3brlEazVc6CZJ .labelText,#mermaid-svg-ldM3brlEazVc6CZJ .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-ldM3brlEazVc6CZJ .loopText,#mermaid-svg-ldM3brlEazVc6CZJ .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-ldM3brlEazVc6CZJ .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-ldM3brlEazVc6CZJ .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-ldM3brlEazVc6CZJ .noteText,#mermaid-svg-ldM3brlEazVc6CZJ .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-ldM3brlEazVc6CZJ .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-ldM3brlEazVc6CZJ .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-ldM3brlEazVc6CZJ .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-ldM3brlEazVc6CZJ .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-ldM3brlEazVc6CZJ .section{stroke:none;opacity:0.2}#mermaid-svg-ldM3brlEazVc6CZJ .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-ldM3brlEazVc6CZJ .section2{fill:#fff400}#mermaid-svg-ldM3brlEazVc6CZJ .section1,#mermaid-svg-ldM3brlEazVc6CZJ .section3{fill:#fff;opacity:0.2}#mermaid-svg-ldM3brlEazVc6CZJ .sectionTitle0{fill:#333}#mermaid-svg-ldM3brlEazVc6CZJ .sectionTitle1{fill:#333}#mermaid-svg-ldM3brlEazVc6CZJ .sectionTitle2{fill:#333}#mermaid-svg-ldM3brlEazVc6CZJ .sectionTitle3{fill:#333}#mermaid-svg-ldM3brlEazVc6CZJ .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-ldM3brlEazVc6CZJ .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-ldM3brlEazVc6CZJ .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-ldM3brlEazVc6CZJ .grid path{stroke-width:0}#mermaid-svg-ldM3brlEazVc6CZJ .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-ldM3brlEazVc6CZJ .task{stroke-width:2}#mermaid-svg-ldM3brlEazVc6CZJ .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-ldM3brlEazVc6CZJ .taskText:not([font-size]){font-size:11px}#mermaid-svg-ldM3brlEazVc6CZJ .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-ldM3brlEazVc6CZJ .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-ldM3brlEazVc6CZJ .task.clickable{cursor:pointer}#mermaid-svg-ldM3brlEazVc6CZJ .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-ldM3brlEazVc6CZJ .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-ldM3brlEazVc6CZJ .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-ldM3brlEazVc6CZJ .taskText0,#mermaid-svg-ldM3brlEazVc6CZJ .taskText1,#mermaid-svg-ldM3brlEazVc6CZJ .taskText2,#mermaid-svg-ldM3brlEazVc6CZJ .taskText3{fill:#fff}#mermaid-svg-ldM3brlEazVc6CZJ .task0,#mermaid-svg-ldM3brlEazVc6CZJ .task1,#mermaid-svg-ldM3brlEazVc6CZJ .task2,#mermaid-svg-ldM3brlEazVc6CZJ .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-ldM3brlEazVc6CZJ .taskTextOutside0,#mermaid-svg-ldM3brlEazVc6CZJ .taskTextOutside2{fill:#000}#mermaid-svg-ldM3brlEazVc6CZJ .taskTextOutside1,#mermaid-svg-ldM3brlEazVc6CZJ .taskTextOutside3{fill:#000}#mermaid-svg-ldM3brlEazVc6CZJ .active0,#mermaid-svg-ldM3brlEazVc6CZJ .active1,#mermaid-svg-ldM3brlEazVc6CZJ .active2,#mermaid-svg-ldM3brlEazVc6CZJ .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-ldM3brlEazVc6CZJ .activeText0,#mermaid-svg-ldM3brlEazVc6CZJ .activeText1,#mermaid-svg-ldM3brlEazVc6CZJ .activeText2,#mermaid-svg-ldM3brlEazVc6CZJ .activeText3{fill:#000 !important}#mermaid-svg-ldM3brlEazVc6CZJ .done0,#mermaid-svg-ldM3brlEazVc6CZJ .done1,#mermaid-svg-ldM3brlEazVc6CZJ .done2,#mermaid-svg-ldM3brlEazVc6CZJ .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-ldM3brlEazVc6CZJ .doneText0,#mermaid-svg-ldM3brlEazVc6CZJ .doneText1,#mermaid-svg-ldM3brlEazVc6CZJ .doneText2,#mermaid-svg-ldM3brlEazVc6CZJ .doneText3{fill:#000 !important}#mermaid-svg-ldM3brlEazVc6CZJ .crit0,#mermaid-svg-ldM3brlEazVc6CZJ .crit1,#mermaid-svg-ldM3brlEazVc6CZJ .crit2,#mermaid-svg-ldM3brlEazVc6CZJ .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-ldM3brlEazVc6CZJ .activeCrit0,#mermaid-svg-ldM3brlEazVc6CZJ .activeCrit1,#mermaid-svg-ldM3brlEazVc6CZJ .activeCrit2,#mermaid-svg-ldM3brlEazVc6CZJ .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-ldM3brlEazVc6CZJ .doneCrit0,#mermaid-svg-ldM3brlEazVc6CZJ .doneCrit1,#mermaid-svg-ldM3brlEazVc6CZJ .doneCrit2,#mermaid-svg-ldM3brlEazVc6CZJ .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-ldM3brlEazVc6CZJ .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-ldM3brlEazVc6CZJ .milestoneText{font-style:italic}#mermaid-svg-ldM3brlEazVc6CZJ .doneCritText0,#mermaid-svg-ldM3brlEazVc6CZJ .doneCritText1,#mermaid-svg-ldM3brlEazVc6CZJ .doneCritText2,#mermaid-svg-ldM3brlEazVc6CZJ .doneCritText3{fill:#000 !important}#mermaid-svg-ldM3brlEazVc6CZJ .activeCritText0,#mermaid-svg-ldM3brlEazVc6CZJ .activeCritText1,#mermaid-svg-ldM3brlEazVc6CZJ .activeCritText2,#mermaid-svg-ldM3brlEazVc6CZJ .activeCritText3{fill:#000 !important}#mermaid-svg-ldM3brlEazVc6CZJ .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-ldM3brlEazVc6CZJ g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-ldM3brlEazVc6CZJ g.classGroup text .title{font-weight:bolder}#mermaid-svg-ldM3brlEazVc6CZJ g.clickable{cursor:pointer}#mermaid-svg-ldM3brlEazVc6CZJ g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-ldM3brlEazVc6CZJ g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-ldM3brlEazVc6CZJ .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-ldM3brlEazVc6CZJ .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-ldM3brlEazVc6CZJ .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-ldM3brlEazVc6CZJ .dashed-line{stroke-dasharray:3}#mermaid-svg-ldM3brlEazVc6CZJ #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-ldM3brlEazVc6CZJ #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-ldM3brlEazVc6CZJ #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-ldM3brlEazVc6CZJ #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-ldM3brlEazVc6CZJ #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-ldM3brlEazVc6CZJ #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-ldM3brlEazVc6CZJ #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-ldM3brlEazVc6CZJ #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-ldM3brlEazVc6CZJ .commit-id,#mermaid-svg-ldM3brlEazVc6CZJ .commit-msg,#mermaid-svg-ldM3brlEazVc6CZJ .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-ldM3brlEazVc6CZJ .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-ldM3brlEazVc6CZJ .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-ldM3brlEazVc6CZJ g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-ldM3brlEazVc6CZJ g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-ldM3brlEazVc6CZJ g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-ldM3brlEazVc6CZJ g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-ldM3brlEazVc6CZJ g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-ldM3brlEazVc6CZJ g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-ldM3brlEazVc6CZJ .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-ldM3brlEazVc6CZJ .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-ldM3brlEazVc6CZJ .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-ldM3brlEazVc6CZJ .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-ldM3brlEazVc6CZJ .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-ldM3brlEazVc6CZJ .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-ldM3brlEazVc6CZJ .edgeLabel text{fill:#333}#mermaid-svg-ldM3brlEazVc6CZJ .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-ldM3brlEazVc6CZJ .node circle.state-start{fill:black;stroke:black}#mermaid-svg-ldM3brlEazVc6CZJ .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-ldM3brlEazVc6CZJ #statediagram-barbEnd{fill:#9370db}#mermaid-svg-ldM3brlEazVc6CZJ .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-ldM3brlEazVc6CZJ .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-ldM3brlEazVc6CZJ .statediagram-state .divider{stroke:#9370db}#mermaid-svg-ldM3brlEazVc6CZJ .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-ldM3brlEazVc6CZJ .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-ldM3brlEazVc6CZJ .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-ldM3brlEazVc6CZJ .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-ldM3brlEazVc6CZJ .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-ldM3brlEazVc6CZJ .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-ldM3brlEazVc6CZJ .note-edge{stroke-dasharray:5}#mermaid-svg-ldM3brlEazVc6CZJ .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-ldM3brlEazVc6CZJ .error-icon{fill:#522}#mermaid-svg-ldM3brlEazVc6CZJ .error-text{fill:#522;stroke:#522}#mermaid-svg-ldM3brlEazVc6CZJ .edge-thickness-normal{stroke-width:2px}#mermaid-svg-ldM3brlEazVc6CZJ .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-ldM3brlEazVc6CZJ .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-ldM3brlEazVc6CZJ .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-ldM3brlEazVc6CZJ .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-ldM3brlEazVc6CZJ .marker{fill:#333}#mermaid-svg-ldM3brlEazVc6CZJ .marker.cross{stroke:#333}:root { --mermaid-font-family: "trebuchet ms", verdana, arial;}#mermaid-svg-ldM3brlEazVc6CZJ {color: rgba(0, 0, 0, 0.75);font: ;}

简 介
目 录
Contents
样例代码
读入图像
尺寸变化
指定宽高
指定比例
插值方法
总 结
图片尺寸变化
网络演示程序

本文翻译自 Image Resizing with OpenCV中对事OpenCV中对于图像进行尺寸变换。

§00 简  介

  下面我们来谈谈如何通过OpenCV来对图片进行尺寸变化。 所谓的图片尺寸变化,就是沿着不同的坐标轴(也就是图片的高、宽)进行收缩,通过指定收缩尺度比例因子或者直接指定结果图片的尺寸(高、宽)。

  在进行图片尺寸变换的时候:

  • 如果你想保持图片不变形,维持相同的纵横比,则需要注意原始图像的尺寸比例(也就是宽度比上高度的数值)。
  • 在降低图片尺寸的时候,需要对于是图像进行重采样。
  • 增加图片的尺寸,则要求对图像进行重构,这意味着需要通过插值来获得增加像素点的数值。

  不同的插值方法用于完成这些操作。OpenCV也提供了部分这些方法,针对不同的应用可以选择合适的插值方法。

0.1 样例代码

  下面的代码通过指定对应的高、宽来对图像 进行拉伸或者压缩。随着你进一步研读代码,我们将进一步讨论是所有不同的尺度因子和插值方法来实现对图像的尺寸变化。

0.1.1 Python代码

# let's start with the Imports
import cv2
import numpy as np# Read the image using imread function
image = cv2.imread('image.jpg')
cv2.imshow('Original Image', image)# let's downscale the image using new  width and height
down_width = 300
down_height = 200
down_points = (down_width, down_height)
resized_down = cv2.resize(image, down_points, interpolation= cv2.INTER_LINEAR)# let's upscale the image using new  width and height
up_width = 600
up_height = 400
up_points = (up_width, up_height)
resized_up = cv2.resize(image, up_points, interpolation= cv2.INTER_LINEAR)# Display images
cv2.imshow('Resized Down by defining height and width', resized_down)
cv2.waitKey()
cv2.imshow('Resized Up image by defining height and width', resized_up)
cv2.waitKey()#press any key to close the windows
cv2.destroyAllWindows()

0.1.2 C语言代码

// let's start with including libraries
#include<opencv2/opencv.hpp>
#include<iostream>// Namespace to nullify use of cv::function(); syntax
using namespace std;
using namespace cv;int main()
{// Read the image using imread functionMat image = imread("image.jpg");imshow("Original Image", image);// let's downscale the image using new  width and heightint down_width = 300;int down_height = 200;Mat resized_down;//resize downresize(image, resized_down, Size(down_width, down_height), INTER_LINEAR);// let's upscale the image using new  width and heightint up_width = 600;int up_height = 400;Mat resized_up;//resize upresize(image, resized_up, Size(up_width, up_height), INTER_LINEAR);// Display Images and press any key to continueimshow("Resized Down by defining height and width", resized_down);waitKey();imshow("Resized Up image by defining height and width", resized_up);waitKey();destroyAllWindows();return 0;
}

§01 读入图像


  首先导入所需要的软件包。

  • Python
# Importing the libraries
import cv2
import numpy as np
  • C++
#include<opencv2/opencv.hpp>
#include<iostream>
// Namespace to nullify use of cv::function(); syntax
using namespace std;
using namespace cv;

  下面这两漂亮的跑车就是下面代码测试中所使用的图片。

▲ 图1.1 用于实验的图片:一辆漂亮的跑车

  • Python
# Reading image
image= cv2.imread('image.jpg')
  • C++
// Reading imageMat image = imread("image.jpg");

  请注意在上面的C++代码片段中,你需要先创先一个图像的矩阵实例,然后通过 imread() 函数读取图片。

  在你开始对图片进行尺寸变化之前,你需要先了解他的原始尺寸。通过以下方式方式可以获得图片的尺寸:

  • 使用 python中的 shape方法;
  • 在C++中通过 rows 和 cols 获得;

  image.shape是在Python中可以 获得对应的图像的高、宽以及颜色通道的数量。

  在C++中:

  • image.rows给出的图片的高;
  • image.columns 给出了图片的宽度;

  也可通过size()函数获得上述结果:

  • image.size().width:返回宽度;

  • image.size().height返回高度是数值。

  • Python

# Get original height and width
h,w,c = image.shape
print("Original Height and Width:", h,"x", w)
  • C++
// Get height and width
cout << "Original Height and Width :" << image.rows << "x" << image.cols << endl;

  一个重要事情需要记住,在OpenCV中输出的图片的形状为:heightwidthchannels的格式,但一些其他的上T片处理库则使用 width*height的格式。因此需要注意这方面的逻辑差异。

  OpenCV读取图片之后,在内存使用NumPy矩阵存储图片数据,所以一般情况下,你总可以通过矩阵的形状来确定图片的尺寸,也就是通过 rowscolumns(rows代表图片的高度,columns代表图片的宽度)。 因此,计时通过OpenCV过得他们的 形状,NumpPy矩阵法则在此也起作用。你所得到的图片数据形状为 heightwidth*channels。

§02 尺寸变化


  下面让我们看看 OpenCV中的尺寸变化函数 resize() 的调用语法。 可以注意到它只要两个必须输入参数,其他参数都可以省略。

  1. 源图像;
  2. 所需要变化后的图像尺寸, dsize

  下面是resize的调用格式,后面给出了参数定义。

resize(src, dsize[, dst[, fx[, fy[, interpolation]]]])
  • rc: It is the required input image, it could be a string with the path of the input image (eg: ‘test_image.png’).
  • dsize: It is the desired size of the output image, it can be a new height and width.
  • fx: Scale factor along the horizontal axis.
  • fy: Scale factor along the vertical axis.
  • interpolation: It gives us the option of different methods of resizing the image.

§03 指定宽高


  下面的例子是通过指定新的图像的宽高来对图像进行缩小。在下面的代码中:

  • 我们设置结果图像的宽为300, 高为200;

  • 这两个数字使用2D向量表示,这是resize() 函数要求的;

  • 我们同样指明插值的方法,这也是缺省的参数;

  • Python

# Set rows and columns
# lets downsize the image using new  width and height
down_width = 300
down_height = 200
down_points = (down_width, down_height)
resize_down = cv2.resize(image, down_points, interpolation= cv2.INTER_LINEAR)
  • C++
// Set rows and columns
// lets downsize the image using new width and heightint down_width = 300;int down_height = 200;Mat resize_down;// resize downresize(image, resize_down, Size(down_width, down_height), INTER_LINEAR);

  下面我们使用另外的尺寸来变换图像。

  • Python
# Set rows and columns
up_width = 600
up_height = 400
up_points = (up_width, up_height)
# resize the image
resized_up = cv2.resize(image, up_points, interpolation = cv2.INTER_LINEAR)
  • C++
// Set rows and columns
int up_width = 600;
int up_height = 400;
Mat resized_up;
//resize up
resize(image, resized_up, Size(up_width, up_height), INTER_LINEAR);

  在上面Python代码片段中,我们通过定义型的宽、高来拉伸原始图像,这都是通过 resize() 这个函数,处理过程与前面缩小图像的过程是一样的。

  在C++的代码片段中:

  • 我们通过定出了新的宽、高整形数来拉伸图像;
  • 获得了输出图的矩阵;
  • 使用相同的 resize() 函数进行上述操作;

  现在,我们通过OpenCV中的imshow()来显示变化后的图像。

  • Python
# Display images
cv2.imshow('Resized Down by defining height and width', resized_down)
cv2.waitKey()
cv2.imshow('Resized Up image by defining height and width', resized_up)
cv2.waitKey()
cv2.destroyAllWindows()
  • C++
// Display Images and press any key to continue
imshow("Resized Down by defining height and width", resized_down);
waitKey();
imshow("Resized Up image by defining height and width", resized_up);
waitKey();
destroyAllWindows();

▲ 图3.1 尺寸变化后的两个图像:左:缩小尺寸;右:拉伸尺寸

  上面图像中,左边的图像是缩小的图片,右边的图像是拉伸后的图片。

  正如我们期望的那样,我们通过resize改变了图像的尺寸,图像可以增大,也可以缩小,这都是根据给定的高和宽参数来决定的。

  但我们可以感觉得,通过这样指明高和宽参数来改变图像的大小,对于图像来说好像出现了变形,也就是图像的宽高比发生了改变,也就是图像的纵横比例发生的改变。

  怎么纠正这个问题呢。好的,下面我们可以通过指定比例因子来进行尺寸变换。

§04 指定比例


  OK,下面我们通过指定比例因子来改变图片的尺寸。不过首先需要了解什么事比例因子。

  比例因子,也称尺度因子也就是一个数字,用于乘以,或者按比例改变某些数值。在我们对图片处理中,也就是将该数字乘以图像的宽、高。这样可以保持图像的本身的纵横比例不发生改变,进而保持图像的质量。因此图像在你进行拉伸或者压缩的过程中也不会显得发生了变形。

  • Python
# Scaling Up the image 1.2 times by specifying both scaling factors
scale_up_x = 1.2
scale_up_y = 1.2
# Scaling Down the image 0.6 times specifying a single scale factor.
scale_down = 0.6scaled_f_down = cv2.resize(image, None, fx= scale_down, fy= scale_down, interpolation= cv2.INTER_LINEAR)
scaled_f_up = cv2.resize(image, None, fx= scale_up_x, fy= scale_up_y, interpolation= cv2.INTER_LINEAR)
  • C++
// Scaling Up the image 1.2 times by specifying both scaling factors
double scale_up_x = 1.2;
double scale_up_y = 1.2;
// Scaling Down the image 0.6 times specifying a single scale factor.
double scale_down = 0.6;
Mat scaled_f_up, scaled_f_down;
//resize
resize(image,scaled_f_down, Size(), scale_down, scale_down, INTER_LINEAR);
resize(image, scaled_f_up, Size(), scale_up_x, scale_up_y, INTER_LINEAR);

  在上面的Python代码片段中:

  • 我们定义了新的沿着水平和垂直方向上的比例因子;
  • 通过比例因子,省略了指明宽和高的像素个数,所以对应的dsize为 None;

  在上面的C++代码片段中:

  • 通过指明型的比例因子来改变图像数据矩阵;
  • 所以也不需要额外指明矩阵的高和宽,对应的Size()是一个空向量;

  下面通过显示结果图像可以更好地理解变换的结果。

  • Python
# Display images and press any key to check next image
cv2.imshow('Resized Down by defining scaling factor', scaled_f_down)
cv2.waitKey()
cv2.imshow('Resized Up image by defining scaling factor', scaled_f_up)
cv2.waitKey()
  • C++
// Display images and Press any key to continue check next image
imshow("Resized Down by defining scaling factor", scaled_f_down);
waitKey();
imshow("Resized Up by defining scaling factor", scaled_f_up);
waitKey();

▲ 图4.1 通过指明比例因子对图像进行拉伸和缩小的结果

  左边的图像是缩小的图片,右边则是拉伸的图片。

§05 插值方法


  下面讨论一下不同的插值方法用于图片的尺寸变化。

  下面给出了不同的插值方法选项所对应的变化方法。

  • INTER_AREA: INTER_AREA uses pixel area relation for resampling. This is best suited for reducing the size of an image (shrinking). When used for zooming into the image, it uses the INTER_NEAREST method.
  • INTER_CUBIC: This uses bicubic interpolation for resizing the image. While resizing and interpolating new pixels, this method acts on the 4×4 neighboring pixels of the image. It then takes the weights average of the 16 pixels to create the new interpolated pixel.
  • INTER_LINEAR: This method is somewhat similar to the INTER_CUBIC interpolation. But unlike INTER_CUBIC, this uses 2×2 neighboring pixels to get the weighted average for the interpolated pixel.
  • INTER_NEAREST: The INTER_NEAREST method uses the nearest neighbor concept for interpolation. This is one of the simplest methods, using only one neighboring pixel from the image for interpolation.

  不易担心,如果你不完全理解上述方法的函数,后面还会通过不同的博文来进行展示。

  • Python
# Scaling Down the image 0.6 times using different Interpolation Method
res_inter_nearest = cv2.resize(image, None, fx= scale_down, fy= scale_down, interpolation= cv2.INTER_NEAREST)
res_inter_linear = cv2.resize(image, None, fx= scale_down, fy= scale_down, interpolation= cv2.INTER_LINEAR)
res_inter_area = cv2.resize(image, None, fx= scale_down, fy= scale_down, interpolation= cv2.INTER_AREA)
  • C++
# Scaling Down the image 0.6 using different Interpolation Method
Mat res_inter_linear, res_inter_nearest, res_inter_area;
resize(image, res_inter_linear, Size(), scale_down, scale_down, INTER_LINEAR);
resize(image, res_inter_nearest, Size(), scale_down, scale_down, INTER_NEAREST);
resize(image, res_inter_area, Size(), scale_down, scale_down, INTER_AREA);

  上面Python代码片段中,我们使用了不同的插值方法。类似在C++ d代码中,我们首先定义了输出图片的数据矩阵,让后通过不同的插值方法进行尺度变化。

  下面让我们看看对于的图片结果有什么不同。

  • Python
# Concatenate images in horizontal axis for comparison
vertical= np.concatenate((res_inter_nearest, res_inter_linear, res_inter_area), axis = 0)
# Display the image Press any key to continue
cv2.imshow('Inter Nearest :: Inter Linear :: Inter Area', vertical)
  • C++
Mat a,b,c;
vconcat(res_inter_linear, res_inter_nearest, a);
vconcat(res_inter_area, res_inter_area, b);
vconcat(a, b, c);
// Display the image Press any key to continue
imshow("Inter Linear :: Inter Nearest :: Inter Area :: Inter Area", c);

▲ 图5.1 不同的插值方法对应的图片尺寸变化结果

  在上述图片结果中,左边是通过 INTER_LINEAR进行插值的; 中间是通过 INTER_NEAREST 进行插值的;右边是通过 INTER_AREA进行插值的。

  希望到现在为止你已经对于OpenCV中的 resize() 函数有所了解,我们也展示了不同的方法来改变图像的尺寸。

  下面的动图显示了执行图片尺寸变化过程,通过指明接过图片的高、宽,或者比例因子,以及不同的插值方法来获得相应的尺寸变化的结果。

▲ 图5.2 图相尺寸变化操作过程

※ 总  结 ※


  本博文给你展示了如何通过指定输出结果的宽高来对图像进行尺寸变化。你也了解如何根据比例因子来保持图像的纵横比不发生变化,从而在改变图像尺寸的过程中图像不发生形变。最后也讨论了不同的插值方法如何改变尺寸变化的结果的。

6.1 图片尺寸变化网络演示程序

  现在你已经充分了解了resize 函数的功能,那么可以在web app中进行测试,看看改变图像的尺寸的实际效果。 我们开发了一个小的演示应用程序来展示 resize()DSE功能gsju uiys Streamlit。 你可以通过这个 链接 来访问这个应用程序,利用提供的图片进行操作或者上载你自己的选择的图像。

▲ 图6.1.1 图片尺寸变化网络演示程序


■ 相关文献链接:

  • Image Resizing with OpenCV
  • 链接

● 相关图表链接:

  • 图1.1 用于实验的图片:一辆漂亮的跑车
  • 图3.1 尺寸变化后的两个图像:左:缩小尺寸;右:拉伸尺寸
  • 图4.1 通过指明比例因子对图像进行拉伸和缩小的结果
  • 图5.1 不同的插值方法对应的图片尺寸变化结果
  • 图5.2 图相尺寸变化操作过程
  • 图6.1.1 图片尺寸变化网络演示程序

OpenCV对图像进行尺寸变换相关推荐

  1. 4.3【图像镜像】-------------基于Opencv实现-----图像的镜像变换

     [1]理论知识:         镜像变换又分为水平镜像和垂直镜像,水平镜像即将图像左半部分和右半部分以图像竖直中轴线为中心轴进行兑换,而竖直镜像则是将图像上半部分和下半部分以图像水平中轴线为中 ...

  2. 【OpenCV】图像的特效变换扭曲变换、球形变换、波动变换

    [原创][OpenCV]图像的特效变换扭曲变换.球形变换.波动变换 扭曲变换.球形变换.波动变换 代码如下,每个变换三个结果 什么?你说解释在哪里? 扭曲变换.球形变换.波动变换 话不多说,林忠老师的 ...

  3. 最近邻插值实现:图像任意尺寸变换

    #<opencv2/imgproc/imgproc.hpp> #include <opencv2/core/core.hpp> #include <opencv2/hig ...

  4. matlab 图像读取长宽_计算机视觉学习笔记1 图像读取显示和尺寸变换

    第一天,图像的读取,显示和尺寸变换 c++,python,MATLAB各实现一遍,以这个头骨图像为例吧 c++调用OpenCv实现图像显示和尺寸变换 c++用的编译器为clion,因此需要先配置cma ...

  5. 【OpenCV 例程200篇】33. 图像的复合变换

    [OpenCV 例程200篇]33. 图像的复合变换 欢迎关注 『OpenCV 例程200篇』 系列,持续更新中 欢迎关注 『Python小白的OpenCV学习课』 系列,持续更新中 图像的复合变换是 ...

  6. OpenCV学习笔记(十六)——CamShift研究 OpenCV学习笔记(十七)——运动分析和物体跟踪Video OpenCV学习笔记(十八)——图像的各种变换(cvtColor*+)imgproc

    OpenCV学习笔记(十六)--CamShift研究 CamShitf算法,即Continuously Apative Mean-Shift算法,基本思想就是对视频图像的多帧进行MeanShift运算 ...

  7. (OpenCV)图像目标尺寸检测

      本文翻译自pyimagesearch技术博客上的一篇文章,<Measuring size of objects in an image with OpenCV>,原文作者:Adrian ...

  8. OpenCV学习-图像大小计算方法以及获取图像的尺寸和通道数

    图像大小的计算方法: 即:长X宽X位数 (KB) 关于位数:单通道图像像素值最大为256,也即是2的八次方,所以是8位的,那么彩色图像是三通道的,即24位. 例如 图像的尺寸就是高和宽,对于二维数组( ...

  9. 【OpenCV】—图像金子塔与图片尺寸缩放

    序言: 继续,继续,开干!!! 本节学习OpenCV中的函数pyrUp和pyrDown对图像进行向上和向下采样,以及了解专门用于缩放图像尺寸的resize函数的用法 文章目录 1.引言 2.关于图像金 ...

最新文章

  1. 基于图像的摄像机姿态估计方法评析
  2. Windows Vista Ultimate 版测试结果汇整
  3. maven 笔记,概念
  4. 用开源NAC阻止非法网络访问
  5. 分享一批国内常用的tracker地址
  6. X3D.Studio编辑器界面介绍
  7. BUUOJ reverse SimpleRev (爆破)
  8. Java RandomAccessFile skipBytes()方法与示例
  9. qt xml组包_Qt 利用XML文档,写一个程序集合 二
  10. 注意!微软宣布将停止支持多个版本的 .NET Framework
  11. 【笔记】shell下的主要工具
  12. EPUB阅读工具-读者推荐
  13. ( 教程 ) 微信公众号做淘宝优惠券自动查券返利机器人怎么设置?
  14. JS实现浏览器菜单命令
  15. php最小二乘法,Python中实现最小二乘法思路及实现代码
  16. 华为AR系列路由器密码重置
  17. 网路营销seo站群策略
  18. 燕青分布式系统开发之分布式事务解决方案视频(2天)
  19. 睡眠健康与肠道健康之间的双向联系
  20. python写酒店管理系统报告_酒店管理系统e-r图_酒店系统管理实验报告

热门文章

  1. Apache - AH00526 – server.crt
  2. wordpress 后台,登录,注册开启https的重写规则
  3. 模仿SDWebImage实现异步加载图片
  4. 今天有了自己得博客,心情不错。
  5. 编译安装samba4.89遇到的错误
  6. 留着肯定有用,让你脱胎成技术大神的JAVA开发技巧
  7. Touch Event
  8. 【高效使用】生产力提升
  9. HTML有几种发音模式,html – 使用lang属性和拼音发音的正确方法是什么(如果有的话)?...
  10. python模拟登录qq账号密码_最新的Python模拟登陆QQ脚本,一键批量登录,强行过验证!...