我想创建一个有4个通道的图像,图像输出是RGB图像和灰度图像的混合,我用c++开发了这段代码:
cv::Mat cv_img ( height, width, CV_8UC4 heredcv::Scalar(1,2,3,4) );
cv::Mat cv_img_rgb = ReadImageToCVMat(filename_rgb, height, width, is_color);
cv::Mat cv_img_gray = ReadImageToCVMat(filename_gray, height, width, !is_color);
cv::Mat out[] = { cv_img_rgb, cv_img_gray };
int from_to[] = { 0,0, 1,1, 2,2, 3,3 };
cv::mixChannels( &cv_img, 1, out, 2, from_to, 4);
问题是"Segmentation fault"
与ReadImageToCVMat是这样的函数:
cv::Mat ReadImageToCVMat(const string& filename,
const int height, const int width, const bool is_color) {
cv::Mat cv_img;
int cv_read_flag = (is_color ? CV_LOAD_IMAGE_COLOR :
CV_LOAD_IMAGE_GRAYSCALE);
cv::Mat cv_img_origin = cv::imread(filename, cv_read_flag);
if (!cv_img_origin.data) {
LOG(ERROR) << "Could not open or find file " << filename;
return cv_img_origin;
}
if (height > 0 && width > 0) {
cv::resize(cv_img_origin, cv_img, cv::Size(width, height));
} else {
cv_img = cv_img_origin;
}
return cv_img;
}
看起来好像您混淆了cv::mixChannels的参数。假设cv_img
是您的目标缓冲区,它应该是这样的:
cv::Mat src[] = { cv_img_rgb, cv_img_gray };
int from_to[] = { 0,0, 1,1, 2,2, 3,3 };
cv::mixChannels( src, 2, &cv_img, 1, from_to, 4);