c++ - OpenCV: A straighforward method of colorizing a grayscale image -
what straight forward method of "colorizing" grayscale image. colorizing, mean porting grayscale intensity values 1 of 3 r, g, b channels in new image.
for example 8uc1 grayscale pixel intensity of i = 50 should become 8uc3 color pixel of intensity bgr = (50, 0, 0) when picture colorized "blue".
in matlab example, i'm asking can created 2 lines of code:
color_im = zeros([size(gray_im) 3], class(gray_im)); color_im(:, :, 3) = gray_im; but amazingly cannot find similar in opencv.
well, same thing requires bit more work in c++ , opencv:
// load single-channel grayscale image cv::mat gray = cv::imread("filename.ext", cv_load_image_grayscale); // create empty matrix of same size (for 2 empty channels) cv::mat empty = cv::mat::zeros(gray.size(), cv_8uc1); // create vector containing channels of new colored image std::vector<cv::mat> channels; channels.push_back(gray); // 1st channel channels.push_back(empty); // 2nd channel channels.push_back(empty); // 3rd channel // construct new 3-channel image of same size , depth cv::mat color; cv::merge(channels, color); or function (compacted):
cv::mat colorize(cv::mat gray, unsigned int channel = 0) { cv_assert(gray.channels() == 1 && channel <= 2); cv::mat empty = cv::mat::zeros(gray.size(), gray.depth()); std::vector<cv::mat> channels(3, empty); channels.at(channel) = gray; cv::mat color; cv::merge(channels, color); return color; }
Comments
Post a Comment