首页 文章

在opencv 3中使用cuda :: morphologyex

提问于
浏览
1

我正在使用一个使用physicsex函数的opencv项目 . 现在我正在尝试使用gpu支持 .

当我使用opencv 3.0和cuda 7.5支持编译我的程序时,它接受大多数函数(例如cuda :: threshold,cuda :: cvtcolor等),除了physicsEx . 注意,在opencv 2.4.9中将morphologyex称为gpu :: morphologyEx .

如何在OpenCV 3.0或3.1中使用此功能?如果不支持,是否有替代此功能?

实际上我在非均匀照明中使用此功能进行背景检测 . 我正在为问题添加代码 . 请建议我如何替换morphologyEx功能 .

#include <opencv2\opencv.hpp>
using namespace cv;

int main()
{   
// Step 1: Read Image
Mat1b img = imread("path_to_image", IMREAD_GRAYSCALE);

// Step 2: Use Morphological Opening to Estimate the Background
Mat kernel = getStructuringElement(MORPH_ELLIPSE, Size(15,15));
Mat1b background;
morphologyEx(img, background, MORPH_OPEN, kernel);

// Step 3: Subtract the Background Image from the Original Image
Mat1b img2;
absdiff(img, background, img2);

// Step 4: Increase the Image Contrast
// Don't needed it here, the equivalent would be  cv::equalizeHist

// Step 5(1): Threshold the Image
Mat1b bw;
threshold(img2, bw, 50, 255, THRESH_BINARY);

// Step 6: Identify Objects in the Image
vector<vector<Point>> contours;
findContours(bw.clone(), contours, CV_RETR_LIST, CV_CHAIN_APPROX_NONE);


for(int i=0; i<contours.size(); ++i)
{
    // Step 5(2): bwareaopen
    if(contours[i].size() > 50)
    {
        // Step 7: Examine One Object
        Mat1b object(bw.size(), uchar(0));
        drawContours(object, contours, i, Scalar(255), CV_FILLED);

        imshow("Single Object", object);
        waitKey();
    }
}

return 0;
}

================================================== ========================感谢@Roy Falk阅读了有 Value 的评论和文档后,我觉得morphEX函数

morphologyEx(img, background, MORPH_OPEN, kernel);

可以替换为

cv::Ptr<cv::cuda::Filter>morph = cuda::createMorphologyFilter(MORPH_OPEN, out.type(), kernel);
    morph->apply(out, bc);

我可以自由地说我错了

1 回答

  • 1

    如上面的评论所述,topologyex不在3.1 API中 .

    我猜你需要将2.4 documentation中记录的调用映射到它在3.1中完成的方式 .

    具体来说,morphologyex具有以下参数:

    • src源图像 . Use cuda::Filter and cuda::Filter::apply

    • dst目的 Map 片 same as above

    • op形态学操作的类型 . Try cuda::createMorphologyFilter

    ...等等 .

    换句话说,2.4一次完成操作(morphologyex) . 在3.1中,首先使用createFooFilter创建一个过滤器,然后调用apply过滤器 .

    这不是一个明确的答案,而是更多的建议,但不能在评论中真正写下这一切 . 祝好运 .

    ================================================== ===============

    Edit: 试试看https://github.com/Itseez/opencv/blob/master/samples/gpu/morphology.cpp . 它显示了如何使用cuda :: createMorphologyFilter

相关问题