Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Transform buffer to cv::Mat without copy.

I try to convert buffered data in cv::Mat (it's originaly to make a python binding numpy array to cv::Mat)

in python when we do channel slicing the internal strides in "simply" set to 3 instead of 1 (there is no copy)

a = eb.read_image("test.png")
b=a[:,:,0]

so b is not continuous

In full opencv C++, it's equivalente to try to get only one channel from a CV_8UC3 image. I try this but it don't seem to work as I think

cv::Mat image_from_file = cv::imread("test.png", CV_LOAD_IMAGE_COLOR);
cv::Mat image_transform = cv::Mat(cv::Size(image_from_file.cols, image_from_file.rows), 
                                  CV_8UC1,                       // output type one channel
                                  image_from_file.data,          // buffered data
                                  image_from_file.elemSize()*image_from_file.cols*3   // total byte size of one rows
                                  );
cv::imshow("image_split_channel_from_buffer", image_transform);
cv::waitKey(0);

the other interface with step parameter give me the same result :

cv::Mat::Mat    (  int           ndims,
                   const int *   sizes,
                   int           type,
                   void *        data,
                   const size_t *   steps = 0 
)

Transform buffer to cv::Mat without copy.

I try to convert buffered data in cv::Mat (it's originaly to make a python binding numpy array to cv::Mat)cv::Mat) my code here https://github.com/edmBernard/pybind11_opencv_numpy

in python when we do channel slicing the internal strides in "simply" set to 3 instead of 1 (there is no copy)

a = eb.read_image("test.png")
b=a[:,:,0]

so b is not continuous

In full opencv C++, it's equivalente to try to get only one channel from a CV_8UC3 image. I try this but it don't seem to work as I think

cv::Mat image_from_file = cv::imread("test.png", CV_LOAD_IMAGE_COLOR);
cv::Mat image_transform = cv::Mat(cv::Size(image_from_file.cols, image_from_file.rows), 
                                  CV_8UC1,                       // output type one channel
                                  image_from_file.data,          arr is my buffer (raw data)
cv::Mat(cv::Size(shape[1], shape[0]), 
        dtype_cv, 
        arr.mutable_data(), 
        image_from_file.elemSize()*image_from_file.cols*3);
// buffered data
                                  image_from_file.elemSize()*image_from_file.cols*3   another solution with the same result
// total byte size of one rows
                                  );
cv::imshow("image_split_channel_from_buffer", image_transform);
cv::waitKey(0);
here info.ptr is my buffer
cv::Mat(ndims,
        &shape[0],
        dtype
        info.ptr
        &strides[0]);

the other interface with step parameter give me the same result edit :

cv::Mat::Mat    (  int           ndims,
                   const int *   sizes,
                   int           type,
                   void *        data,
                   const size_t *   steps = 0 
)
there some misunderstanding of my problem I try to be cleaner

Transform buffer to cv::Mat without copy.

I try to convert buffered data in cv::Mat (it's originaly to make a python binding numpy array to cv::Mat) my code here https://github.com/edmBernard/pybind11_opencv_numpy

in python when we do channel slicing the internal strides in "simply" set to 3 instead of 1 (there is no copy)

a = eb.read_image("test.png")
b=a[:,:,0]

so b is not continuous

In full opencv C++, it's equivalente to try to get only one channel from a CV_8UC3 image. I try this but it don't seem to work as I think

// arr is my buffer (raw data)
cv::Mat(cv::Size(shape[1], shape[0]), 
        dtype_cv, 
        arr.mutable_data(), 
        image_from_file.elemSize()*image_from_file.cols*3);
// another solution with the same result
// here info.ptr is my buffer
cv::Mat(ndims,
        &shape[0],
        dtype
        info.ptr
        &strides[0]);

edit : there is some misunderstanding of my problem I try to be cleaner