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 code:

img = imread('c:img.png');
bg = imclose(img, strel('disk', 15));
figure('name', 'bg'), imshow(bg);

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?


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)));

I had to modify the structuring element size slightly to produce similar results to your MATLAB example:

封闭的图像

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.

链接地址: http://www.djcxy.com/p/61166.html

上一篇: 如何处理图像范围错误?

下一篇: 在opencv中实现imclose(IM,SE)