DISCLAIMER: It is difficult to give a good answer because you have provided very little information. If you placed your image before and after binarization, it would be much easier. However, I will try to give some tips.
If the holes are large enough, then probably the threshold value is wrong, try increasing or decreasing it and checking the result. You can try
cv::threshold(gray_frame, gray_frame, 0, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);
This will automatically calculate the threshold value. If you cannot find a good threshold value, try some adaptive threshold algorithms, opencv has an adaptiveThreshold () function, but that is not so good.
If the holes and noise are quite small (a few pixels each), you can try the following:
Use opening (erosion, next dilatation) to remove white noise and close (dilatation, next erosion) to a little black noise. But remember that opening by removing white noise will also amplify black noise and vice versa.
Medium blur AFTER you create the threshold. It can remove small noise, both black and white, while maintaining colors (the image will be binary) and with possible small errors, shapes. Applying median blur to binarization can also help reduce a little noise.
Tomasz niedabylski
source share