Exemple #1
0
    def gaussian_otsu_thresholding(self, data):
        """ This function store the resulting image that has been in an thresholding process
        in diferent methods.
        :param data: The data of the image to process.
        :return: It return the images thresholded and the titles of the methods
        """
        # Otsu's thresholding after Gaussian filtering
        image_255 = np.array(data * 255, dtype=np.uint8)
        blur = cv2.GaussianBlur(image_255, (3, 5), 0)
        median_blur = cv2.medianBlur(image_255, 7)

        # Ridler & Calvard thresholding with median blur
        ret1 = mh.rc(median_blur)
        th1 = (median_blur > ret1)
        th1 = th1.astype(int)
        th1 = th1 * 255
        th1 = np.uint8(th1)

        # Rilder & Calvard thresholding without blur
        ret2 = mh.rc(image_255)
        th2 = (image_255 > ret2)
        th2 = th2.astype(int)
        th2 = th2 * 255
        th2 = np.uint8(th2)

        # Otsu's thresholding after Gaussian filtering
        ret3, th3 = cv2.threshold(blur, 0, 255,
                                  cv2.THRESH_BINARY + cv2.THRESH_OTSU)

        # plot all the images and their histograms
        images = [
            image_255, 0, th1, th1, image_255, 0, th2, th2, blur, 0, th3, th3
        ]

        # plot al the titles
        titles = [
            'Median Gaussian filtered TopHat Image', 'Histogram Freq.',
            'Ridler & Calvard Thresholding (v={0})'.format(int(ret1)),
            'Area Of Interest (AOI)', 'Original Noisy TopHat Image',
            'Histogram Freq.',
            "Ridler & Calvard Thresholding (v={0})".format(int(ret2)),
            'Area Of Interest (AOI)', 'Gaussian filtered TopHat Image',
            'Histogram Freq.', "Otsu's Thresholding (v={0})".format(int(ret3)),
            'Area Of Interest (AOI)'
        ]

        # it return all the images, histogram and titles.
        return images, titles
Exemple #2
0
def edgify(im):
    print classifications.keys()
    
    print "Blurring..."

    from scipy import misc
    from scipy import ndimage

    arr = misc.fromimage(img)
    very_blurred = ndimage.gaussian_filter(arr, sigma=15)

    arr = arr.astype(numpy.uint8)
    T_rc = mahotas.rc(arr)

    print "Saving as %s" % (outfile)
    misc.imsave(outfile, T_rc) 
Exemple #3
0
def circles():
    # Image 1 is the circles.png file
    my_image = mh.imread(filename)
    # Threshold using the Riddler-Calvard method 
    # More on Riddler-Calvard: http://mahotas.readthedocs.org/en/latest/thresholding.html
    thres = mh.rc(my_image)
    #use the value to form a binary image
    b_image = (my_image > thres)
    #use gaussian filter
    g_image = mh.gaussian_filter(b_image, 33)
    #separate objects stuck together
    rmax = mh.regmax(g_image)
    #count the number of objects in the picture
    labeled, nr_objects = mh.label(rmax)
    #find center point for each object
    centers = mh.center_of_mass(my_image, labeled)[1:]
    print "The circles.png file contains ", nr_objects, " objects."
    o = 1
    for center in centers:
        print "Object %s center: %s" %(o, center)
        o = o + 1
Exemple #4
0
def circles():
    # Image 1 is the circles.png file
    my_image = mh.imread(filename)
    # Threshold using the Riddler-Calvard method
    # More on Riddler-Calvard: http://mahotas.readthedocs.org/en/latest/thresholding.html
    thres = mh.rc(my_image)
    #use the value to form a binary image
    b_image = (my_image > thres)
    #use gaussian filter
    g_image = mh.gaussian_filter(b_image, 33)
    #separate objects stuck together
    rmax = mh.regmax(g_image)
    #count the number of objects in the picture
    labeled, nr_objects = mh.label(rmax)
    #find center point for each object
    centers = mh.center_of_mass(my_image, labeled)[1:]
    print "The circles.png file contains ", nr_objects, " objects."
    o = 1
    for center in centers:
        print "Object %s center: %s" % (o, center)
        o = o + 1
import mahotas
import mahotas.demos
import numpy as np
from pylab import imshow, gray, show
from os import path

img = mahotas.demos.load('lean', as_grey = True)
img = img.astype(np.uint8)

#Seuillage avec méthode Otsu
seuillage_otsu = mahotas.otsu(img)
imshow(img > seuillage_otsu)
show()

#seuillage avec méthode Riddler_Calvard
seuillage_rc = mahotas.rc(img)
imshow(img > seuillage_rc)
show()
Exemple #6
0
args = vars(ap.parse_args())

image = cv2.imread(args["image"])
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(image, (5, 5), 0)
plt.imshow(blurred, cmap="gray")

T = mahotas.otsu(blurred)
print("Otsu`s threshold:{}".format(T))

thresh = image.copy()
thresh[thresh > T] = 255
thresh[thresh < T] = 0
thresh = cv2.bitwise_not(thresh)
plt.figure()
plt.imshow(thresh, cmap="gray")

T = mahotas.rc(blurred)
print("Riddler-Calvard:{}".format(T))

thresh = image.copy()
thresh[thresh > T] = 255
thresh[thresh < T] = 0
thresh = cv2.bitwise_not(thresh)
cv2.imwrite("binew.png", thresh)

plt.figure()
plt.imshow(thresh, cmap="gray")

plt.show()
image_rgb_array = io.imread(image_file)

# (2) separate the colors
print 'Image array shape: ' + str(image_rgb_array.shape)
print 'Image array dtype: ' + str(image_rgb_array.dtype)
red_image = image_rgb_array[:, :, 0]  # take the red color only
green_image = image_rgb_array[:, :, 1]  #take the green color only

#save the red and green images as individual greayscale images
io.imsave('image_red.tif', red_image)
io.imsave('image_green.tif', green_image)

# (3) threshold the image, global methods return the threshold value:
#Ridler-Calvard method, this is default ImageJ 'threshold':
# http://fiji.sc/Auto_Threshold#Default
th = mahotas.rc(red_image)
red_mask = red_image > th

th = mahotas.rc(green_image)
green_mask = green_image > th

print 'RC threshold: ' + str(th)

#save red and green mask - white out True values and cast the data type
io.imsave('image_red_mask.tif',
          255 * red_mask.astype('uint8'))  #, plugin='freeimage')
io.imsave('image_green_mask.tif',
          255 * green_mask.astype('uint8'))  #, plugin='freeimage')

# (4) detect the clusters - cluster regions are labeled by integers, 1 to num_labels
#use 8-connectivity, diagonal pixels will be included as part of a structure
Exemple #8
0
import mahotas
import numpy as np
from pylab import imshow, gray, show, subplot
from os import path

luispedro_image = path.join(
                    path.dirname(path.abspath(__file__)),
                    'data',
                    'luispedro.jpg')

photo = mahotas.imread(luispedro_image, as_grey=True)
photo = photo.astype(np.uint8)

gray()
subplot(131)
imshow(photo)

T_otsu = mahotas.otsu(photo)
print(T_otsu)
subplot(132)
imshow(photo > T_otsu)

T_rc = mahotas.rc(photo)
print(T_rc)
subplot(133)
imshow(photo > T_rc)
show()

Exemple #9
0
import mahotas
import numpy as np
from pylab import imshow, gray, show, subplot
from os import path

luispedro_image = path.join(path.dirname(path.abspath(__file__)), 'data',
                            'luispedro.jpg')

photo = mahotas.imread(luispedro_image, as_grey=True)
photo = photo.astype(np.uint8)

gray()
subplot(131)
imshow(photo)

T_otsu = mahotas.otsu(photo)
print T_otsu
subplot(132)
imshow(photo > T_otsu)

T_rc = mahotas.rc(photo)
print T_rc
subplot(133)
imshow(photo > T_rc)
show()
print 'Image array dtype: ' + str(image_rgb_array.dtype)
red_image = image_rgb_array[:,:,0] # take the red color only (value is repeated in the blue to map to magenta)
green_image = image_rgb_array[:,:,1] #take the green color only

#save the red and green images as individual greayscale images
io.imsave('image_red.tif', red_image)
io.imsave('image_green.tif', green_image)

# (3) threshold the image, global methods return the threshold value:

#Otsu's method, as in http://scikit-image.org/docs/dev/auto_examples/plot_otsu.html
th = filter.threshold_otsu(red_image)
print 'Otsu: ' + str(th)

#Ridler-Calvard method, this is default ImageJ 'threshold': http://fiji.sc/Auto_Threshold#Default
th = mahotas.rc(red_image)
print 'RC: ' + str(th)

#also, local thresholding available which directly returns the masked array, e.g. filter.threshold_adaptive(...)

#mask is all values greater than threshold
red_mask = red_image > th

th  = mahotas.rc(green_image)
green_mask = green_image > th

#save red and green mask - white out True values and cast the data type
io.imsave('image_red_mask.tif', 255*red_mask.astype('uint8')) #, plugin='freeimage')
io.imsave('image_green_mask.tif', 255*green_mask.astype('uint8')) #, plugin='freeimage')

# (4) detect the clusters - cluster regions are labeled by integers, 1 to num_labels
Exemple #11
0
def applyth(image):
    newim = dict()
    for th in range(len(image)):
        newim[th] = mh.rc(image[th])
    return newim