如何正确使用cv :: merge来合并3个分割的通道

时间:2017-04-20 09:16:15

标签: c++ opencv array-merge kinect-v2 libfreenect2

使用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但是我遇到了分段错误,所以我现在想要自己做usig cv :: split和cv :: merge。但同样,我得到了“分段错误(核心转储)”。但我真的不明白为什么。这里有代码,其中rgba_mat使用上面的3行填充:

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)

由于所有渠道都有良好的规模,我不明白为什么合并不起作用......

感谢您的帮助:)

0 个答案:

没有答案