赞
踩
使用OpenCV进行裁剪相当于对NumPy数组做类似切片的动作。
每个图像都存储在2D阵列中,区域的高度和宽度(以像素为单位)就是一张对应的图片。
004_cropping_image是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/
C++应用Demo工程结构:
004_cropping_image/CPP$ tree .
.
├── CMakeLists.txt
├── image_crop.cpp
├── Patching
│ ├── saved_patches
│ ├── CMakeLists.txt
│ ├── patching.cpp
│ └── test_cropped.jpg
└── test.jpg
2 directories, 6 files
C++应用Demo工程编译执行:
$ mkdir build
$ cd build
$ cmake ..
$ cmake --build . --config Release
$ cd ..
$ ./build/image_crop
$ cd Patching
$ mkdir build
$ cd build
$ cmake ..
$ cmake --build . --config Release
$ cd ..
$ ./build/image_patch
Python应用Demo工程结构:
004_cropping_image/Python$ tree .
.
├── saved_patches
├── image_crop.py
├── patching.py
├── requirements.txt
├── test_cropped.jpg
└── test.jpg
1 directory, 5 files
Python应用Demo工程执行:
$ workoncv-4.9.0
$ python image_crop.py
$ python patching.py
第一个参数:行 (高度, 自上而下递增)
第二个参数:列 (宽度, 自左往右递增)
C++:
// Crop image
// First Range(20,500) is for y coordinates and the second is for x respectively
Mat cropped_image = img(Range(80,280), Range(150,330));
Python:
# Cropping an image
cropped_image = img[80:280, 150:330]
C++:
rectangle(img, Point(x,y), Point(x1,y1), Scalar(0,255,0), 1);
Python:
cv2.rectangle(img, (x, y), (x1, y1), (0, 255, 0), 1)
通过对NumPy二维数组操作,对图像进行剪裁,使用rectangle
函数进行矩形框的绘制。
【1】ubuntu22.04@laptop OpenCV Get Started
【2】ubuntu22.04@laptop OpenCV安装
【3】ubuntu22.04@laptop OpenCV定制化安装
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。