Abhinav Sood
Abhinav Sood

Reputation: 789

Difference in 'step' for 2 Matrices of the same image in OpenCV

Why is the 'step' value for 2 matrices for the same image different?

One of them is generated by LoadImageM() and another by GetMat() after the image was loaded with LoadImage(). My code is:

import cv2.cv as cv
def main():
    org_win = 'Original'
    cv.NamedWindow(org_win, cv.CV_WINDOW_AUTOSIZE)
    org_img = cv.LoadImage("bed.jpg", cv.CV_LOAD_IMAGE_COLOR)
    cv.ShowImage(org_win, org_img)

    org_img_mat1 = cv.LoadImageM("bed.jpg", cv.CV_LOAD_IMAGE_COLOR)
    org_img_mat2 = cv.GetMat(org_img, 0)

    print org_img_mat1
    print org_img_mat2

    cv.WaitKey(0)
    cv.DestroyWindow(org_win)

if __name__ == '__main__': main()

The result I'm getting is:

<cvmat(type=42424010 8UC3 rows=497 cols=681 step=2043 )>
<cvmat(type=42420010 8UC3 rows=497 cols=681 step=2044 )>

What is causing this difference in step value? Kindly enlighten me.

Upvotes: 2

Views: 334

Answers (1)

mevatron
mevatron

Reputation: 14011

Here is the source code for cv.LoadImage and cv.LoadImageM:

static PyObject *pycvLoadImage(PyObject *self, PyObject *args, PyObject *kw)
{
  const char *keywords[] = { "filename", "iscolor", NULL };
  char *filename;
  int iscolor = CV_LOAD_IMAGE_COLOR;

  if (!PyArg_ParseTupleAndKeywords(args, kw, "s|i", (char**)keywords, &filename, &iscolor))
    return NULL;

  // Inside ALLOW_THREADS, must not reference 'filename' because it might move.
  // So make a local copy 'filename_copy'.
  char filename_copy[2048];
  strncpy(filename_copy, filename, sizeof(filename_copy));

  IplImage *r;
  Py_BEGIN_ALLOW_THREADS
  r = cvLoadImage(filename_copy, iscolor);
  Py_END_ALLOW_THREADS

  if (r == NULL) {
    PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename);
    return NULL;
  } else {
    return FROM_IplImagePTR(r);
  }
}

static PyObject *pycvLoadImageM(PyObject *self, PyObject *args, PyObject *kw)
{
  const char *keywords[] = { "filename", "iscolor", NULL };
  char *filename;
  int iscolor = CV_LOAD_IMAGE_COLOR;

  if (!PyArg_ParseTupleAndKeywords(args, kw, "s|i", (char**)keywords, &filename, &iscolor))
    return NULL;

  // Inside ALLOW_THREADS, must not reference 'filename' because it might move.
  // So make a local copy 'filename_copy'.
  char filename_copy[2048];
  strncpy(filename_copy, filename, sizeof(filename_copy));

  CvMat *r;
  Py_BEGIN_ALLOW_THREADS
  r = cvLoadImageM(filename_copy, iscolor);
  Py_END_ALLOW_THREADS

  if (r == NULL) {
    PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename);
    return NULL;
  } else {
    return FROM_CvMatPTR(r);
  }
}

It looks like the main difference is return FROM_IplImagePTR(r) vs. return FROM_CvMatPTR(r) maybe step is one-based indexing in IplImage but zero-based indexing in CvMat?

Upvotes: 1

Related Questions