Using libfreenect2 I'm able to get an rgba image in a 4 channels cv::mat tanks to these 3 lines which are working :
cv::Mat rgba_mat;
libfreenect2::Frame *rgba = frames[libfreenect2::Frame::Color];
cv::Mat(rgba->height, rgba->width, CV_8UC4, rgba->data).copyTo(rgba_mat);
But I want to only get the rgb image. I tryed to use cv::cvtColor but I got a segmentation fault so I now want to do it by myself usig cv::split and cv::merge. But again, I got a "Segmentation fault (core dumped)". But I really don't understand why. Here there is the code where rgba_mat is filled using the 3 lines above :
cv::Mat tmp[4];
std::vector<cv::Mat> to_merge(3);
std::cout << "------- RGBA mat => nbr cannel :" << rgba_mat.channels() << " | cols : " << rgba_mat.cols << " | row : " << rgba_mat.rows << std::endl;
cv::split(rgba_mat, tmp);
std::cout << "1 - split done" << std::endl;
cv::waitKey(1);
cv::Mat r,g,b, zeros = cv::Mat::zeros(rgba_mat.rows, rgba_mat.cols, CV_8UC1);
std::cout << "------- tmp 0 mat => nbr cannel :" << tmp[0].channels() << " | cols : " << tmp[0].cols << " | row : " << tmp[0].rows << std::endl;
std::cout << "------- tmp 1 mat => nbr cannel :" << tmp[1].channels() << " | cols : " << tmp[1].cols << " | row : " << tmp[1].rows << std::endl;
std::cout << "------- tmp 2 mat => nbr cannel :" << tmp[2].channels() << " | cols : " << tmp[2].cols << " | row : " << tmp[2].rows << std::endl;
std::cout << "------- tmp 3 mat => nbr cannel :" << tmp[3].channels() << " | cols : " << tmp[3].cols << " | row : " << tmp[3].rows << std::endl;
std::cout << "------- zeros mat => nbr cannel :" << zeros.channels() << " | cols : " << zeros.cols << " | row : " << zeros.rows << std::endl;
std::cout << "2 - zeros done" << std::endl;
cv::waitKey(1);
to_merge[0] = (tmp[0]);
to_merge[1] = (zeros);
to_merge[2] = (zeros);
std::cout << "------- to_merge vector size : " << to_merge.size() << ", 0's channels = " << to_merge[0].channels() << " | cols : " << to_merge[0].cols << " | row : " << to_merge[0].rows << std::endl;
std::cout << "------- to_merge vector size : " << to_merge.size() << ", 1's channels = " << to_merge[1].channels() << " | cols : " << to_merge[1].cols << " | row : " << to_merge[1].rows << std::endl;
std::cout << "------- to_merge vector size : " << to_merge.size() << ", 2's channels = " << to_merge[2].channels() << " | cols : " << to_merge[2].cols << " | row : " << to_merge[2].rows << std::endl;
std::cout << "2.5 - to_merge filled" << std::endl;
cv::waitKey(1);
cv::merge(to_merge, r);
std::cout << "3 - merge r done" << std::endl;
cv::waitKey(1);
As you can see I tryed several things to find out where the problem come from and got this output :
------- RGBA mat => nbr cannel :4 | cols : 1920 | row : 1080
1 - split done
------- tmp 0 mat => nbr cannel :1 | cols : 1920 | row : 1080
------- tmp 1 mat => nbr cannel :1 | cols : 1920 | row : 1080
------- tmp 2 mat => nbr cannel :1 | cols : 1920 | row : 1080
------- tmp 3 mat => nbr cannel :1 | cols : 1920 | row : 1080
------- zeros mat => nbr cannel :1 | cols : 1920 | row : 1080
2 - zeros done
------- to_merge vector size : 3, 0's channels = 1 | cols : 1920 | row : 1080
------- to_merge vector size : 3, 1's channels = 1 | cols : 1920 | row : 1080
------- to_merge vector size : 3, 2's channels = 1 | cols : 1920 | row : 1080
2.5 - to_merge filled
Segmentation fault (core dumped)
As all the channels have the good size I don't understand why the merge doesn't work ...
Thanks for your help :)