ubuntu22.04@laptop OpenCV Get Started: 003_image_resizing

ubuntu22.04@laptop OpenCV Get Started: 003_image_resizing

  • [1. 源由](#1. 源由)
  • [2. resize应用Demo](#2. resize应用Demo)
  • [3 image_resize](#3 image_resize)
    • [3.1 C++应用Demo](#3.1 C++应用Demo)
    • [3.2 Python应用Demo](#3.2 Python应用Demo)
    • [3.3 重点过程分析](#3.3 重点过程分析)
      • [3.3.1 根据宽高调整大小](#3.3.1 根据宽高调整大小)
      • [3.3.2 根据比例调整大小](#3.3.2 根据比例调整大小)
      • [3.3.3 根据插值方式调整大小](#3.3.3 根据插值方式调整大小)
  • [4. 总结](#4. 总结)
  • [5. 参考资料](#5. 参考资料)

1. 源由

在OpenCV中调整图像大小:

  • 要记住图像的原始纵横比(即宽高比)
  • 减小图像的大小,需要对像素缩减采样值。
  • 增加图像的大小,需要对新像素进行插值。

各种插值技术开始发挥作用来完成上述操作。

2. resize应用Demo

003_image_resizing是OpenCV调整图像比例的示例程序。

确认OpenCV安装路径:

复制代码
$ find /home/daniel/ -name "OpenCVConfig.cmake"
/home/daniel/OpenCV/installation/opencv-4.9.0/lib/cmake/opencv4/
/home/daniel/OpenCV/opencv/build/OpenCVConfig.cmake
/home/daniel/OpenCV/opencv/build/unix-install/OpenCVConfig.cmake


$ export OpenCV_DIR=/home/daniel/OpenCV/installation/opencv-4.9.0/lib/cmake/opencv4/

3 image_resize

3.1 C++应用Demo

C++应用Demo工程结构:

复制代码
003_image_resizing/CPP$ tree .
.
├── CMakeLists.txt
├── image.jpg
└── image_resize.cpp

0 directories, 3 files

C++应用Demo工程编译执行:

复制代码
$ mkdir build
$ cd build
$ cmake ..
$ cmake --build . --config Release
$ cd ..
$ ./build/image_resize

3.2 Python应用Demo

Python应用Demo工程结构:

复制代码
003_image_resizing/Python$ tree .
.
├── image.jpg
├── image_resize.py
└── requirements.txt

0 directories, 3 files

Python应用Demo工程执行:

复制代码
$ workoncv-4.9.0
$ python image_resize.py

3.3 重点过程分析

  1. src: It is the required input image, it could be a string with the path of the input image (eg: 'test_image.png').
  2. dsize: It is the desired size of the output image, it can be a new height and width.
  3. fx: Scale factor along the horizontal axis.
  4. fy: Scale factor along the vertical axis.
  5. interpolation: It gives us the option of different methods of resizing the image.

3.3.1 根据宽高调整大小

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:

复制代码
# 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)

3.3.2 根据比例调整大小

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:

复制代码
# 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.6
 
scaled_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)

3.3.3 根据插值方式调整大小

  • 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.

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:

复制代码
# 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)

4. 总结

  • resize() 将图像源数据,通过参数指定的画面尺寸,进行相应的插值调整。

其他API函数:

  • imshow
  • imwrite
  • vconcat 数据源连接函数

5. 参考资料

【1】ubuntu22.04@laptop OpenCV Get Started

【2】ubuntu22.04@laptop OpenCV安装

【3】ubuntu22.04@laptop OpenCV定制化安装

相关推荐
OAFD.4 小时前
机器学习之线性回归:原理、实现与实践
人工智能·机器学习·线性回归
SHIPKING3936 小时前
【机器学习&深度学习】LMDeploy的分布式推理实现
人工智能·深度学习
mit6.8246 小时前
[RestGPT] docs | RestBench评估 | 配置与环境
人工智能·python
CareyWYR7 小时前
每周AI论文速递(250818-250822)
人工智能
门思科技7 小时前
LoRaWAN 的网络拓扑全解析:架构、原理与应用实践
服务器·网络·人工智能·科技·物联网·架构
兔子的倔强7 小时前
Transformer在文本、图像和点云数据中的应用——经典工作梳理
人工智能·深度学习·transformer
lxmyzzs8 小时前
【图像算法 - 21】慧眼识虫:基于深度学习与OpenCV的农田害虫智能识别系统
人工智能·深度学习·opencv·算法·yolo·目标检测·计算机视觉
Gloria_niki8 小时前
机器学习之K 均值聚类算法
人工智能·机器学习
AI人工智能+8 小时前
表格识别技术:通过图像处理与深度学习,将非结构化表格转化为可编辑结构化数据,推动智能化发展
人工智能·深度学习·ocr·表格识别
深圳多奥智能一卡(码、脸)通系统9 小时前
智能二维码QR\刷IC卡\人脸AI识别梯控系统功能设计需基于模块化架构,整合物联网、生物识别、权限控制等技术,以下是多奥分层次的系统设计框架
人工智能·门禁·电梯门禁·二维码梯控·梯控·电梯