[ITK] [ITK-users] Is this a bug ?

Francois Budin francois.budin at kitware.com
Sat Nov 5 09:41:23 EDT 2016


Hello Cyril,

Thank you for pointing out this behavior. This is a memory allocation
problem in your program. To need to create some memory space to save the
data that is returned by the vnl_matrix.
See below a way to solve your issue.

Hope this helps,
Francois

#include <iostream>
#include <vnl_vector.h>
#include <vnl_matrix.h>
#include <cstring>

int main()
{
  vnl_matrix<double> matrix(2, 2);
  matrix.put(0, 0, 4);
  matrix.put(0, 1, 3);
  matrix.put(1, 0, 2);
  matrix.put(1, 1, 1);

  // Display the matrix
  std::cout << "Matrix" << std::endl;
  std::cout << matrix << std::endl;

  // Display the first row of the matrix,
  vnl_vector<double> matrixRow;
  matrixRow = matrix.get_row(0);
  std::cout << "First row, stored in a vnl_vector" << std::endl;
  std::cout << matrixRow << std::endl;

  double matrixRowData[2];
  std::memcpy(matrixRowData,  matrix.get_row(0).data_block(),
2*sizeof(double));
  std::cout << "First row, stored in a double*" << std::endl;
  std::cout << matrixRowData[0] << " " << matrixRowData[1] << std::endl;

  double* matrixRowDataFromVector = matrixRow.data_block();
  std::cout << "First row, stored in a double* after passing through a
vnl_vector" << std::endl;
  std::cout << matrixRowDataFromVector[0] << " " <<
matrixRowDataFromVector[1] << std::endl;

  return 0;
}

Matrix

4 3

2 1


First row, stored in a vnl_vector

4 3

First row, stored in a double*

4 3

First row, stored in a double* after passing through a vnl_vector

4 3


On Fri, Nov 4, 2016 at 10:26 AM, Cyril Mory <cyril.mory at creatis.insa-lyon.fr
> wrote:

> Hello, ITK users,
>
> I've encountered a behavior with vnl_matrix, and I'm wondering whether it
> is a bug or a feature. The following code outputs differents results:
>
> #include <iostream>
> #include <vnl_vector.h>
> #include <vnl_matrix.h>
>
> int main()
> {
>   vnl_matrix<double> matrix(2, 2);
>   matrix.put(0, 0, 4);
>   matrix.put(0, 1, 3);
>   matrix.put(1, 0, 2);
>   matrix.put(1, 1, 1);
>
>   // Display the matrix
>   std::cout << "Matrix" << std::endl;
>   std::cout << matrix << std::endl;
>
>   // Display the first row of the matrix,
>   vnl_vector<double> matrixRow;
>   matrixRow = matrix.get_row(0);
>   std::cout << "First row, stored in a vnl_vector" << std::endl;
>   std::cout << matrixRow << std::endl;
>
>   double* matrixRowData = matrix.get_row(0).data_block();
>   std::cout << "First row, stored in a double*" << std::endl;
>   std::cout << matrixRowData[0] << " " << matrixRowData[1] << std::endl;
>
>   double* matrixRowDataFromVector = matrixRow.data_block();
>   std::cout << "First row, stored in a double* after passing through a
> vnl_vector" << std::endl;
>   std::cout << matrixRowDataFromVector[0] << " " <<
> matrixRowDataFromVector[1] << std::endl;
>
>   return EXIT_SUCCESS;
> }
>
> Output :
>
> Matrix
>
> 4 3
>
> 2 1
>
> First row, stored in a vnl_vector
>
> 4 3
>
> First row, stored in a double*
>
> 0 3
>
> First row, stored in a double* after passing through a vnl_vector
>
> 4 3
>
>
> It was compiled with the following CMakeLists.txt:
>
>
> cmake_minimum_required(VERSION 2.8)
>
> project(IsThisAvnlBug)
>
> find_package(ITK REQUIRED)
> include(${ITK_USE_FILE})
>
> add_executable(IsThisAvnlBug MACOSX_BUNDLE IsThisAvnlBug.cxx)
> target_link_libraries(IsThisAvnlBug ${ITK_LIBRARIES})
>
>
> _____________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Kitware offers ITK Training Courses, for more information visit:
> http://www.kitware.com/products/protraining.php
>
> Please keep messages on-topic and check the ITK FAQ at:
> http://www.itk.org/Wiki/ITK_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/insight-users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/community/attachments/20161105/901a9c5a/attachment-0001.html>
-------------- next part --------------
_____________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at
http://www.kitware.com/opensource/opensource.html

Kitware offers ITK Training Courses, for more information visit:
http://www.kitware.com/products/protraining.php

Please keep messages on-topic and check the ITK FAQ at:
http://www.itk.org/Wiki/ITK_FAQ

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/insight-users


More information about the Community mailing list