opencv:获取图像基本信息

在 OpenCV 中,图像的格式通常与图像的类型和通道数相关。可以通过 Mat 对象的属性来查询图像的格式,以下是一些常用的方式来获取图像的格式信息:

1. 查询图像的通道数

你可以使用 channels() 方法来获取图像的通道数。

cv::Mat img = cv::imread("image.jpg");
int channels = img.channels();
std::cout << "Number of channels: " << channels << std::endl;

2. 查询图像的深度(每个像素的类型)

depth() 方法可以获取图像每个像素的位深度,即每个通道存储的数据类型。OpenCV 使用常量来表示不同的类型:

  • CV_8U:0 (8 位无符号整数)
  • CV_8S:1 (8 位有符号整数)
  • CV_16U:2 (16 位无符号整数)
  • CV_16S:3 (16 位有符号整数)
  • CV_32S:4 (32 位有符号整数)
  • CV_32F:5 (32 位浮点数)
  • CV_64F:6 (64 位浮点数)
int depth = img.depth();
std::cout << "Image depth: " << depth << std::endl;

3. 查询图像的类型

type() 方法返回一个整数,它结合了图像的深度和通道数。常用的类型有:

  • CV_8UC1: 0 (8 位无符号整数,单通道)
  • CV_8UC3: 16 (8 位无符号整数,3 通道)
  • CV_8UC4: 32 (8 位无符号整数,4 通道)
int type = img.type();
std::cout << "Image type: " << type << std::endl;

你可能感兴趣的:(opencv,opencv)