cv::dilate doesn't respect image boundaries with shallow copy images. (Bug #2598)


Added by Alexander Haase over 12 years ago. Updated almost 10 years ago.


Status:Cancelled Start date:2012-12-05
Priority:Normal Due date:
Assignee:Vadim Pisarevsky % Done:

0%

Category:imgproc, video
Target version:3.0
Affected version:2.4.0 - 2.4.8 Operating System:Any
Difficulty: HW Platform:Any
Pull request:

Description

It looks like cv::dilate doesn't respect image boundaries with shallow copy images.

Attached is an example function that creates a black one channel parent image with a 2px line at the bottom, a sub image without the line, and a clone of the sub image. Both the sub image and its clone are dilated twice with an erosion rect, and then shown along with the absolute difference. In an optimal world the absolute difference image would be all zeros.

Opencv version 2.4.2 VC10 x86

void cvDilateBugExample( void )
{
    cv::Mat image = cv::Mat::zeros( cv::Size( 640, 482 ), CV_8UC1 );
    cv::Mat( image, cv::Rect( 0, 480, 640, 2 ) ).setTo( cv::Scalar( 255 ) );
    cv::Mat subImage( image, cv::Rect( 0, 0, 640, 480 ) );
    cv::Mat subImageClone = subImage.clone();
    const int erosionWidth = 10;
    const cv::Size erosionSize( 2*erosionWidth + 1, 2*erosionWidth +1 );
    cv::Mat element = getStructuringElement( cv::MORPH_RECT, erosionSize, cv::Point( erosionWidth, erosionWidth ) );
    dilate( subImage, subImage, element );
    dilate( subImage, subImage, element );
    dilate( subImageClone, subImageClone, element );
    dilate( subImageClone, subImageClone, element );
    cv::Mat diff;
    cv::absdiff( subImage, subImageClone, diff );
    cv::imshow( "DilateBug", diff );
    cv::imshow( "subImage", subImage );
    cv::imshow( "subImageClone", subImageClone );
    cv::waitKey( -1 );
    cv::destroyAllWindows();
}


History

Updated by Anna Kogan over 12 years ago

  • Target version set to 3.0
  • Category set to imgproc, video
  • Assignee set to Vadim Pisarevsky

Updated by Anna Kogan over 12 years ago

  • Description changed from It looks like cv::dilate doesn't respect image boundaries with shallow co... to It looks like cv::dilate doesn't respect image boundaries with shallow co... More

Updated by Vadim Pisarevsky almost 10 years ago

hello from the future (after 2 years) :)

1. when you work with a ROI, opencv filtering functions (like dilate) try to use all the available pixels, not just the ones that are in the ROI, this is quite natural. If you do not want this behaviour, add BORDER_ISOLATED flag to the border mode parameter.
2. copyTo/clone only copy the ROI. It would be a huge waste of resources to copy the whole image if you need only the ROI.
3. in the new image there are no pixels outside the ROI, so they are extrapolated.

In other words, the differences are expected and natural, if you analyze it carefully. If you do not them, use BORDER_ISOLATED. Whether BORDER_ISOLATED should be made default mode and "non-isolated" - user-specified, that's a philosophical question. We made this decision and so far stick to it.

  • Status changed from Open to Cancelled
  • HW Platform set to Any
  • Operating System set to Any
  • Affected version set to 2.4.0 - 2.4.8

Also available in: Atom PDF