Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
724 views
in Technique[技术] by (71.8m points)

matlab - 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

enter image description here

Someone performs morphological closing on the image with disk-shaped structuring element and obtain a good result:

enter image description here

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

Closed image

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...