在opencv中实现imclose(IM,SE) [英] Implementing imclose(IM, SE) in opencv
问题描述
我想检测下面图像的背景,其前景总是很多黑点:
I want to detect the background of the following image whose foreground is always lots of black dots:
img.png
有人使用磁盘形结构元素对图像执行形态学闭合并获得良好结果:
Someone performs morphological closing on the image with disk-shaped structuring element and obtain a good result:
Matlab代码:
img = imread('c:\img.png');
bg = imclose(img, strel('disk', 15));
figure('name', 'bg'), imshow(bg);
那么如何在opencv中实现imclose(IM,SE)来替换MATLAB中的工作或者有使用opencv方法检测此类背景的另一种更好的方法是?
So how to implement imclose(IM, SE) in opencv to replace the work in MATLAB or there is another better way to detect such background using opencv method?
推荐答案
您可以使用 cv :: getStructuringElement( )
创建椭圆结构元素, cv :: morphologyEx()
执行结束操作。
You can use cv::getStructuringElement()
to create an elliptical structuring element, and cv::morphologyEx()
to perform a closing operation.
cv::morphologyEx(img, img, cv::MORPH_CLOSE, cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(21,21)));
我必须稍微修改结构元素大小以产生与MATLAB示例相似的结果:
I had to modify the structuring element size slightly to produce similar results to your MATLAB example:
由于您似乎对OpenCV的形态学操作感兴趣,我建议您给出文档一个通读,以查看它能够做什么。
Since you seem to be interested in morphological operations with OpenCV, I recommend you give the documentation a read-through to see what all it is capable of.
这篇关于在opencv中实现imclose(IM,SE)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!