0

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 :)

Bastienm
  • 363
  • 2
  • 16
  • The code runs fine on my computer. – Quang Hoang Apr 20 '17 at 09:40
  • I forgot to say it, but I'm using OpenCv 3.2.0. Do you think that it can change something ? I looked at the doc (http://docs.opencv.org/3.2.0/d2/de8/group__core__array.html#ga61f2f2bde4a0a0154b2333ea504fab1d) ... and should work. I don't know where the seg fault comes from – Bastienm Apr 20 '17 at 09:48
  • I'm using 3.2.0 also, but I'm getting `rgba_mat` from camera. – Quang Hoang Apr 20 '17 at 09:59
  • While rgba_mat is still a 4 channels matrix I don't think that it will change something. My code is executed in a loop at 25Hz. Do you think that it can change something ? – Bastienm Apr 20 '17 at 10:12
  • My camera is at 30Hz, so I don't think it's a problem. Also, if you want rgb only, you can use `cvtColor`. – Quang Hoang Apr 20 '17 at 10:19
  • As I said at the beginning, I tryed cv::cvtColor(cv_rgba_mat, cv_rgb_mat, CV_RGBA2RGB) but i also gave me a seg fault, even if I initialise cv_rgb_mat. That's why I'm trying to do it by myself. – Bastienm Apr 20 '17 at 12:20

0 Answers0