Ask Your Question

Revision history [back]

If you have an image and you want to "reshape" it as a 3D matrix, I think that your image is a grayscale one. So if you want to transform your image to a 3D matrix, you have to options:

Convert your image from grayscale to BGR:

Mat color_dst;
cvtColor(gray, color_dst, CV_GRAY2BGR);

Convert your image from grayscale to BGR but with one channel information. So depending on what channel you put the information, you'll get a red, green or blue image:

Mat img, g, color;
img = imread("test.jpg",CV_LOAD_IMAGE_GRAYSCALE);
vector<Mat> channels;

g = Mat::zeros(Size(img.cols, img.rows), CV_8UC1);
imshow("grayscale", img);

channels.push_back(g);
channels.push_back(g);
channels.push_back(img);

merge(channels, color);
imshow("color_red", color);
imwrite("result_color_red.jpg",color); //red image

If you have an image and you want to "reshape" it as a 3D matrix, I think that your image is a grayscale one. So if you want to transform your image to a 3D matrix, you have to options:

Convert your image from grayscale to BGR:

Mat color_dst;
cvtColor(gray, color_dst, CV_GRAY2BGR);

Convert your image from grayscale to BGR but with one channel information. So depending on what channel you put the information, you'll get a red, green or blue image:

Mat img, g, color;
img = imread("test.jpg",CV_LOAD_IMAGE_GRAYSCALE);
vector<Mat> channels;

g = Mat::zeros(Size(img.cols, img.rows), CV_8UC1);
imshow("grayscale", img);

channels.push_back(g);
channels.push_back(g);
channels.push_back(img);

merge(channels, color);
imshow("color_red", color);
imwrite("result_color_red.jpg",color); //imwrite("result_color_red.jpg",color); //red image

If you have an image and you want to "reshape" it as a 3D matrix, I think that your image is a grayscale one. So if you want to transform your image to a 3D matrix, you have to options:

Convert your image from grayscale to BGR:

Mat color_dst;
cvtColor(gray, color_dst, CV_GRAY2BGR);

Convert your image from grayscale to BGR but with one channel information. So depending on what channel you put the information, you'll get a red, green or blue image:

Mat img, g, color;
img = imread("test.jpg",CV_LOAD_IMAGE_GRAYSCALE);
vector<Mat> channels;

g = Mat::zeros(Size(img.cols, img.rows), CV_8UC1);
imshow("grayscale", img);

channels.push_back(g);
channels.push_back(g);
channels.push_back(img);
channels.push_back(g); //blue information
channels.push_back(g); //green information
channels.push_back(img); //red information 

merge(channels, color);
imshow("color_red", color);
//imwrite("result_color_red.jpg",color); //red image