MSER with a masked image

Hi everyone,

I am trying to find the regions of a gray-level image. However I really need that MSER only considers regions in the foreground and not in the background. For my example the background is considered as all the pixels with value 0.

For example , the function loadAllImages is previously defined and it returns the image and the background. The image in gray level and the foreground as 255 for the region of interest and 0 for the rest.

image, foreground= loadAllImages() 
image1 = cv2.bitwise_and(image,image, mask=foreground)

At this point I have the image1 matrix as image with a mask. However, when I apply MSER, I always get regions in the background.

mser = cv2.MSER_create( 5, # delta 
                       60, # min_area
                       14400, #max_area 
                       .5, # max_variation 
                       .0002, # min_diversity 
                       5, # max_evolution 
                       1.01, # area_threshold 
                       0.003, # min_margin
                       5) # edge_blur_size

regions, bboxes = mser.detectRegions(image1)
regions = sorted(regions, key=cv2.contourArea, reverse=True)

To plot the regions that I am getting:

img_mser = cv2.cvtColor(image1, cv2.COLOR_GRAY2RGB)
for p in regions[:]:
  for k in p:, (k[0],k[1]), radius=0, color=(0, 0, 255), thickness=-1)

If anyone can help me to find a way to mask the image in order to define the limits for MSER to find regions only in the foreground, I will really appreciate it.

Thank you