Skip to content

Commit

Permalink
Changed strides in Image python bindings to account for row-major Ima…
Browse files Browse the repository at this point in the history
…ge class

It seems from the comment in the code that with the introduction of the core::Image class in July 2018, I just copied the cv::Mat bindings file, and did only minor adjustments (and haven't adjusted the strides). The core::Image class stores the data in row-major order, so the strides would've likely changed. Indeed the `core::Image4u` image of `extract_texture(...)` was returned to Python transposed. And when I am testing the new `draw_wireframe(...)` returning an `Image3u`, it's all interleaved/wrong.
This change now fixes both and makes it so that for fitting on the example image, it works correctly.
  • Loading branch information
patrikhuber committed Dec 22, 2019
1 parent b53f057 commit dc0fce8
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions python/pybind11_Image.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ struct type_caster<eos::core::Image3u>

// (2048, 4, 1) is default which results in transposed image
// Below line works now. In numpy the strides are (2048, 4, 1) though. I think a copy gets created nevertheless?
std::vector<std::size_t> strides = { num_channels, num_channels * src.height(), 1 }; // might be cols or rows...? I think rows?
//std::vector<std::size_t> strides = { num_channels, num_channels * src.height(), 1 }; // might be cols or rows...? I think rows?
std::vector<std::size_t> strides = { num_channels * src.width(), num_channels, 1 }; // This seems to work with the row-major Image class. I just returned the same strides as the ones we got from NumPy.
// Note: I think with the change to the new Image class (July 2018), which is now row-major by default, the strides here might have changed. I didn't check.
// Also, since the new Image stores a vector of Pixels, the question is whether there's any padding added by the compiler, but probably not, since all types that we use are 1 byte or a multiple thereof.
// numpy: 'f' = fortran = col-major
Expand Down Expand Up @@ -167,7 +168,7 @@ struct type_caster<eos::core::Image4u>

// (2048, 4, 1) is default which results in transposed image
// Below line works now. In numpy the strides are (2048, 4, 1) though. I think a copy gets created nevertheless?
std::vector<size_t> strides = { num_channels, num_channels * src.height(), 1 }; // might be cols or rows...? I think rows?
std::vector<size_t> strides = { num_channels * src.width(), num_channels, 1 }; // might be cols or rows...? I think rows?
// Note: I think with the change to the new Image class (July 2018), which is now row-major by default, the strides here might have changed. I didn't check.
// Also, since the new Image stores a vector of Pixels, the question is whether there's any padding added by the compiler, but probably not, since all types that we use are 1 byte or a multiple thereof.
return array(pybind11::dtype::of<std::uint8_t>(), shape, strides, &src(0, 0).data()[0]).release();
Expand Down

0 comments on commit dc0fce8

Please sign in to comment.