Skip to content

External Interface (OpenCV) with OpenMP enabled produce partial image #191

@VisualFox

Description

@VisualFox

Component

DIPlib 3.5.2
tested also with DIPlib 3.4.1

Describe the bug

Calling a DipLib function from OpenCV using one of these three methods (method #3 is tested with and without copy) described here: https://github.com/DIPlib/diplib/blob/master/examples/external_interfaces/opencv_with_dip.cpp
with OpenMP enabled (DIP_ENABLE_MULTITHREADING=ON) will randomly produce an OpenCV image (cv::Mat) with only partially proceeded channels (channel partially filled with 0). Disabling OpenMP (DIP_ENABLE_MULTITHREADING=OFF) resolve the issue.

This behavior with OpenMP enabled seems random, not all channels are affected all the time. Multiple call of the same DipLib function (for example dip::Gauss) will only sometime produce the issue, but in my test the same binary will always produce the exact same defect in the exact order and place.

This behavior is also present with DIPlib 3.4.1 (I have not tried any other version).

Screenshot from 2025-01-03 19-36-04

To Reproduce

cv::Mat DipGauss(const cv::Mat& src, const dip::FloatArray& sigmas, const dip::UnsignedArray& derivativeOrder, const dip::String& method, const dip::StringArray& boundaryCondition, const dip::dfloat truncation) {
    dip_opencv::ExternalInterface external;

    const dip::Image src_dip = dip_opencv::MatToDip(src);
    dip::Image output_dip = external.NewImage();
    dip::Gauss(src_dip, output_dip, sigmas, derivativeOrder, method, boundaryCondition, truncation);

    return external.GetMat(output_dip);
}

int main(int argc, char** argv) {
    cv::Mat src = cv::imread("_PATH_TO _IMAGE_FILE", -1);
    cv::imshow("src", src);

    cv::Mat blurredA = DipGauss(src, {1.0}, {0}, "best", {"mirror"}, 3);
    cv::imshow("blurred 1.0", blurredA);

    cv::Mat blurredB = DipGauss(src, {3.0}, {0}, "best", {"mirror"}, 3);
    cv::imshow("blurred 3.0", blurredB);

    cv::Mat dx = DipGauss(src, {1.0}, {1, 0}, "best", {"mirror"}, 3);
    cv::imshow("dx", dx);

    cv::Mat dy = DipGauss(src, {1.0}, {0, 1}, "best", {"mirror"}, 3);
    cv::imshow("dy", dy);

    cv::Mat dxx = DipGauss(src, {1.0}, {2, 0}, "best", {"mirror"}, 3);
    cv::imshow("dxx", dxx);

    cv::Mat dyy = DipGauss(src, {1.0}, {0, 2}, "best", {"mirror"}, 3);
    cv::imshow("dyy", dyy);
}

System information:

Ubuntu 24.04
C++ compiler: GNU 13.3.0
OpenCV: 4.10.0
OpenMP: version 4.5 as reported by CMake (libomp-dev/noble 1:18.0-59~exp2 amd64)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions