6

グレースケール画像を「色付け」する簡単な方法は何ですか。色付けとは、グレースケール強度値を新しい画像の 3 つの R、G、B チャネルのいずれかに移植することを意味します。

たとえば、強度が の8UC1グレースケール ピクセルは、画像が「青」に着色されると、強度がカラー ピクセルにI = 50なるはずです。8UC3BGR = (50, 0, 0)

たとえば、Matlab では、私が求めているものは 2 行のコードで簡単に作成できます。

color_im = zeros([size(gray_im) 3], class(gray_im));
color_im(:, :, 3) = gray_im; 

しかし驚くべきことに、OpenCV で同様のものを見つけることができません。

4

2 に答える 2

5

同じことを C++ と OpenCV でもう少し作業する必要があります。

// Load a single-channel grayscale image
cv::Mat gray = cv::imread("filename.ext", CV_LOAD_IMAGE_GRAYSCALE);

// Create an empty matrix of the same size (for the two empty channels)
cv::Mat empty = cv::Mat::zeros(gray.size(), CV_8UC1);

// Create a vector containing the channels of the 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 a new 3-channel image of the same size and depth
cv::Mat color;
cv::merge(channels, color);

または関数として(圧縮):

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;
}
于 2013-04-05T21:42:17.850 に答える
5

There are special function to do this - applyColorMap in OpenCV from v2.4.5 in contrib module. There are different color maps available:

Color maps

于 2013-04-07T20:17:39.123 に答える