Is there a good algorithm for detecting particles when the background intensity changes? For example, if I have the following image:

Is there a way to count small white particles, even with a clearly distinguishable background that appears in the lower left corner?
To be a little clearer, I would like to tag the image and count the particles by an algorithm that considers these particles significant:

I tried a lot of things with the modules PIL, cv, scipy, numpy, etc. I got some tips from this very similar SO question , and at first glance it seems like you can make a simple threshold:
im = mahotas.imread('particles.jpg')
T = mahotas.thresholding.otsu(im)
labeled, nr_objects = ndimage.label(im>T)
print nr_objects
pylab.imshow(labeled)
but due to the changing background you will get the following:

, , , :
import numpy as np
import scipy
import pylab
import pymorph
import mahotas
from scipy import ndimage
import cv
def detect_peaks(image):
"""
Takes an image and detect the peaks usingthe local maximum filter.
Returns a boolean mask of the peaks (i.e. 1 when
the pixel value is the neighborhood maximum, 0 otherwise)
"""
neighborhood = ndimage.morphology.generate_binary_structure(2,2)
local_max = ndimage.filters.maximum_filter(image, footprint=neighborhood)==image
background = (image==0)
eroded_background = ndimage.morphology.binary_erosion(background, structure=neighborhood, border_value=1)
detected_peaks = local_max - eroded_background
return detected_peaks
im = mahotas.imread('particles.jpg')
imf = ndimage.gaussian_filter(im, 3)
detected_peaks = detect_peaks(imf)
pylab.imshow(pymorph.overlay(im, detected_peaks))
pylab.show()
, :

, , , -, , ( 2, 3 4):



, , :

, .
EDIT: : , :
import cv2
import pylab
from scipy import ndimage
im = cv2.imread('particles.jpg')
pylab.figure(0)
pylab.imshow(im)
gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (5,5), 0)
maxValue = 255
adaptiveMethod = cv2.ADAPTIVE_THRESH_GAUSSIAN_C
thresholdType = cv2.THRESH_BINARY
blockSize = 5
C = -3
im_thresholded = cv2.adaptiveThreshold(gray, maxValue, adaptiveMethod, thresholdType, blockSize, C)
labelarray, particle_count = ndimage.measurements.label(im_thresholded)
print particle_count
pylab.figure(1)
pylab.imshow(im_thresholded)
pylab.show()
:
( )

( )
60.