gpt4 book ai didi

opencv - 在 opencv 3 中使用 cuda::morphologyex

转载 作者:太空宇宙 更新时间:2023-11-03 22:53:16 30 4
gpt4 key购买 nike

我在一个使用形态学函数的 opencv 项目中工作。现在我正在尝试在 gpu 支持下进行。

当我使用 opencv 3.0 和 cuda 7.5 支持编译我的程序时,它接受除了 morphologyEx 之外的大部分函数(例如 cuda::threshold、cuda::cvtcolor 等)。请注意,morphologyex 在 opencv 2.4.9 中称为 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)的评论和文档,感觉morphologyEX的功能

morphologyEx(img, background, MORPH_OPEN, kernel);

可以替换为

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

如果我说错了,欢迎指出

最佳答案

如上面的评论所述,形态学不在 3.1 API 中。

我猜您需要按照 2.4 documentation 中的说明映射调用在 3.1 中完成的方式.

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

  • src 源图像。 使用 cuda::Filter 和 cuda::Filter::apply
  • dst 目标图片同上
  • op 形态学操作的类型。 试试 cuda::createMorphologyFilter

...等等

换句话说,2.4是一口气做了操作(morphologyex)。在 3.1 中,您首先使用 createFooFilter 创建过滤器,然后调用应用过滤器。

这不是一个明确的答案,更多的是一个建议,但不能真正在评论中写下所有这些。祝你好运。

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

编辑:尝试查看 https://github.com/Itseez/opencv/blob/master/samples/gpu/morphology.cpp .它展示了如何使用 cuda::createMorphologyFilter

关于opencv - 在 opencv 3 中使用 cuda::morphologyex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35321289/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com