I have an image with some blobs. Some is one pixel dot and some are not. When I use cvBlobsLibs to find the height and width of the one pixel dot, it shows the value equals to zero. Is this correct? I tried to use contours to fill the one dot pixel but seems to fail too. Any other approach that can remove the one dot pixel or even remove the height or width that is equal to zero?
Share
I am not sure why its area of a single pixel element is zero. ( my mind says it should be one). Check out documentation for
contourArea. It says, area is calculated usingGreen Formula, so area and number of pixels may be different.Secondly, to remove this noise, you can use medianFilter. I have shown it below using Python.
Input image :
Now Code :
Number of non-zero pixels:
Now apply medianFilter and check again number of non-zero pixels:
Output image :
EDIT:
If an image has an object, it will not be affected much by the blurring.
Input Image :
Output Image :
EDIT after second comment to this answer.
As Mizuki commented, there is a chance where closely placed objects get connected each other after median filtering. To understand this, i give here an image from wikipedia page. Check how yellow objects which are seperated by black lines get connected after using median filter of high radius.
It is because, median filter uses a window which calculates the median of all values in that windows and replaces center element by that median. As size of window increases, more elements are used to calculate median. So narrow gaps are removed.
Wikipedia articles is a good one : Link
Also see this link for a simple explanation : Link
So to avoid this, there is another method called erosion and dilation ( both implemented in OpenCV). Simply saying, erosion reduces the size of white object and dilation increases the size of white object.
So erosion removes small white pixels, but it also reduces the size of our object. So we use dilate to get back its size. Since, all white noises are already removed due to erosion, they won’t come back in dilation.
This is a good procedure to remove noise.