def watershed_3d(sphere): """ Markers should be int8 Image should be uint8 """ sphere = median_filter(sphere, 3) thresh = threshold_otsu(sphere) sphere = (sphere >= thresh) * 1 sphere = sobel(sphere) size = (sphere.shape[0], sphere.shape[1], sphere.shape[2]) marker = np.zeros(size, dtype=np.int16) pl.imshow(sphere[:,:,50]) pl.show() # mark everything outside as background marker[5, :, :] = -1 marker[size[0] - 5, :, :] = -1 marker[:, :, 5] = -1 marker[:, :, size[2] - 5] = -1 marker[:, 5, :] = -1 marker[:, size[1] - 5, :] = -1 marker[:,0,0] = -1 # mark everything inside as a sphere marker[size[0] / 2., size[1] / 2., size[2] / 2.] = 5 result = measurements.watershed_ift(sphere.astype(dtype=np.uint16), marker) pl.imshow(result[:,:,50]) pl.show() return result
def watershed_3d(sphere): """ Markers should be int8 Image should be uint8 """ sphere = median_filter(sphere, 3) thresh = threshold_otsu(sphere) sphere = (sphere >= thresh) * 1 sphere = sobel(sphere) size = (sphere.shape[0], sphere.shape[1], sphere.shape[2]) marker = np.zeros(size, dtype=np.int16) pl.imshow(sphere[:, :, 50]) pl.show() # mark everything outside as background marker[5, :, :] = -1 marker[size[0] - 5, :, :] = -1 marker[:, :, 5] = -1 marker[:, :, size[2] - 5] = -1 marker[:, 5, :] = -1 marker[:, size[1] - 5, :] = -1 marker[:, 0, 0] = -1 # mark everything inside as a sphere marker[size[0] / 2., size[1] / 2., size[2] / 2.] = 5 result = measurements.watershed_ift(sphere.astype(dtype=np.uint16), marker) pl.imshow(result[:, :, 50]) pl.show() return result
def watershed2(img, markers): markers = np.int8(imtransform.setMarkersValues(markers, black=1, gray=-1, white=0)) # markers2 = np.zeros(markers.shape, np.uint8) # markers2[markers == 1] = 0 # markers2[markers ==-1] = 127 # markers2[markers == 0] = 255 # imsave(settings.UPLOAD_FOLDER + '/markers222222.png', markers2) greyImg = imtransform.rgb_2_greyscale(img) mask = watershed_ift(greyImg, markers) mask[mask < 0] = 0 mask = 255 * mask return np.array(mask, dtype=np.uint8)
def watershed2(img, markers): markers = np.int8( imtransform.setMarkersValues(markers, black=1, gray=-1, white=0)) # markers2 = np.zeros(markers.shape, np.uint8) # markers2[markers == 1] = 0 # markers2[markers ==-1] = 127 # markers2[markers == 0] = 255 # imsave(settings.UPLOAD_FOLDER + '/markers222222.png', markers2) greyImg = imtransform.rgb_2_greyscale(img) mask = watershed_ift(greyImg, markers) mask[mask < 0] = 0 mask = 255 * mask return np.array(mask, dtype=np.uint8)
def processImage(self): log.debug("IMAGE: " + self.image.z + ',' + self.image.t + ',' + self.image.ch) print 'In process image' if int(self.image.z) == 1 and int(self.image.t) == 1 and (int(self.image.ch) ==1 or int(self.image.ch) == 3): # read image from Bisque system resp, content = request(self.image.src + '?format=tiff', "GET", userpass = self.userpass ) # convert stream into Image im = cStringIO.StringIO(content) img = Image.open(im) log.debug("IMAGE: " + str(img.format) + ',' + str(img.size) + ',' + str(img.mode)) ''' IMAGE PROCESSING ''' # convert color image into grayscale image grayimg = ImageOps.grayscale(img) # convert Image into numpy array in_im = asarray(grayimg) # normalize image norm_im = self.normalizeImage(in_im) # set the threshold value thNorm = double((double(self.thValue)/100.0)) # threshold image with thNorm value th_im = norm_im < thNorm; # label image with 8conn structure = [[1,1,1], [1,1,1], [1,1,1]] th_im = binary_erosion(~th_im,structure) label_tuple = label(th_im,structure) #(data, dtype, number of labels) label_im = label_tuple[0]-1 # wathershed wh_im = watershed_ift(in_im, label_im) # convert numpy array into Image img_out = Image.fromarray(wh_im.astype('uint8')) ''' IMAGE PROCESSING ''' # convert Image into stream buffer = StringIO.StringIO() img_out.save(buffer, 'TIFF') # upload image into Bisque system buffer.seek(0) buffer.name = 'file.tif' fields = { 'file' : buffer } resp, content = post_files (self.client_server + '/bisquik/upload_images', fields=fields, userpass = self.userpass,) log.debug("RESP: " + str(content)) self.image_out_url = str(content)
from scipy.cluster.vq import vq, kmeans2, whiten from scipy.ndimage.measurements import watershed_ift from scipy.misc.pilutil import imread, imsave from matplotlib.pyplot import imshow import color img = imread("blue.jpg") markers = imread("bluem2.jpg") markers = int32(markers[:, :, 1]) markers = threshold(markers, 100, None, 1) # black to 1 print max(markers) print min(markers) markers = threshold(markers, None, 200, -1) # white to -1 print max(markers) print min(markers) markers = threshold(markers, None, 2, 0) # gray to 0 markers = int8(markers) mask = watershed_ift(img[:, :, 1], markers) print mask mask = mask + 1 print mask print max(mask) mask = threshold(mask, None, 1, 1) print max(mask) mask = 255 * mask imsave("mask_watershed.jpg", mask) a = 1