Ask Your Question
0

Transform buffer to cv::Mat without copy.

asked 2016-12-02 04:11:42 -0600

Narew gravatar image

updated 2016-12-02 05:11:29 -0600

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/pybind1...

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

edit retag flag offensive close merge delete

Comments

1

how about the Answer here?

Vintez gravatar imageVintez ( 2016-12-02 04:24:28 -0600 )edit
1

just curious, why are you trying to reinvent the wheel here ? (opencv has python bindings already)

berak gravatar imageberak ( 2016-12-02 04:32:16 -0600 )edit

sure but i have some code in c++ that I want to use in python without recoding every things

Narew gravatar imageNarew ( 2016-12-02 04:58:43 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2016-12-02 04:28:32 -0600

berak gravatar image

opencv has "interleaved" pixels, so you can't extract a color plane without copying.

extractChannel() would be the "least expensive" way to do that

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-12-02 04:11:42 -0600

Seen: 1,564 times

Last updated: Dec 02 '16