opencv::mixchannels segmentation fault

415 views Asked by At

I want to create an image with 4 channels, the image output is the mix of a RGB image and an grey scale image, I developed with C++ this code :

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

The problem is that "Segmentation fault"

with ReadImageToCVMat is a function like that:

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;
}
1

There are 1 answers

0
Atze Kaputtnik On

It looks as if you mixed up the arguments of cv::mixChannels. Assuming that cv_img is your destination buffer, it should be something like this instead:

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