Reputation: 3
I need to read large numbers of images with high speed requirements, and just need to handle the Blue channel of a color image.
If I read image with cv::imread(imgName, CV_LOAD_IMAGE_COLOR);
It will be very long time, so I want to read only one color image channel. How to do it ??? thanks very much !!
Upvotes: 0
Views: 4839
Reputation: 41765
OpenCV doesn't provide any method to load only a specific channel. However, you have a few options.
Load as color image and extract the channel you need
cv::Mat3b img("path/to/image", cv::IMREAD_COLOR);
cv::Mat1b blue;
cv::extractChannel(img, blue, 0);
This is a little faster than using the split
approach, but you still need to load the color image.
In a preprocessing stage, load all your images (you can use glob to retrieve all images into a folder), extract the blue channel and store it as grayscale. Then you can load the image as grayscale.
// 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);
You can improve speed by saving / loading the image in binary format:
// Preprocessing
...
matwrite("some/other/name", blue);
// Processing
cv::Mat1b blue = matread("path/to/image");
Upvotes: 5
Reputation: 1925
I think you can not do this, at least with OpenCV. If you check the documentation of cv::imread
you will see that there is no option to read only one color channel:
If you want, you can split the channels of a matrix after loading it usin Mat::split
:
Mat src = imread("img.png",CV_LOAD_IMAGE_COLOR); //load image
Mat bgr[3]; //destination array
split(src,bgr);//split source
//Note: OpenCV uses BGR color order
imwrite("blue.png",bgr[0]); //blue channel
imwrite("green.png",bgr[1]); //green channel
imwrite("red.png",bgr[2]); //red channel
Upvotes: 4