ukoethe / vigra

a generic C++ library for image analysis
http://ukoethe.github.io/vigra/
Other
411 stars 191 forks source link

Is it legal to use the same view for input and output of labelMultiArray()? #453

Closed emmenlau closed 2 years ago

emmenlau commented 6 years ago

I have a student who just spent an hour trying to find an issue that is not so obvious. When using labelMultiArray() (https://ukoethe.github.io/vigra/doc-release/vigra/group__Labeling.html#ga8692820fcfdc8adb7ad57582bbb20392) it seems illegal that source and destination point to the same MultiArrayView. This is currently not highlighted in the documentation. But what is more unfortunate, there is also no warning or exception, the code just produces unexpected results.

If this is indeed illegal, would it possible to check if two views point to the same memory?

emmenlau commented 6 years ago
   //mLabelsImage is a vigra::MultiArray<2, size_t> with binary labels. To find connected compontents:
   vigra::MultiArray<2, size_t> out(vigra::Shape2(mLabelsImage.width(),mLabelsImage.height()));
   const int max_region_label = labelMultiArray(mLabelsImage, out);
   const int max_region_label_same = labelMultiArray(mLabelsImage,mLabelsImage);
   //prints actual number of connected components:
   std::cout << "max: " << max_region_label << std::endl; 
   //prints the wrong value:
   std::cout << "max same: " << max_region_label_same << std::endl;
ukoethe commented 5 years ago

This is a general issue: some functions can operate in-place (i.e. input and output are the same array), others can't. I tried to find a HiWi to add this information to the documentation, but everyone considered the task too boring and refused. It is also not easy to detect illegal calls automatically. For example, if the source is a view to the left part of an array, and the target a view to the right part, the call is only illegal when the views overlap in the middle. Distinguishing all variants of legal and illegal calls is very expensive, if at all possible.

Upshot: I know about the problem, but didn't find a workable solution so far.

emmenlau commented 2 years ago

I guess this can be closed now.