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.