I can't see anything wrong with your code. I suspect it may be the source data, or how it is being treated by the importer. After you load the images, can you print out the image metadata to see what attributes OpenCV is reading? They all must have the same dimensions and a single channel.
std::cout << "Red: " << R.size().width << " x " << R.size().height << " x " << R.channels() << std::endl;
std::cout << "Green: " << G.size().width << " x " << G.size().height << " x " << G.channels() << std::endl;
std::cout << "Blue: " << B.size().width << " x " << B.size().height << " x " << B.channels() << std::endl;
I just ran your program, just with the headers and main added. I made one small change, to merge in B,G,R order (the default). I compiled it on Mac OS X 10.8 with:
g++ -o merge merge.cpp -lopencv_core -lopencv_highgui
against OpenCV version 2.4.3.
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
int main(int argc, char* argv[])
{
cv::Mat R = cv::imread("chan_dest_red.png", 0);
cv::Mat G = cv::imread("chan_dest_green.png", 0);
cv::Mat B = cv::imread("chan_dest_blue.png", 0);
std::vector<cv::Mat> array_to_merge;
array_to_merge.push_back(B);
array_to_merge.push_back(G);
array_to_merge.push_back(R);
cv::Mat color;
cv::merge(array_to_merge, color);
imwrite("merged.png", color);
return 0;
}
I had some single channel images from another project already split into files, and I just merged those into a single colour image and it worked just fine. The source file info is as follows:
Red: 500 x 333 x 1
Green: 500 x 333 x 1
Blue: 500 x 333 x 1
Best Answer
Assuming you have three matrices
R
,G
,B
of typeint16
. If you tryIMSHOW will complain that:
RGB images must be uint8, uint16, single, or double.
. In fact if you check the documentation:So if you concatenate three
int16
grayscale images, and you want to use IMSHOW, you have to convert the resulting truecolor image to something it supports: