lua---opencv icon indicating copy to clipboard operation
lua---opencv copied to clipboard

IplImage row alignment

Open jtbates opened this issue 12 years ago • 4 comments

The libopencv routines assume that IplImage data is contiguous, but the rows are padded to be 4 or 8 byte aligned (although I've only observed 4 byte alignment).

http://opencv.willowgarage.com/documentation/c/core_basic_structures.html#align

As an example, this works:

t7> require 'opencv'
t7> require 'image'
t7> imgL=opencv.imgL()
t7> imgR=opencv.imgR()
t7> sc=opencv.StereoCorrespondenceGC(imgL,imgR)
t7> image.display(sc)

But this does not:

t7> sc_m1=opencv.StereoCorrespondenceGC(imgL[{{},{},{1,639}}],imgR[{{},{},{1,639}}])
t7> image.display(sc_m1)

jtbates avatar Sep 28 '12 22:09 jtbates

The interface was coded a bit too hastily :-). In C, there's an API function called THTensor_getContiguous(), which creates a contiguous tensor from any tensor. In Lua, that function is just called :contiguous(). These should always be used/called when creating interfaces to other libs.

clementfarabet avatar Sep 28 '12 23:09 clementfarabet

I'm not sure how they would help in this case. For example, in the function

static IplImage * libopencv_(Main_torchimg2opencv_8U)(THTensor *source) {

the copy is done like this

TH_TENSOR_APPLY(real, tslice,
      *destp = (uchar)(*tslice_data * 255.0);
      // step through ipl
      destp = destp + dest->nChannels;
      );

Whether tslice is contiguous or not isn't the problem. It's that we're treating the raw data from IplImage as if widthStep == width.

I think this could be solved by dropping the macro and writing the inner loops, by padding tslice, or if you're feeling particularly evil by referencing the macro's loop variable inside the code we send it.

jtbates avatar Sep 29 '12 00:09 jtbates

I believe this was from a while ago, but fixed the bug in my branch and issued a pull request. Issue can be closed after pulled. https://github.com/marcoscoffier/lua---opencv/pull/3

soumith avatar Feb 07 '13 21:02 soumith

Hi, I want to convert a real * variable to an IplImage *variable. Can you tell me how to do it? thanks

arthitag avatar Sep 10 '15 14:09 arthitag