🌈write in front🌈
🧸大家好,我是N阶二进制.希望你看完之后,能对你有所帮助,不足请指正!共同学习交流.
🆔本文由N阶二进制原创 CSDN首发🐒 如需转载还请通知⚠️
📝个人主页:N阶二进制🧸—CSDN博客
🎁欢迎各位→点赞👍 + 收藏⭐️ + 留言📝
📣系列专栏:N阶二进制🧸的openssl学习系列专栏——CSDN博客
一、opencv 图片读取
OpenCV 的 imread()
函数可以支持多种图像格式,包括但不限于:
1.1 opencv检查支持的图像读取格式
-
方法一:
通过获取build的信息查看支持media段中支持那些#include <opencv2/opencv.hpp> #include <iostream> int main() { // 获取OpenCV的构建信息 std::string buildInfo = cv::getBuildInformation(); // 打印构建信息,其中包含了支持的图像格式 std::cout << "OpenCV Build Information:" << std::endl; std::cout << buildInfo << std::endl; return 0; }
在统信专业版上输出内容如下:
OpenCV Build Information:
General configuration for OpenCV 3.2.0 =====================================
Version control: unknown
Extra modules:
Location (extra): /build/opencv-3.2.0+dfsg/contrib/modules
Version control (extra): unknown
Platform:
Timestamp: 2023-08-03T09:48:04Z
Host: Linux 4.19.0-server-amd64 x86_64
CMake: 3.13.4
CMake generator: Unix Makefiles
CMake build tool: /usr/bin/make
Configuration: Release
C/C++:
Built as dynamic libs?: YES
C++ Compiler: /usr/bin/c++ (ver 8.3.0)
C++ flags (Release): -g -O2 -fdebug-prefix-map=/build/opencv-3.2.0+dfsg=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -msse -msse2 -mno-avx -mno-sse3 -mno-ssse3 -mno-sse4.1 -mno-sse4.2 -ffunction-sections -fvisibility=hidden -fvisibility-inlines-hidden -g -O2 -fdebug-prefix-map=/build/opencv-3.2.0+dfsg=. -fstack-protector-strong -Wformat -Werror=format-security -DNDEBUG
C++ flags (Debug): -g -O2 -fdebug-prefix-map=/build/opencv-3.2.0+dfsg=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -msse -msse2 -mno-avx -mno-sse3 -mno-ssse3 -mno-sse4.1 -mno-sse4.2 -ffunction-sections -fvisibility=hidden -fvisibility-inlines-hidden -g -O0 -DDEBUG -D_DEBUG
C Compiler: /usr/bin/cc
C flags (Release): -g -O2 -fdebug-prefix-map=/build/opencv-3.2.0+dfsg=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wno-narrowing -Wno-comment -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -msse -msse2 -mno-avx -mno-sse3 -mno-ssse3 -mno-sse4.1 -mno-sse4.2 -ffunction-sections -fvisibility=hidden -g -O2 -fdebug-prefix-map=/build/opencv-3.2.0+dfsg=. -fstack-protector-strong -Wformat -Werror=format-security -DNDEBUG
C flags (Debug): -g -O2 -fdebug-prefix-map=/build/opencv-3.2.0+dfsg=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wno-narrowing -Wno-comment -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -msse -msse2 -mno-avx -mno-sse3 -mno-ssse3 -mno-sse4.1 -mno-sse4.2 -ffunction-sections -fvisibility=hidden -g -O0 -DDEBUG -D_DEBUG
Linker flags (Release): -Wl,-z,relro -Wl,-z,now -Wl,-z,relro -Wl,-z,now
Linker flags (Debug): -Wl,-z,relro -Wl,-z,now
ccache: NO
Precompiled headers: NO
Extra dependencies: /usr/lib/x86_64-linux-gnu/libwebp.so gdcmMSFF /usr/lib/x86_64-linux-gnu/libImath.so /usr/lib/x86_64-linux-gnu/libIlmImf.so /usr/lib/x86_64-linux-gnu/libIex.so /usr/lib/x86_64-linux-gnu/libHalf.so /usr/lib/x86_64-linux-gnu/libIlmThread.so /usr/lib/libgdal.so gtk-3 gdk-3 pangocairo-1.0 pango-1.0 atk-1.0 cairo-gobject cairo gdk_pixbuf-2.0 gio-2.0 gobject-2.0 glib-2.0 gthread-2.0 dc1394 avcodec avformat avutil swscale avresample gphoto2 gphoto2_port /usr/lib/x86_64-linux-gnu/hdf5/serial/libhdf5.so /usr/lib/x86_64-linux-gnu/libpthread.so /usr/lib/x86_64-linux-gnu/libsz.so /usr/lib/x86_64-linux-gnu/libdl.so /usr/lib/x86_64-linux-gnu/libm.so vtkRenderingOpenGL vtkImagingHybrid vtkIOImage vtkCommonDataModel vtkCommonMath vtkCommonCore vtksys vtkCommonMisc vtkCommonSystem vtkCommonTransforms vtkCommonExecutionModel vtkDICOMParser vtkIOCore /usr/lib/x86_64-linux-gnu/libz.so vtkmetaio /usr/lib/x86_64-linux-gnu/libjpeg.so /usr/lib/x86_64-linux-gnu/libpng.so /usr/lib/x86_64-linux-gnu/libtiff.so vtkImagingCore vtkRenderingCore vtkCommonColor vtkFiltersExtraction vtkFiltersCore vtkFiltersGeneral vtkCommonComputationalGeometry vtkFiltersStatistics vtkImagingFourier vtkalglib vtkFiltersGeometry vtkFiltersSources vtkInteractionStyle vtkRenderingLOD vtkFiltersModeling vtkIOPLY vtkIOGeometry vtkFiltersTexture vtkRenderingFreeType /usr/lib/x86_64-linux-gnu/libfreetype.so vtkftgl vtkIOExport vtkRenderingAnnotation vtkImagingColor vtkRenderingContext2D vtkRenderingGL2PS vtkRenderingContextOpenGL /usr/lib/x86_64-linux-gnu/libgl2ps.so vtkRenderingLabel tesseract lept dl m pthread rt tbb
3rdparty dependencies:
OpenCV modules:
To be built: core flann hdf imgproc ml photo reg surface_matching video viz freetype fuzzy imgcodecs shape videoio highgui objdetect plot superres ts xobjdetect xphoto bgsegm bioinspired dpm face features2d line_descriptor saliency text calib3d ccalib datasets rgbd stereo stitching videostab ximgproc aruco optflow phase_unwrapping structured_light java python2 python3
Disabled: world contrib_world
Disabled by dependency: tracking
Unavailable: cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev cnn_3dobj cvv dnn matlab sfm
GUI:
QT: NO
GTK+ 3.x: YES (ver 3.24.5)
GThread : YES (ver 2.58.3)
GtkGlExt: NO
OpenGL support: NO
VTK support: YES (ver 6.3.0)
Media I/O:
ZLib: /usr/lib/x86_64-linux-gnu/libz.so (ver 1.2.11)
JPEG: /usr/lib/x86_64-linux-gnu/libjpeg.so (ver )
WEBP: /usr/lib/x86_64-linux-gnu/libwebp.so (ver encoder: 0x020e)
PNG: /usr/lib/x86_64-linux-gnu/libpng.so (ver 1.6.36)
TIFF: /usr/lib/x86_64-linux-gnu/libtiff.so (ver 42 - 4.1.0)
JPEG 2000: NO
OpenEXR: /usr/lib/x86_64-linux-gnu/libImath.so /usr/lib/x86_64-linux-gnu/libIlmImf.so /usr/lib/x86_64-linux-gnu/libIex.so /usr/lib/x86_64-linux-gnu/libHalf.so /usr/lib/x86_64-linux-gnu/libIlmThread.so (ver 2.2.1)
GDAL: /usr/lib/libgdal.so
GDCM: YES (ver 2.8.8)
Video I/O:
DC1394 1.x: NO
DC1394 2.x: YES (ver 2.2.5)
FFMPEG: YES
avcodec: YES (ver 58.35.100)
avformat: YES (ver 58.20.100)
avutil: YES (ver 56.22.100)
swscale: YES (ver 5.3.100)
avresample: YES (ver 4.0.0)
GStreamer: NO
OpenNI: NO
OpenNI PrimeSensor Modules: NO
OpenNI2: NO
PvAPI: NO
GigEVisionSDK: NO
Aravis SDK: NO
UniCap: NO
UniCap ucil: NO
V4L/V4L2: NO/YES
XIMEA: NO
Xine: NO
gPhoto2: YES
Parallel framework: TBB (ver 2018.0 interface 10006)
Other third-party libraries:
Use IPP: NO
Use IPP Async: NO
Use VA: NO
Use Intel VA-API/OpenCL: NO
Use Lapack: NO
Use Eigen: YES (ver 3.3.7)
Use Cuda: NO
Use OpenCL: YES
Use OpenVX: NO
Use custom HAL: NO
OpenCL: <Dynamic loading of OpenCL library>
Include path: /usr/include/CL
Use AMDFFT: NO
Use AMDBLAS: NO
Python 2:
Interpreter: /usr/bin/python2.7 (ver 2.7.16)
Libraries: /usr/lib/x86_64-linux-gnu/libpython2.7.so (ver 2.7.16)
numpy: /usr/lib/python2.7/dist-packages/numpy/core/include (ver 1.16.2)
packages path: lib/python2.7/dist-packages
Python 3:
Interpreter: /usr/bin/python3 (ver 3.7.3)
Libraries: /usr/lib/x86_64-linux-gnu/libpython3.7m.so (ver 3.7.3)
numpy: /usr/lib/python3/dist-packages/numpy/core/include (ver 1.16.2)
packages path: lib/python3.7/dist-packages
Python (for build): /usr/bin/python2.7
Java:
ant: /bin/ant (ver 1.10.5)
JNI: /usr/lib/jvm/default-java/include /usr/lib/jvm/default-java/include/linux /usr/lib/jvm/default-java/include
Java wrappers: YES
Java tests: NO
Matlab: Matlab not found or implicitly disabled
Documentation:
Doxygen: /usr/bin/doxygen (ver 1.8.13)
Tests and samples:
Tests: NO
Performance tests: YES
C/C++ Examples: YES
Install path: /usr
cvconfig.h is in: /build/opencv-3.2.0+dfsg/obj-x86_64-linux-gnu
-----------------------------------------------------------------
1.2 常见图像格式
- JPEG(
.jpg
、.jpeg
) - PNG(
.png
) - BMP(
.bmp
) - TIFF(
.tiff
、.tif
) - GIF(
.gif
) - WebP(
.webp
)
1.3 其他图像格式
- HDR(High Dynamic Range)图像(
.hdr
) - OpenEXR 图像(
.exr
) - PFM(Portable Float Map)图像(
.pfm
) - Radiance RGBE 图像(
.hdr
)
在 OpenCV 中,imread()
函数支持读取这些常见格式的图像。例如:
cv::Mat image = cv::imread("example.jpg"); // 读取JPEG格式的图像
请注意,不同的平台和操作系统可能对支持的图像格式有所差异,因此在特定的开发环境中,你需要确保你的图像文件是 OpenCV 支持的格式。如果尝试读取不支持的格式,imread()
会返回一个空的 cv::Mat
对象。你可以通过检查返回的 cv::Mat
对象是否为空来判断图像是否成功读取。
cv::Mat image = cv::imread("example.bmp");
if (image.empty()) {
std::cerr << "Could not open or find the image!" << std::endl;
return -1;
}
在使用 imread()
函数时,确保提供的文件路径是正确的,文件存在且格式是 OpenCV 支持的。
二、opencv读取图片案例
2.1 opencv读取jpg
在OpenCV中,你可以使用cv::imread()
函数来读取JPEG格式的图像文件。以下是一个基本的示例代码:
#include <opencv2/opencv.hpp>
#include <iostream>
int main() {
// 读取JPEG格式的图像
cv::Mat image = cv::imread("1.jpg");
// 检查图像是否成功读取
if (image.empty()) {
std::cerr << "Could not open or find the image!" << std::endl;
return -1;
}
// 显示图像
cv::imshow("Image", image);
// 等待用户按下任意键
cv::waitKey(0);
return 0;
}
在这个示例中,cv::imread("example.jpg")
会尝试读取名为 “example.jpg” 的JPEG格式图像文件。如果成功读取,它将把图像数据加载到cv::Mat
对象中。如果读取失败,它将在控制台输出错误消息。
请确保在编译时链接了OpenCV库,并且程序的当前工作目录中存在名为 “example.jpg” 的JPEG格式图像文件。如果你希望读取其他路径的图像,需要在imread()
函数中传入完整的文件路径。
2.2 opencv读取png
与读取JPEG格式图像类似,OpenCV也提供了读取PNG格式图像的功能。你可以使用cv::imread()
函数来读取PNG格式的图像文件。以下是一个基本的示例代码:
#include <opencv2/opencv.hpp>
#include <iostream>
int main() {
// 读取PNG格式的图像
cv::Mat image = cv::imread("war3.png");
// 检查图像是否成功读取
if (image.empty()) {
std::cerr << "Could not open or find the image!" << std::endl;
return -1;
}
// 显示图像
cv::imshow("Image", image);
// 等待用户按下任意键
cv::waitKey(0);
return 0;
}
在这个示例中,cv::imread("example.png")
会尝试读取名为 “example.png” 的PNG格式图像文件。如果成功读取,它将把图像数据加载到cv::Mat
对象中。如果读取失败,它将在控制台输出错误消息。
请确保在编译时链接了OpenCV库,并且程序的当前工作目录中存在名为 “example.png” 的PNG格式图像文件。如果你希望读取其他路径的图像,需要在imread()
函数中传入完整的文件路径。
2.3 opencv读取tiff
OpenCV 提供了读取TIFF格式图像的功能,你可以使用 cv::imread()
函数来读取TIFF格式的图像文件。以下是一个基本的示例代码:
#include <opencv2/opencv.hpp>
#include <iostream>
int main() {
// 读取TIFF格式的图像
cv::Mat image = cv::imread("example.tiff", cv::IMREAD_UNCHANGED);
// 检查图像是否成功读取
if (image.empty()) {
std::cerr << "Could not open or find the image!" << std::endl;
return -1;
}
// 显示图像
cv::imshow("Image", image);
// 等待用户按下任意键
cv::waitKey(0);
return 0;
}
在这个示例中,cv::imread("example.tiff", cv::IMREAD_UNCHANGED)
会尝试读取名为 “example.tiff” 的TIFF格式图像文件。cv::IMREAD_UNCHANGED
参数表示图像应该以原始的通道数和深度加载。如果成功读取,它将把图像数据加载到 cv::Mat
对象中。如果读取失败,它将在控制台输出错误消息。
请确保在编译时链接了OpenCV库,并且程序的当前工作目录中存在名为 “example.tiff” 的TIFF格式图像文件。如果你希望读取其他路径的图像,需要在 imread()
函数中传入完整的文件路径。