Convert single channle image to 3 channel image C++ / OpenCV
Im using openCV C++
Im need to convert a single channel image to 3 channels image. So i can use this: cvCvtColor(result,gray开发者_StackOverflow,CV_BGR2GRAY);
I cannot do that because the result img is a single channel image. Any ideas?
You should use CV_GRAY2BGR
instead of CV_BGR2GRAY
.
I checked cvtColor in OpenCV reference book, and found this:
C++:void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0 )
dstCn – Number of channels in the destination image. If the parameter is 0, the number of the channels is derived automatically from src and code .
I have used
cvtColor(src,gray,CV_BGR2GRAY,1);
to convert a 3-channels Mat to 1-channels, and it worked. So changing dstCn to 3 might work for you :)
Try this:
CvSize dim = cvSize(int width, int height);
IplImage* dst = cvCreateImage( dim, 8, 3 );
IplImage* gray = cvCreateImage(dim, 8, 1);
// Load the gray scale image
cvMerge(gray , NULL, NULL, NULL, dst);
cvShowImage("gray",gray);
cvShowImage("dst",dst);
cvWaitKey(0);
Both dst and gray must have their data types same. You cant simply merge a float in a uint matrix. You will have to use cvScale for that.
Try merging gray,gray,gray into a BGR.
You can create a vector of the same channel, then merge them, like as follows:
std::vector<cv::Mat> copies{mat,mat,mat};
cv::merge(copies,mat);
IMHO:
If input is gray, then output would be gray. The best you can get is a colored distributed image based on pixel data say (110, 110, 110)
for pixel data of 110
, and not the true colored image
精彩评论