I managed to do what I need with Python code I found on Stack Overflow:
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
labelnum, labelimg, contours, GoCs = cv2.connectedComponentsWithStats(gray)
for label in xrange(1, labelnum):
x,y,w,h,size = contours[label]
if size <= N:
img_white[y:y+h, x:x+w] = 0
cv2.imwrite("img_filter.png", img_white)
It managed to remove small areas (small blobs and particles) by accessing contours elements.
I want to do this in C++. Is it possible? I have found this function:
int connectedComponentsWithStats(InputArray image, OutputArray labels, OutputArray stats, OutputArray centroids, int connectivity=8, int ltype=CV_32S)
But I don't see how to access the contours elements. Any tip?