0

使用 libfreenect2 我可以在 4 通道 cv::mat 坦克中获得 rgba 图像,这些 3 行正在工作:

cv::Mat rgba_mat;
libfreenect2::Frame *rgba = frames[libfreenect2::Frame::Color];
cv::Mat(rgba->height, rgba->width, CV_8UC4, rgba->data).copyTo(rgba_mat);

但我只想得到 rgb 图像。我尝试使用 cv::cvtColor 但我遇到了分段错误,所以我现在想自己使用 cv::split 和 cv::merge 来完成。但是,我再次遇到“分段错误(核心转储)”。但我真的不明白为什么。这是使用上面的 3 行填充 rgba_mat 的代码:

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

如您所见,我尝试了几件事来找出问题的根源并得到以下输出:

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

由于所有频道的尺寸都很好,我不明白为什么合并不起作用......

谢谢你的帮助 :)

4

0 回答 0