OpenCV 不提供任何仅加载特定通道的方法。但是,您有几个选择。
加载为彩色图像并提取您需要的通道
cv::Mat3b img("path/to/image", cv::IMREAD_COLOR);
cv::Mat1b blue;
cv::extractChannel(img, blue, 0);
这比使用split 方法快一点,但您仍然需要加载彩色图像。
在预处理阶段,加载所有图像(您可以使用glob 将所有图像检索到文件夹中),提取蓝色通道并将其存储为灰度。然后您可以将图像加载为灰度。
// Preprocessing
cv::String folder = "your_folder_with_images/*.jpg";
std::vector<cv::String> filenames;
cv::glob(folder, filenames);
for (size_t i = 0; i < std::filenames.size(); ++i)
{
cv::Mat3b img = cv::imread(filenames[i], cv::IMREAD_COLOR);
cv::Mat1b blue;
cv::extractChannel(img, blue, 0);
cv::imwrite("some/other/name", blue);
}
// Processing
cv::Mat1b blue = imread("path/to/image", cv::IMREAD_UNCHANGED);
您可以通过saving / loading the image in binary format提高速度:
// Preprocessing
...
matwrite("some/other/name", blue);
// Processing
cv::Mat1b blue = matread("path/to/image");