OpenCV 笔记 图像与视频的读写
文章目录
读取图片
imread方法
Mat imread(const String & filename ,int flags=IMREAD_COLOR);
imread读取图像,返回Mat对象,两个参数,第一个是文件名,支持位图bmp、dib,JPEG图像,PNG,webp,pbm,pgm,ppm,TIFF等多种图像,读取失败则返回空矩阵
第二个读取方式,默认值是1 flag = 1 返回三通道彩色图 flag = 0 灰度图 flag = -1 原图带alpha通道
如果返回三通道,编码顺序是BGR。支持读取的图像格式如下
Currently, the following file formats are supported:
- Windows bitmaps - *.bmp, *.dib (always supported)
- JPEG files - *.jpeg, *.jpg, *.jpe (see the Note section)
- JPEG 2000 files - *.jp2 (see the Note section)
- Portable Network Graphics - *.png (see the Note section)
- WebP - *.webp (see the Note section)
- Portable image format - *.pbm, *.pgm, *.ppm *.pxm, *.pnm (always supported)
- Sun rasters - *.sr, *.ras (always supported)
- TIFF files - *.tiff, *.tif (see the Note section)
- OpenEXR Image files - *.exr (see the Note section)
- Radiance HDR - *.hdr, *.pic (always supported)
- Raster and Vector geospatial data supported by GDAL (see the Note section)
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
int main(int argc, char **argv) {
// 图片路径 可以是/或者//或者\\或者 /、//、\\混合;但不能是单独的反斜杠 \
Mat src = imread("C:/Users/muyi/Pictures/pic/5503.jpg");
imshow("input", src);
waitKey(0);
destroyAllWindows();
return 0;
}
import cv2
img = cv2.imread(filename,flags) # 返回array矩阵,读取失败则是nonetype
图片读取出错处理
文件损坏、不存在、权限错误等问题导致读取图片失败,程序不会报错,但是返回的是空矩阵,不处理后续使用可能会出错。
Mat src = imread("test.png");
if (src.empty()) {
printf("could not load image...\n");
return -1;
}
src = cv2.imread("test.png")
# 断言判断读取是否成功
assert type(src)==None,"load image error"
# 或者if语句判断
if src.all() == None: #
print("load image error")
# 或者 try,自定义异常
def read_img(path):
img = cv2.imread(path)
if img == None:
raise Exception("load image error")
return img
try :
read_img('eee.png')
except Exception as err:
print(err)
else:
后续处理
读取的图片属性
C++版本,读取图像以Mat对象形式存储,Python版本以np.array形式存储
Mat img = imread("C:\Users\muyi\Pictures\pic\5503.jpg");
// Mat类的部分属性
cout << "dims:" << img.dims << endl; // 矩阵的维度
cout << "rows:" << img.rows << endl; // 矩阵的行数
cout << "cols:" << img.cols << endl; // 矩阵列数
cout << "channels:" << img.channels() << endl; // 图像通道数
cout << "type:" << img.type() << endl; // 表示了矩阵中元素的类型以及矩阵的通道个数
cout << "depth:" << img.depth() << endl; // 深度
cout << "elemSize:" << img.elemSize() << endl; // 矩阵一个元素占用的字节数
cout << "elemSize1:" << img.elemSize1() << endl; //矩阵元素一个通道占用的字节数= elemSize / channels
// type为 CV_16SC3,那么elemSize = 3 * 16 / 8 = 6 bytes
图像矩阵的type取值:参考博客
它是一系列的预定义的常量,其命名规则为CV_(位数)+(数据类型)+(通道数)U(unsigned integer)表示的是无符号整数,S(signed integer)是有符号整数,F(float)是浮点数。 C1,C2,C3,C4则表示通道是1,2,3,4
type一般是在创建Mat对象时设定,如果要取得Mat的元素类型,则无需使用type,使用depth
depth 矩阵中元素的一个通道的数据类型,这个值和type是相关的。例如 type为 CV_16SC2,一个2通道的16位的有符号整数。那么,depth则是CV_16S。depth也是一系列的预定义值,
将type的预定义值去掉通道信息就是depth值: CV_8U CV_8S CV_16U CV_16S CV_32S CV_32F CV_64F
表1. Mat对象type的取值表
CV_8UC1 | CV_8UC2 | CV_8UC3 | CV_8UC4 |
---|---|---|---|
CV_8SC1 | CV_8SC2 | CV_8SC3 | CV_8SC4 |
CV_16UC1 | CV_16UC2 | CV_16UC3 | CV_16UC4 |
CV_16SC1 | CV_16SC2 | CV_16SC3 | CV_16SC4 |
CV_32SC1 | CV_32SC2 | CV_32SC3 | CV_32SC4 |
CV_32FC1 | CV_32FC2 | CV_32FC3 | CV_32FC4 |
CV_64FC1 | CV_64FC2 | CV_64FC3 | CV_64FC4 |
Mat img(3, 4, CV_16UC4, Scalar_<uchar>(1, 2, 3, 4));//3X4的矩阵 16位无符号4通道
//Scalar_是一个模板向量,用来初始化矩阵的每个像素,因为矩阵具有4个通道,Scalar_有四个值。
python版本cv图像格式,type(img) = numpy.ndarray 因此,它具有array的一切属性和方法,而不同于c++版本中Mat的deepth()、type()等写法。
img = cv2.imread("test.png")
print(img.shape) # [M,N,K] 行 列 通道数
print(img.size) # M*N*K
# 一些特殊方法
print(img.mean()) #平均值 img.sum() 元素和 std()标准差 等等
img = img.flatten()
img = img.</